【问题标题】:graphql mongoose must be Output Type but got: undefinedgraphql mongoose 必须是输出类型,但得到:未定义
【发布时间】: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。

请看看我哪里做错了

【问题讨论】:

    标签: mongodb graphql


    【解决方案1】:
    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
    }
    })
    

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 2019-09-19
      • 2018-12-18
      • 2017-08-16
      • 2018-06-25
      • 2016-10-14
      • 2018-11-18
      • 2016-06-29
      • 2017-12-12
      相关资源
      最近更新 更多