在上下文中附加属性(express.js中的请求)对象
我是GraphQL的新手,我现在正尝试使用express-graphql
制作API服务器。
我想做的是在context
对象中为解析器添加新属性,这可以在初始化express-graphql
服务器实例时完成。
根据官方文档,默认情况下表示,如果代码中未指定任何内容,则每个解析器函数中的context
对象将具有先前在Express.js]中称为req
的对象>。
app.use( '/graphql', graphqlHTTP({ schema: schema, graphiql: true }) ) // by this, // in resolver there will be `context` available // which includes the data previously available in `req` object (express.js case)
然后,如果我想在
req
或context
对象中添加自己的自定义属性,并且所有req
的成员都在一起怎么办?我只想添加一个或两个新值,但不丢失req
已经拥有的其他值。
app.use(
'/graphql',
graphqlHTTP({
schema: schema,
graphiql: true,
context: {
customData: 'Yay!'
}
})
)
// I was able to add my own data in `context`,
// but eventually I lost all of data which was previously in the `req` object!