gpt4 book ai didi

graphql-js - 使用graphql时出现意外的

转载 作者:行者123 更新时间:2023-12-03 16:25:09 24 4
gpt4 key购买 nike

每次在同一行上都出现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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com