每次在同一行上都出现EOF错误,多次更改代码,甚至降级到graphql
的早期版本,但没有任何积极结果。
我的代码是:
const graphql = require('graphql')
const _ = require('lodash')
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema
} = graphql
const users = [
{id: '1', firstName: 'Ansh', age: 20},
{id: '2', firstName: 'Ram', age: 21},
{id: '3', firstName: 'Sham', age: 20}
]
const UserType = new GraphQLObjectType({
name: 'User',
fields: {
id: {type: GraphQLString},
firstName: {type: GraphQLString},
age: {type: GraphQLInt}
}
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: {id: {type: GraphQLString}},
resolve(parentValue, args) {
return _.find(users, {id: args.id})
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery
})
错误是:
{
"errors": [
{
"message": "Syntax Error GraphQL request (30:1) Unexpected <EOF>\n\n29: \n30: \n ^\n",
"locations": [
{
"line": 30,
"column": 1
}
]
}
]
}
最佳答案
问题是因为您传递的查询可能为空。
例如:
curl -X POST http://localhost:4000/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ user { id } }"}'
工作良好。
但是,如果您进行以下操作:
curl -X POST http://localhost:4000/graphql \
-H "Content-Type: application/json" \
-d '{"query": ""}'
您会得到意想不到的
另外,检查GraphQL end of line issue。
关于graphql-js - 使用graphql时出现意外的<EOF>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50827295/