GraphQL的新手,出现此错误,错误:Show.resolve字段配置必须是一个对象

问题描述 投票:0回答:1

这是我的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()
            }
        }
    }
})
graphql
1个回答
0
投票

因为无法将解析函数放入子对象中。

例如

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,您会错过这一点。

© www.soinside.com 2019 - 2024. All rights reserved.