【发布时间】:2020-03-23 02:38:39
【问题描述】:
这是我的 schema.js 文件。我遵循了一个教程,所以不确定我哪里出错了。任何帮助表示赞赏,谢谢!这是终端中的错误消息:
错误:Show.resolve 字段配置必须是对象
由于我是 GraphQL 的新手,我再次不确定我错了。
const graphql = require('graphql')
const _ = require('lodash')
const Show = require('../models/show')
const { GraphQLObjectType, GraphQLString, GraphQLSchema, GraphQLID, GraphQLInt, GraphQLList } =
graphql
const ShowType = new GraphQLObjectType({
name: 'Show',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
genre: { type: GraphQLString },
year: { type: GraphQLInt },
poster: { type: GraphQLString },
resolve(parent, args) {
// return _.find(users, { id: parent.userId } )
return Show.findById(args.id)
}
})
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
show: {
type: ShowType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return Show.findById(args.id)
}
},
shows: {
type: new GraphQLList(ShowType),
resolve(parent, args) {
// return shows
return Show.find({})
}
}
}
})
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addShow: {
type: ShowType,
args: {
name: { type: GraphQLString },
genre: { type: GraphQLString },
year: { type: GraphQLInt },
poster: { type: GraphQLString },
},
resolve(parent, args) {
let show = new Show({
name: args.name,
genre: args.genre,
year: args.year,
poster: args.poster,
})
return show.save()
}
}
}
})
【问题讨论】:
-
解析器函数应与特定字段相关联。查看
Show的字段——看起来对吗?与您的其他类型相比,该字段配置有什么不同(除了它是一个函数这一事实——那部分很好)。 -
我不确定你的意思 Daniel Rearden,你能详细说明一下吗?谢谢。
-
我在 ShowType 中取出了 resolve 函数,不确定这是否是正确的修复,但它现在可以工作了。
-
你不能有一个类型的解析器——只有字段有解析器。
标签: graphql