【问题标题】:GraphQL Query.resolve must be an objectGraphQL Query.resolve 必须是一个对象
【发布时间】:2018-04-14 01:55:33
【问题描述】:

我是 GraphQL 新手,并试图针对一个模拟数据源进行基本查询设置,该数据源只是用 filter 解决了一个通过 id 提取记录的承诺。我有以下内容:

const {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLList
} = require('graphql')

const db = require('../db')

const getUserById = (id) => db.read(id)

const UserType = new GraphQLObjectType({
  name: 'User',
  description: 'User Type',
  fields: () => ({
    first_name: {
      type: GraphQLString
    },
    last_name: {
      type: GraphQLString
    },
    email: {
      type: GraphQLString
    },
    friends: {
      type: new GraphQLList(UserType),
      resolve: (user) => user.friends.map(getUserById)
    }
  })
})

const QueryType = new GraphQLObjectType({
  name: 'Query',
  description: 'User Query',
  fields: () => ({
    user: {
      type: UserType,
      args: {
        id: { type: GraphQLString }
      }
    },
    resolve: (root, args) => getUserById(args.id)
  })
})

const schema = new GraphQLSchema({
  query: QueryType
})


module.exports = schema

当我尝试使用 graphQLHTTP 运行它时,我得到以下信息:

Error: Query.resolve field config must be an object

我一直在关注Zero to GraphQL in 30 Minutes,但不知道我做错了什么。

【问题讨论】:

    标签: node.js graphql


    【解决方案1】:

    您不小心将user 查询的解析器设置为查询类型的字段之一。将它移到user 字段中,你应该会很好。

    【讨论】:

    • 完美!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 2021-09-16
    • 2017-12-31
    • 2013-09-09
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    相关资源
    最近更新 更多