【发布时间】:2019-12-23 19:53:46
【问题描述】:
架构:
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。
请看看我哪里做错了
【问题讨论】: