let root = {
books: (parent, args, info) { ... }
}
let res = await graphql(schema, `{ books { title } }`, root);
graphql.js中是否已经有一个辅助函数来使用info
来重建查询?
答案 0 :(得分:0)
是的,有一种方法:
当您使用来自express-graphql的graphqlHTTP时,第三个参数是graphQLParams,您可以为其提供实例上下文。
graphqlHTTP(async (request, response, graphQLParams) => ({
schema,
graphiql: true,
context: graphQLParams
}))
然后在您的模式定义中,您可以从以下任何解析函数中获取上下文:
products: {
type: new GraphQLList(productType),
resolve: (parent, args, { query }, info) => {
return getProducts({
query
});
}
},
context.query是您在此示例中的实际查询,它是:
{ products { id } }