有没有一种方法可以从graphql.js解析程序的信息中重建查询?

时间:2019-11-27 06:15:27

标签: graphql

    let root = {
        books: (parent, args, info) { ... }
    }
    let res = await graphql(schema, `{ books { title } }`, root);

graphql.js中是否已经有一个辅助函数来使用info来重建查询?

1 个答案:

答案 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 } }