【问题标题】:How to pass arguments to resolve function in a GraphQL Object?如何传递参数以解析 GraphQL 对象中的函数?
【发布时间】:2017-11-13 02:39:39
【问题描述】:

我在 Javascript 中使用 GraphQL,我希望能够将参数传递给 GraphQLObjectType 字段中的 resolve() 函数。

这是GraphQLObjectType 声明:

export const ModelSchema = new GraphQLObjectType({
  name: 'Model',
  description: 'Model information',
  fields: () => ({
    tags: {
      type: TagList,
      description: 'Model\'s UUID',
      async resolve(obj, args) {
        console.log('args', args); // expecting to see an object
      },
    },
  }),
});

这是我想在 GraphQLI 中查询的方式:

{
  getModels(UUIDs:"0AAAA2EFF6677194ED227EE4AAAA8D4A") {
    total
    models {
      tags (limit: 1) {
        tags {
          UUID
          name
        }
      }
    }
  }
}

所以我希望能够将参数(在本例中为 limit)发送到 tags,因此当调用 resolve() 函数时,我可以使用此参数并限制结果,或者执行其他操作。

我该怎么做?

谢谢

【问题讨论】:

    标签: arguments subquery schema graphql resolve


    【解决方案1】:

    好的,知道了...需要像这样添加args:

    export const ModelSchema = new GraphQLObjectType({
      name: 'Model',
      description: 'Model information',
      args: {
            limit: {
              type: GraphQLInt,
            },
          },
      fields: () => ({
        tags: {
          type: TagList,
          description: 'Model\'s UUID',
          async resolve(obj, args) {
            console.log('args', args); // expecting to see an object
          },
        },
      }),
    });
    

    现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 2016-12-12
      • 2017-02-05
      • 2019-04-18
      • 2017-04-19
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      相关资源
      最近更新 更多