我是不熟悉graphQL的MERN的人。我收到以下错误。我被困了两天。我编写了MainSchema和GraphQL查询。我不太了解我对哪里出错了。
以下是后端代码,其中我使用Express,GraphQL,MongoDB。请让我知道是否遗漏了任何东西
grapqiQL
{
"errors": [
{
"message": "Type MainQuery must define one or more fields."
},
{
"message": "Type Mains must define one or more fields."
}
]
}
MainSchema:MongoDB模式
let Mongoose=require('mongoose');
let MainSchema=new Mongoose.Schema({
id:String,
MainName:{type:String,required:true,unique:true},
Owner:String,
MainType:String,
subCategory1:{
subCategory1Name:String,
CreatedDate:{type:Date,default:Date}
},
subCategory2:{
subCategory2Id:String,
subCategory2Name:String,
subCategory2Role:Number,
CreatedDate:{type:Date,default:Date}
},
CreatedDate:{type:Date,default:Date}
});
module.exports=Mongoose.model('MainSchema',MainSchema);
MainType:GraphQL类型
let GraphQLObjectType = require('graphql').GraphQLObjectType;
let GraphQLString = require('graphql').GraphQLString;
let GraphQLBoolean = require('graphql').GraphQLBoolean;
let GraphQLInt = require('graphql').GraphQLInt;
let GraphQLDate = require('graphql-date');
let MainType = new GraphQLObjectType({
name:'Mains',
feilds:function(){
return{
id:{
type:GraphQLString
},
MainName:{
type:GraphQLString
},
Owner:{
type:GraphQLString
},
MainType:{
type:GraphQLString
},
subCategory1:{
subCategory1Name: {
type:GraphQLString
},
CreatedDate:{
type:GraphQLDate
}
},
subCategory2:{
subCategory2Id:{
type:GraphQLInt
},
subCategory2Name:{
type:GraphQLString
},
subCategory2Role:{
type:GraphQLInt
},
CreatedDate:{
type:GraphQLDate
}
},
CreatedDate:{
type:GraphQLDate
}
}
}
});
module.exports=MainType;
MainQueryType:graphQl查询类型
let GraphQLObjectType = require('graphql').GraphQLObjectType;
let GraphQLString = require('graphql').GraphQLString;
let GraphQLList = require('graphql').GraphQLList;
let MainSchema=require('./MainsSchema');
let MainType=require('./MainsType');
let QueryType=new GraphQLObjectType({
name:"MainQuery",
feilds:function(){
return{
Mains:{//List of all Mains
type:new GraphQLList(MainType),
resolve:function(){
const Mains=MainSchema.find().exec()
if(!Mains){
throw new Error("Error")
}
return Mains
}
},
Main:{//search name
type:MainType,
args:{
id:{
name:'id',
type:GraphQLString
},
MainName:{
name:'MainName',
type:GraphQLString
}
},
resolve:function(root,params){
const MainDetails=''
if(params.id){
MainDetails=MainSchema.find({}).exec()
}else{
MainDetails=MainSchema.find({MainName:params.MainName}).exec()
}
if(!MainDetails){
throw new Error('Error')
}
return MainDetails
}
}
}
}
});
module.exports=QueryType;
MainMutation:GraphQL突变
let GraphQLSchema = require('graphql').GraphQLSchema;
let GraphQLObjectType = require('graphql').GraphQLObjectType;
let GraphQLString = require('graphql').GraphQLString;
let GraphQLNonNull = require('graphql').GraphQLNonNull;
let MainSchema=require('./MainsSchema');
let MainType=require('./MainsType');
let MainQueryType=require('./MainsQueryType');
let Mutation=new GraphQLObjectType({
name:"Mutation",
fields:function(){
return{
addMain:{
type:MainType,
args:{
MainName:{
type:new GraphQLNonNull(GraphQLString)
}
},
resolve:function(root,params){
const MainSchema=new MainSchema(params);
const newMain=MainSchema.save();
if(!newMain){
throw new Error("Error")
}
return newMain
}
},
removeMain:{
type:MainType,
args:{
id:{
type:new GraphQLNonNull(GraphQLString)
}
},
resolve:function(root,params){
const removeMain=MainSchema.findByIdAndRemove(params.id).exec();
if(!removeMain){
throw new Error("Error")
}
return removeMain
}
}
}
}
});
module.exports=new GraphQLSchema({query:MainQueryType,mutation:Mutation});
app.js
const MainSchema=require("./Mains/MainMutations");
app.use('/graphql',cors(),ExpressGraphQLHTTP({
schema:MainSchema,
rootValue:global,
graphiql:true
}))
我想Type,QueryType和Mutation出了点问题,我不知道我要去哪里,我用谷歌搜索了一下,但没有发现太多有关该错误的内容,我发现那条代码不起作用我我不太了解。
任何人都可以让我知道我要去哪里了。我们会尽力帮助您。
任何人都可以指导。