Javascript 膳食的类型。营养必须是输出类型,但Get:未定义

Javascript 膳食的类型。营养必须是输出类型,但Get:未定义,javascript,node.js,mongodb,express,graphql,Javascript,Node.js,Mongodb,Express,Graphql,我在使用GraphQL、Express.js和MongoDB设置服务器时遇到了这个问题。这似乎是某种参考错误,我不确定问题是什么,我试图寻找我的答案,但似乎找不到 我在GraphiQL上遇到的错误是,我不知道为什么 { "errors": [ { "message": "The type of Meal.nutrition must be Output Type but got: undefined." } ] } 无论如何,在app.js上 代码是 con

我在使用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上的食物看起来像

const mealSchema = new Schema({
    name: String,
    calories: Number,
    servings: Number,
    nutrition: {
        carbohydrates: Number,
        fats: Number,
        protein: Number
    },
    userId: String
})
现在,在一个名为mealquerys.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
})

不能嵌套如下字段:

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 },
  }),
}),

不能嵌套如下字段:

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”。这听起来像是您在客户端编写查询的方式存在问题。感谢您的建议,这对我来说很有意义,我确实做了更改,但是现在,我在类型“query”上遇到了与您的原始问题无关的“Cannot query”字段“query”。这听起来像是在客户端编写查询的方式的问题。