Ошибка файла GraphQL Schema.js — узел/экспресс

Я использую node, express, mongoose и graphql.

Я получаю эту ошибку в консоли graphql: "message": "The type of SocialPostInQue.socialPost must be Output Type but got: undefined.\n\nThe type of SocialPostInQue.schedule must be Output Type but got: undefined.\n\nThe type of Mutation.addSocialPostInQue(socialPost:) must be Input Type but got: undefined.\n\nThe type of Mutation.addSocialPostInQue(schedule:) must be Input Type but got: undefined." Я думаю, что ошибка связана с типом в файле Schema.js.

Я не знаю, откуда берется undefined, потому что я не запускал запрос или мутацию. Вы видите какие-либо проблемы в моем коде?

Мой файл схемы SocialPostInQue:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const SocialPostInQueSchema = new Schema({
    userId: String,
    socialPost: {
        id: String,
        message: String,
        image: {
            url: String
        }
    },
    schedule: {
        month: String,
        date: Number,
        hour: String,
        minute: String
    }
});

module.exports = mongoose.model('SocialPostInQue', SocialPostInQueSchema);

И мой файл Schema.js:

const axios = require('axios');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const {
    GraphQLObjectType,
    GraphQLString,
    GraphQLInt,
    GraphQLID,
    GraphQLSchema,
    GraphQLList,
    GraphQLNonNull
} = require('graphql');

/****** Mongoose Schemas ******/
const SOCIALPOSTINQUE = require('./socialPostInQue');

/******* Types POSSIBLE ORIGIN*******/
const SocialPostInQueType = new GraphQLObjectType({
    name:'SocialPostInQue',
    fields:() => ({
        id: {type:GraphQLID},
        userId: {type:GraphQLID},
        socialPost: {
            id: {type:GraphQLID},
            message: {type:GraphQLString},
            image: {
                url: {type:GraphQLString}
            }
        },
        schedule: {
            month: {type:GraphQLString},
            date: {type:GraphQLInt},
            hour: {type:GraphQLString},
            minute: {type:GraphQLString}
        }
    })
});

/****** functions ******/
const socialPostInQueList = () => {
    return new Promise((resolve, reject) => {
        SOCIALPOSTINQUE.find((err, socialPostsInQue) => {
            if (err) reject(err)
            else resolve(socialPostsInQue)
        })
    })
};

/****** Root Query WHERE 'SocialPostInQue.socialPost OUTPUT UNDEFINED ERROR IS******/
const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        socialPostInQue: {
            type: SocialPostInQueType,
            args: {
                id: {type:GraphQLID}
            },
            resolve (parentValue, {id}) {
                return SOCIALPOSTINQUE.findById(id)
            }
        },
        socialPostsInQue:{
            type: new GraphQLList (SocialPostInQueType),
            resolve (parentValue, args) {
                return socialPostInQueList()
            }
        }
    }
})

/***** Root Mutations WHERE 'Mutation.addSocialPostInQue...' ERRORS COME FROM*******/
const mutation = new GraphQLObjectType({
    name:'Mutation',
    fields:{
        addSocialPostInQue:{
            type: SocialPostInQueType,
            args:{
                userId: {type: new GraphQLNonNull (GraphQLID)},
                socialPost: {
                    id: {type: new GraphQLNonNull (GraphQLID)},
                    message: {type: new GraphQLNonNull (GraphQLString)},
                    image: {
                        url: {type: new GraphQLNonNull (GraphQLString)}
                    }
                },
                schedule: {
                    month: {type: new GraphQLNonNull (GraphQLString)},
                    date: {type: new GraphQLNonNull (GraphQLInt)},
                    hour: {type: new GraphQLNonNull (GraphQLString)},
                    minute: {type: new GraphQLNonNull (GraphQLString)}
                }
            },
            resolve(parentValue, args){
                console.log('READ', args)
                let newSocialPostInQue = new SOCIALPOSTINQUE({
                    name: args.name,
                    email: args.email,
                    age: args.age,
                    userId: args.userId,
                    socialPost: {
                        id: args.socialPost.id,
                        message: args.socialPost.message,
                        image: {
                            url: args.socialPost.image.url
                        }
                    },
                    schedule: {
                        month: args.schedule.month,
                        date: args.schedule.date,
                        hour: args.schedule.hour,
                        minute: args.schedule.minute
                    }
                });
                return new Promise((resolve, reject) => {
                    newSocialPostInQue.save(function (err) {
                        if(err) reject(err)
                        else resolve(newSocialPostInQue)
                    })
                    console.log ("New Social Post In Que Added")
                });
            }
        }
    }
})

module.exports = new GraphQLSchema({
    query: RootQuery,
    mutation
});

person Kevin Danikowski    schedule 26.12.2017    source источник


Ответы (1)


Все вложенные нескалярные типы должны создаваться как объектные типы. Обратите внимание на поля socialPost, image и schedule:

const SocialPostInQueType = new GraphQLObjectType({
    name:'SocialPostInQue',
    fields:() => ({
        id: {type:GraphQLID},
        userId: {type:GraphQLID},
        socialPost: new GraphQLObjectType({
            name: 'SocialPostType',
            fields: {
                id: {type:GraphQLID},
                message: {type:GraphQLString},
                image: new GraphQLObjectType({
                    name: 'ImageType',
                    fields: {
                        url: {type:GraphQLString}
                    }
                })
            }
        }),
        schedule: new GraphQLObjectType({
            name: 'SocialPostSchedule',
            fields: {
                month: {type:GraphQLString},
                date: {type:GraphQLInt},
                hour: {type:GraphQLString},
                minute: {type:GraphQLString}
            }
        })
    })
})
person Petr Bela    schedule 08.01.2018