Node.js GraphQL Query.resolve必须是对象
我是GraphQL新手,尝试针对模拟数据源建立一个基本查询设置,该模拟数据源仅通过Node.js GraphQL Query.resolve必须是对象,node.js,graphql,Node.js,Graphql,我是GraphQL新手,尝试针对模拟数据源建立一个基本查询设置,该模拟数据源仅通过过滤器解析通过id提取记录的承诺。我有以下几点: const { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLList } = require('graphql') const db = require('../db') const getUserById = (id) => db.read(id) const User
过滤器
解析通过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
Error: Query.resolve field config must be an object
当我尝试使用graphQLHTTP
运行此程序时,我得到以下结果:
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
Error: Query.resolve field config must be an object
我一直在跟踪,无法找出我做错了什么。您意外地将
用户的解析器设置为查询类型中的一个字段。将其移动到user
字段中,您应该会表现良好。完美!谢谢您!