node.js - GraphQL.js Node/ express : How to pass object as GraphQL query argument

标签 node.js express graphql

我的目标是能够在 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),
    },

  }),
});

最佳答案

我在这里找到了解决方案。 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
  }
}

关于node.js - GraphQL.js Node/ express : How to pass object as GraphQL query argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42722888/

相关文章:

javascript - 实现实时传感器仪表板

javascript - 如何在nodejs中使用相同的文件执行export.module?

javascript - 如何从 Node.js Express 服务器将 JSON 数据返回到 Riot.js?

javascript - 不使用 CORS 访问后端 API

node.js - Winston:尝试在没有传输的情况下写入日志

node.js - Heroku 上的 Node/Express 应用程序 : Web process failed to bind to $PORT within 60 seconds of launch

Javascript:在方法内部调用方法本身(使用原始参数)

java - GraphQL-SPQR 中返回错误的正确方法

javascript - 具有自己查询的 Gatsby 可重用组件

python - 如何在 Django/Elasticsearch/MySQL 后端之上构建 GraphQL API?