我是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并且无法弄清楚我做错了什么。
答案 0 :(得分:1)
您无意中为user
查询解析器查询了Query类型中的一个字段。将它移到Map
字段内,你应该很好。