如何解决错误 GraphQL“Query.GetUser 的类型必须是输出类型但得到:未定义”
在编写我的第一个 Graphql 时遇到了这个问题。
数据未显示
index.js:
lazy val module = new MapModule{}
lazy val mapper = JsonMapper.builder()
.addModule(module)
.build()
...
val map: Map[String,Any] = mapper.readValue(line,classOf[Map[String,Any]])
schema.js:
const express = require('express')
const app = express()
const graphql = require('graphql')
const {graphqlHTTP} = require('express-graphql')
const { GetUser,mutation } = require('./schema')
const { DataType } = require('./DataType')
app.use(express.json())
const schema = new graphql.GraphQLSchema({
query: new graphql.GraphQLObjectType({
name:"Query",type:new graphql.GraphQLList(DataType),fields:{GetUser}
}),mutation:new graphql.GraphQLObjectType({
name:"Mutation",fields:{mutation}
})
})
app.use('/graphql',graphqlHTTP({
schema,graphiql:true
}))
app.listen(1200,()=>{
console.log("SERVER AT 1200");
})
DataType.js:
const graphql = require('graphql')
const {GraphQLObjectType,GraphQLString} = graphql
const {DataType} = require('./DataType')
var data = [
{
"name":"Book1","price":123
},{
"name":"Book2","price":1232
},{
"name":"Book3","price":2021
},]
exports.GetUser = new GraphQLObjectType({
name:"GetAllUser",fileds:{
getAllUser:{
name:"getUser",resolve:()=>{
return data;
}
}
}
})
exports.mutation = new GraphQLObjectType({
name:"AddData",fields:{
adduser:{
name:"adduser",type:data,args:{
name:{type:GraphQLString},price:{type:graphql.GraphQLInt}
},resolve:(_,args)=>{
data.push(args)
return args
}
}
}
})
运行 graphql:
const graphql = require('graphql')
exports.DataType = new graphql.GraphQLObjectType({
name:"DataType",fields:()=>({
name:{
type: graphql.GraphQLString
},price:{
type:graphql.GraphQLInt
}
})
})
我得到的错误:
query{
GetUser{
name
}
}
错误说 Query.GetUser 的类型必须是输出类型但得到:未定义。
我再次定义了查询的类型,但仍然显示相同的变异错误。
这也是我接触 Graphql 的第一天。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。