Meal.nutrition 的类型必须是输出类型,但得到:未定义 [重复]

Posted

技术标签:

【中文标题】Meal.nutrition 的类型必须是输出类型,但得到:未定义 [重复]【英文标题】:The type of Meal.nutrition must be Output Type but got: undefined [duplicate] 【发布时间】:2020-05-31 08:20:50 【问题描述】:

我在使用 GraphQL、Express.js 和 MongoDB 设置服务器时遇到了这个问题。这似乎是某种参考错误,我不确定问题是什么,我试图寻找我的答案但似乎找不到它。

我在 GraphiQL 上遇到的错误是这样的,我不知道为什么


  "errors": [
    
      "message": "The type of Meal.nutrition must be Output Type but got: undefined."
    
  ]

无论如何,在 app.js 上 代码是

const express = require("express")
const app = express();
const userSchema = require("./graph-schema/userQueries")
const workoutSchema = require("./graph-schema/workoutQueries")
const mealSchema = require("./graph-schema/mealQueries")
const mongoose = require("mongoose")
const mergeSchemas = require("graphql-tools")

//connect to mongoDB atlase database
mongoose.connect("mongodb+srv://Z***98:*****@cluster0-epauj.mongodb.net/test?retryWrites=true&w=majority")
mongoose.connection.once("open", () => 
    console.log("Connected to database")
)

const combinedSchemas = mergeSchemas(
    schemas: [
        userSchema,
        mealSchema,
        workoutSchema
    ]
)


//this module allows express to communicate with graphql ;
//we use it as a single endpoint
const graphqlHTTP = require("express-graphql")

app.use("/graphql" , graphqlHTTP(
    schema: combinedSchemas,
    graphiql: true


))


app.listen(4000, () => 
    console.log(`Listening on port 4000`)
)

MealType 定义在一个名为 schema.js 的文件中,该文件包含我需要的所有其他类型(UserType、AuthType、WorkoutType)。

const graphql = require("graphql")
const Workout = require("../models/Workout.js")
const User = require("../models/User.js")
const Meal = require("../models/Meal")

const GraphQLObjectType, GraphQLID, GraphQLString, GraphQLSchema, GraphQLInt, GraphQLList = graphql;

//describes what attributes and its types, a User has in each query
const UserType = new GraphQLObjectType(
    name: "User",
    fields: () => (
        id: type: GraphQLID,
        name: type: GraphQLString,
        email: type: GraphQLString,
        password: type: GraphQLString,
        workouts: 
            type: new GraphQLList(WorkoutType),
            resolve(parent, args)
                //returns all the workouts created by a user
                return Workout.findById(userId: parent.id)
            
        ,
        meals: 
            type: new GraphQLList(MealType),
            resolve(parent, args)
                //returns all the meals created by a user
                return Meal.findById(userId: parent.id)
            
        

    )
)



const WorkoutType = new GraphQLObjectType(
    name: "Workout",
    fields: () => (
        id: type: GraphQLID,
        name: type: GraphQLString,
        reps: type: GraphQLInt,
        burnedCalories: type: GraphQLInt,
        sets: type: GraphQLInt,
        user: 
            type: UserType,
            resolve(parent, args)
                //returns the user from the database that created the workout instance
                return User.findById(parent.userId)

            
        

    )
)




const AuthType = new GraphQLObjectType(
    name: "Authentication",
    fields: () => (
        token: type: GraphQLString,
        userId: type: GraphQLString
    )
)



const MealType = new GraphQLObjectType(
    name: "Meal",
    fields: () => (
        id: type: GraphQLID,
        calories: type: GraphQLInt,
        servings: type: GraphQLInt,
        nutrition: 
            carbohydrates: type: GraphQLInt,
            fats: type: GraphQLInt,
            protein: type: GraphQLInt
        ,
        user: 
            type: UserType,
            resolve(parent, args)
                //returns the user from the database that created the meal instance
                return User.findById(parent.userId)
            
        

    )
)

module.exports = 
    AuthType,
    WorkoutType,
    UserType,
    MealType

将被保存到 MongoDB atlas 中的 Meal 看起来像

const mealSchema = new Schema(
    name: String,
    calories: Number,
    servings: Number,
    nutrition: 
        carbohydrates: Number,
        fats: Number,
        protein: Number
    ,
    userId: String
)

现在,餐食的变异和查询在名为 mealQueries.js 的文件中定义

const graphql = require("graphql")
const MealType = require("./schema")
const Meal = require("../models/Meal.js")
const GraphQLObjectType, GraphQLID, GraphQLString, GraphQLSchema, GraphQLInt, GraphQLList = graphql;

const MealQuery = new GraphQLObjectType(
    name: "MealQueries",
    fields: () => (
        meal: 
            type: MealType,
            args: id: type: GraphQLID,
            resolve(parent, args)
                return Meal.findById(args.id)
            
        ,

        meals: 
            type: new GraphQLList(MealType),
            resolve(parent, args)
                return Meal.find()
            
        

    )

)

const MealMutation = new GraphQLObjectType(
    name: "MealMutation",
    addMeal: 
        type: MealType,
        args: 
            name: type: GraphQLString,
            servings: type: GraphQLInt,
            calories: type: GraphQLInt,
            nutrition: 
                carbohydrates: type: GraphQLInt,
                proteins: type: GraphQLInt,
                fats: type: GraphQLInt
            ,
            userId: type: GraphQLID
        ,
        resolve(parent, args)

            let meal = new Meal(
                userId: args.userId,
                name: args.name,
                servings: args.servings,
                calories: args.calories,
                nutrition: 
                    carbohydrates: args.nutrition.carbohydrates,
                    fats: args.nutrition.fats,
                    proteins: args.nutrition.proteins
                
            )

            return meal.save();
        
    

)

module.exports = new GraphQLSchema(
    query: MealQuery,
    mutation: MealMutation
)

【问题讨论】:

【参考方案1】:

你不能像这样嵌套字段:

nutrition: 
  carbohydrates: type: GraphQLInt,
  fats: type: GraphQLInt,
  protein: type: GraphQLInt
,

您需要创建一个单独的类型。如果您不需要在架构中的其他地方引用它,则可以内联:

nutrition: new GraphQLObjectType(
  name: 'Nutrition',
  fields: () => (
    carbohydrates:  type: GraphQLInt ,
    fats:  type: GraphQLInt ,
    protein:  type: GraphQLInt ,
  ),
),

【讨论】:

感谢您的建议,这对我来说很有意义,我确实进行了更改,但是现在,我在类型 \"Query\" 上收到无法查询字段 \"query\" 的错误 这与您的原始问题无关。这听起来像是您在客户端编写查询的方式的问题。

以上是关于Meal.nutrition 的类型必须是输出类型,但得到:未定义 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

带有快速错误的 GraphQL:Query.example 字段类型必须是输出类型,但得到:[object Object]

Edge.node 字段类型必须是输出类型但得到:未定义

为啥我的 Graphql 项目抛出错误:字段类型必须是输出类型但得到:未定义

带有 Keystone 的 GraphQL 需要字段类型必须是输出类型,但得到:未定义

错误:Note.user 字段类型必须是输出类型,但得到:[object Object]

节点 - GraphQL - Ad.user 字段类型必须是输出类型,但得到:未定义