Новичок в GraphQL, появляется эта ошибка. Ошибка: поле конфигурации Show.resolve должно быть объектом - PullRequest
0 голосов
/ 17 февраля 2020

Это моя схема. 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()
            }
        }
    }
})

1 Ответ

0 голосов
/ 22 марта 2020

потому что вы не можете поместить функцию разрешения в дочерний объект.

ex.

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 в parent схема, которую вы пропустите.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...