GraphQL 将参数传递给子解析

GraphQL pass args to sub resolve

我和 User 之间有关系 Post。这就是我查询用户 Posts.

的方式
const UserType = new GraphQLObjectType({
  name: 'User'
  fields: () => ({
    name: {
      type: GraphQLString
    },
    posts: {
      type: new GraphQLList(PostType),
      resolve(parent, args , { db }) {
        // I want to get here the args.someBooleanArg
        return someLogicToGetUserPosts();
      }
    }
  })
});

主要查询是:

const queryType = new GraphQLObjectType({
  name: 'RootQuery',
  fields: {
    users: {
      type: new GraphQLList(UserType),
      args: {
        id: {
          type: GraphQLInt
        },
        someBooleanArg: {
          type: GraphQLInt
        }
      },
      resolve: (root, { id, someBooleanArg }, { db }) => {
        return someLogicToGetUsers();
      }
    }
  }
});

问题是 UserType 帖子的解析函数中的参数是空对象,我如何将参数从主查询传递到子解析函数?

解析根查询时,您可以使用对象分配将参数附加到返回的用户对象。 然后,在用户类型上,从根值(解析函数的第一个参数)解析参数。

示例:

const queryType = new GraphQLObjectType({
  name: 'RootQuery',
  fields: {
    users: {
      type: new GraphQLList(UserType),
      args: {
        id: {
          type: GraphQLInt
        },
        someBooleanArg: {
          type: GraphQLInt
        }
      },
      resolve: (root, { id, someBooleanArg }, { db }) => {
        return Promise.resolve(someLogicToGetUsers()).then(v => {
            return Object.assign({}, v, {
                someBooleanArg
            });
        });
      }
    }
  }
});

const UserType = new GraphQLObjectType({
  name: 'User'
  fields: () => ({
    name: {
      type: GraphQLString
    },
    posts: {
      type: new GraphQLList(PostType),
      resolve(parent, args , { db }) {
        console.log(parent.someBooleanArg);
        return someLogicToGetUserPosts();
      }
    }
  })
});

您可以使用解析器第四个参数 info 来接收所需的变量 - 来自 Apollo 文档:

Every resolver in a GraphQL.js schema accepts four positional arguments:

fieldName(obj, args, context, info) { result }

These arguments have the following meanings and conventional names:

obj: The object that contains the result returned from the resolver on the parent field, or, in the case of a top-level Query field, the rootValue passed from the server configuration. This argument enables the nested nature of GraphQL queries.

args: An object with the arguments passed into the field in the query. For example, if the field was called with author(name: "Ada"), the args object would be: { "name": "Ada" }.

context: This is an object shared by all resolvers in a particular query, and is used to contain per-request state, including authentication information, dataloader instances, and anything else that should be taken into account when resolving the query. If you're using Apollo Server, read about how to set the context in the setup documentation.

info: This argument should only be used in advanced cases, but it contains information about the execution state of the query, including the field name, path to the field from the root, and more. It's only documented in the GraphQL.js source code.

info 似乎是一个非常没有记录的功能,但我现在使用它没有任何问题(至少在有人决定更改它之前)。

技巧如下:

const UserType = new GraphQLObjectType({
  name: 'User'
  fields: () => ({
    name: {
      type: GraphQLString
    },
    posts: {
      type: new GraphQLList(PostType),
      resolve(parent, args , { db }, info) {
        // I want to get here the args.someBooleanArg

        console.log("BINGO!");
        console.log(info.variableValues.someBooleanArg);

        return someLogicToGetUserPosts();
      }
    }
  })
});