graphql中未定义类型的输出错误
Posted
技术标签:
【中文标题】graphql中未定义类型的输出错误【英文标题】:undefined type of output error in graphql 【发布时间】:2019-04-25 09:53:34 【问题描述】:我用 mongodb 在 graphql 服务器和 nodejs 中编写了一个简单的代码。我有一个包含 2 个集合的数据库。明星和电影。当我运行此查询以显示所有电影时,我的 GraphQL NodeJS 服务器出现以下错误: “MovieType.year 的类型必须是 Output Type 但得到:未定义。”
这是我的架构:
const
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLNonNull,
GraphQLList
= require("graphql");
const Movie, User = require('/home/fateme/imdb_final/db')
const starType = new GraphQLObjectType(
name: "UserType",
fields:
name:
type: GraphQLString,
async resolve(objId)
const starFind = await User.findOne( _id: objId._id)
return starFind.name
,
imdbId:
type: GraphQLString,
async resolve(objId)
const starFind = await User.findOne( _id: objId._id)
return starFind.imdbId
);
const movieType = new GraphQLObjectType(
name: "MovieType",
fields:
title:
type: GraphQLString,
async resolve(objId)
const movieFind = await Movie.findOne( _id: objId._id)
return movieFind.title
,
year:
type: GraphQLInt,
async resolve(objId)
const movieFind = await Movie.findOne( _id: objId._id)
return movieFind.year
,
rate:
type: GraphQLInt,
async resolve(objId)
const movieFind = await Movie.findOne( _id: objId._id)
return movieFind.rating
,
year:
rate: GraphQLInt,
async resolve(objId)
const movieFind = await Movie.findOne( _id: objId._id)
return movieFind.year
,
director:
type: GraphQLString,
async resolve(objId)
const movieFind = await Movie.findOne( _id: objId._id)
return movieFind.director
,
);
const queryType = new GraphQLObjectType(
name: 'Query',
fields: () => (
users:
type: GraphQLList(starType),
async resolve()
const allStar = await User.find()
return allStar
,
movies:
type: GraphQLList(movieType),
async resolve()
const allMovie = await Movie.find()
console.log(allMovie)
return allMovie
,
)
);
const schema = new GraphQLSchema(
query: queryType
);
module.exports = schema;
【问题讨论】:
【参考方案1】:如错误所示,您尚未为 year
字段定义类型。相反,您不小心添加了 rate
字段:
year:
rate: GraphQLInt, // <-- here
async resolve(objId)
const movieFind = await Movie.findOne( _id: objId._id)
return movieFind.year
,
只需将 rate
更改为 type
即可:)
【讨论】:
以上是关于graphql中未定义类型的输出错误的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 Graphql 项目抛出错误:字段类型必须是输出类型但得到:未定义
节点 - GraphQL - Ad.user 字段类型必须是输出类型,但得到:未定义