【问题标题】:GraphQL Schema.js File Error - Node/expressGraphQL Schema.js 文件错误 - 节点/快递
【发布时间】:2018-06-07 14:13:49
【问题描述】:

我正在使用 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
});

【问题讨论】:

    标签: node.js express mongoose graphql graphql-js


    【解决方案1】:

    所有嵌套的非标量类型都需要构造为对象类型。注意socialPostimageschedule 字段:

    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}
                }
            })
        })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 2018-05-31
      • 2022-01-25
      • 1970-01-01
      相关资源
      最近更新 更多