GraphQL Schema.js 文件错误 - 节点/快递

Posted

技术标签:

【中文标题】GraphQL Schema.js 文件错误 - 节点/快递【英文标题】:GraphQL Schema.js File Error - Node/express 【发布时间】: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
);

【问题讨论】:

【参考方案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
            
        )
    )
)

【讨论】:

以上是关于GraphQL Schema.js 文件错误 - 节点/快递的主要内容,如果未能解决你的问题,请参考以下文章

relayjs 和 graphql 错误:错误:“节点”需要字段“id”

Graphql 字段上的未知参数

如何将 GraphQL 连接到 MySQL

如何将这个现有的 json 转换为 GraphQL 端点?

我们如何递归地自省 GraphQL 模式?

Apollo-server-express 返回“无法获取 /”而不是 Graphql 操场