【问题标题】:GraphQL.js Node/Express: How to pass object as GraphQL query argumentGraphQL.js Node/Express:如何将对象作为 GraphQL 查询参数传递
【发布时间】:2017-08-01 01:32:17
【问题描述】:

我的目标是能够在 GraphQL 查询中将对象作为参数传递。

目标:

{
    accounts (filter: 
      {"fieldName": "id",
      "fieldValues":["123"],
      "filterType":"in"}){
        id
      }
 }

错误:

"message": "filterType fields must be an object with field names as keys or a function which returns such an object."

我尝试了几种不同的方法,但这似乎是最接近潜在解决方案的方法。

架构:

const filterType = new GraphQLObjectType ({
  name: 'filterType',
  fields: {
    fieldName: { type: GraphQLString },
    fieldValues: { type: GraphQLString },
    filterType: { type: GraphQLString },
  }
})

const QueryType = new GraphQLObjectType({
  name: 'Query',
  fields: () => ({
    accounts: {
      type: new GraphQLList(accountType),
      args: {
        filter: { type: new GraphQLInputObjectType(filterType) },
      },
      resolve: (root, args, { loaders }) => loaders.account.load(args),
    },

  }),
});

【问题讨论】:

    标签: node.js express graphql


    【解决方案1】:

    我在这里找到了解决方案。 https://github.com/mugli/learning-graphql/blob/master/7.%20Deep%20Dive%20into%20GraphQL%20Type%20System.md#graphqlinputobjecttype

    架构:

    const filterType = new GraphQLInputObjectType({
      name: 'filterType',
      fields: {
        fieldName: { type: GraphQLString },
        fieldValues: { type: GraphQLString },
        filterType: { type: GraphQLString },
      }
    })
    
    const QueryType = new GraphQLObjectType({
      name: 'Query',
      fields: () => ({
        accounts: {
          type: new GraphQLList(accountType),
          args: {
            filter: { type: filterType },
          },
          resolve: (root, args, { loaders }) => {
            return loaders.account.load(args)},
        },
      }),
    });
    

    问题出在查询中,我将键和值都作为对象参数中的字符串。

    正确查询:

    {
      accounts(filter: {fieldName: "id", fieldValues: "123", filterType: "in"}) {
        id
      }
    }
    

    【讨论】:

    • 创建GraphQLInputObjectType 并以您在查询中的方式使用它没有任何意义 - 您可以简单地在args 中定义这些字段,而不是创建新的输入类型...
    • @piotrbienias args 使用GraphQLInputObjectType。像这样使用没有任何意义,但为什么呢?这不能被认为是一种更清洁的方式吗?即使在 args 中,我们也必须定义字段和类型
    【解决方案2】:

    您没有将 filterType 定义为对象类型,然后将其包装在输入类型中,而是将其创建为输入类型:

    const filterType = new GraphQLInputObjectType({
      name: 'filterType',
      fields: {
        fieldName: { type: GraphQLString },
        fieldValues: { type: GraphQLString },
        filterType: { type: GraphQLString },
      }
    })
    
    const QueryType = new GraphQLObjectType({
      name: 'Query',
      fields: () => ({
        accounts: {
          type: new GraphQLList(accountType),
          args: {
            filter: { type: filterType },
          },
          resolve: (root, args, { loaders }) => loaders.account.load(args),
        },
      }),
    });
    

    您还需要在查询时声明其类型,如@piotrbienias 的回答所示。

    【讨论】:

    • 您好安德鲁,感谢您的意见。我尝试了此解决方案,但收到与以前相同的错误。 “语法错误 GraphQL 请求 (3:8) 预期名称,找到字符串”
    猜你喜欢
    • 2017-07-31
    • 2021-03-30
    • 2017-05-28
    • 2017-09-27
    • 2023-01-26
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多