模式:
const graphql = require("graphql");
const User = require("../models/user");
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
GraphQLID,
GraphQLList
} = graphql;
const CompanyType = new GraphQLObjectType({
name:'Company',
fields: () => ({
name: { type: GraphQLString },
catchPhrase: { type: GraphQLString },
bs: { type: GraphQLString },
})
})
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id : { type: GraphQLID },
name : { type: GraphQLString },
username : { type: GraphQLString },
email : { type: GraphQLString },
address : { type: GraphQLString },
phone : { type: GraphQLInt },
website : { type: GraphQLString },
company : new GraphQLList(CompanyType)
})
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: {
id: { type: GraphQLID }
},
resolve(parent, args){
return User.findById(args.id);
}
},
users: {
type: new GraphQLList(UserType),
resolve(parent, args){
return User.find({});
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery
});
型号:
const mongoose = require('mongoose');
const Scheme = mongoose.Schema;
const userSchema = new Scheme({
id : Number,
name : String,
username : String,
email : String,
address : Object,
phone : Number,
website : String,
company : Object
})
module.exports = mongoose.model('User', userSchema)
在这里,我试图从mongodb数据库中获取数据。 我已经使用带有graphql的expressjs设置了我的服务器,并将mongoose用于mongodb客户端。
但是在graphiql中进行查询时,我遇到了以下错误:
{
"errors": [
{
"message": "The type of User.company must be Output Type but got: undefined."
}
]
}
我的结果是使用嵌套的json,所以我正在使用GraphQLList。
请看看我做错了什么
答案 0 :(得分:0)
company : {type: CompanyType}
且架构应为
const userSchema = new Scheme({
id : Number,
name : String,
username : String,
email : String,
address : Object,
phone : Number,
website : String,
company : {
name: String,
catchPhrase: String,
bs: String
}
})