我正在使用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
});
答案 0 :(得分:0)
所有嵌套的非标量类型都需要构造为对象类型。请注意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}
}
})
})
})