【问题标题】:Unexpected <EOF> while using graphql使用 graphql 时出现意外的 <EOF>
【发布时间】:2018-11-22 10:46:52
【问题描述】:

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

【问题讨论】:

    标签: graphql-js express-graphql


    【解决方案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。

    【讨论】:

      【解决方案2】:

      我在 schema.graphql 文件中有 cmets:

      """
      Some comments
      """
      

      我删除了 cmets,Unexpected &lt;EOF&gt; 错误消失了。

      【讨论】:

        【解决方案3】:

        这是因为没有实际的查询,所以你得到了意外的 EOF(文件结束)。

        猜测您正在使用 GraphiQL(因为您的 EOF 消息显示第 30 行);您需要在浏览器的 GraphiQL 左侧面板上添加查询。符合您的RootQuery 的内容,例如:

        {
          user(id: "1") {
            id,
            firstName,
            age
          }
        }
        

        【讨论】:

          【解决方案4】:

          我在尝试拼接我的架构时遇到了这个错误 这个错误的原因是我定义了一个类型但没有实现它,有空的身体,例如:

          type User {
          
          }
          

          实现类型修复它

          【讨论】:

          • 你在哪里添加的?
          【解决方案5】:

          看起来我们正在使用模式优先的方法,但我们的项目/空文件中没有任何 .graphql 文件。我添加了这个并且错误消失了:

          src/example.graphql

          type Query {
            hello: String
          }
          

          “GraphQLError:语法错误:Unexpected”错误来自 graphql 包。看起来@nestjs/graphql 假设总会有至少一个 .graphql 文件,所以这可能是一个错误,尝试解析空架构与显示对开发人员更友好的消息或忽略它。

          【讨论】:

            【解决方案6】:

            在大多数情况下,传递空查询会导致此错误。 为避免这种情况,请在您的 graphiql 中尝试此操作

            query { 
               user {
                 fields you need to retrieve
              }
            }
            

            【讨论】:

              【解决方案7】:

              这是因为您的graphql.gql 看起来像这样:

              # ------------------------------------------------------
              # THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
              # ------------------------------------------------------
              
              directive @key(fields: String!) on OBJECT | INTERFACE
              
              directive @extends on OBJECT | INTERFACE
              
              directive @external on OBJECT | FIELD_DEFINITION
              
              directive @requires(fields: String!) on FIELD_DEFINITION
              
              directive @provides(fields: String!) on FIELD_DEFINITION
              
              

              您可以做的一件事是创建一个虚拟解析器。

              // src/graphql/resolvers/user.resolver.ts
              
              import { Resolver, Query } from '@nestjs/graphql';
              
              import { User } from 'models/User.model';
              
              @Resolver(() => User)
              export class UserResolver {
                @Query(() => User)
                async ids() {
                  return [];
                }
              }
              

              并创建一个 graphql 模型:

              // src/graphql/models
              
              import { Field, ObjectType } from '@nestjs/graphql';
              
              @ObjectType()
              export class User {
                @Field()
                createdAt: Date;
              }
              
              

              并确保解析器由模块导入,并将模块添加到 App.module

              这应该可以解决它!

              【讨论】:

                猜你喜欢
                • 2016-07-05
                • 2019-04-28
                • 2013-04-25
                • 2021-03-21
                • 2018-02-02
                • 2022-07-08
                • 2011-07-15
                相关资源
                最近更新 更多