【问题标题】:New to GraphQL, getting this error, Error: Show.resolve field config must be an objectGraphQL 新手,收到此错误,错误:Show.resolve field config must be an object
【发布时间】: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


【解决方案1】:

因为您不能将解析函数放入子对象中。

例如

var bookschema = new GraphQLObjectType({
    name : 'book',
    fields : ( ) => ({
        name : {type : GraphQLString},
        genre : {type : GraphQLString},
        id : {type : GraphQLID },
        auther : {
            type : Autherschema,//it is a another schema.
            resolve(parent,args)
            {
                return _.find(Authers,{id : parent.autherid})
            }
        }
    })
});

在我的示例中,'Autherschema' 是另一个模式,我在父模式中称为 auther,你错过了。

【讨论】:

    猜你喜欢
    • 2013-07-25
    • 2023-03-06
    • 2021-09-27
    • 2020-08-07
    • 2021-11-23
    • 2022-08-08
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多