简体   繁体   English

GraphQL gql 语法错误:预期名称,已找到 }

[英]GraphQL gql Syntax Error: Expected Name, found }

I'm attempting to set up Apollo GraphQL support in a new React project, but when I try to compile a query using gql I keep receiving the error:我正在尝试在新的 React 项目中设置 Apollo GraphQL 支持,但是当我尝试使用gql编译查询时,我不断收到错误消息:

Syntax Error: Expected Name, found }语法错误:预期名称,已找到 }

This is generated by the following code:这是由以下代码生成的:

import gql from 'graphql-tag'

const query = gql`
    {
      user(id: 5) {
        firstName
        lastName
      }
    }
  `

console.log(query)

I'm basing this code off the example code found here: https://github.com/apollographql/graphql-tag我将此代码基于此处找到的示例代码: https://github.com/apollographql/graphql-tag

What is the Name referred to in the error message?错误消息中提到的Name是什么? Does anyone know what I'm doing wrong here?有谁知道我在这里做错了什么?

此错误主要发生在有未闭合的大括号或调用查询时未正确定义某些字段时。

The accepted answer didn't solve my issue.接受的答案并没有解决我的问题。 Instead, it worked if you remove the initial curly brackets.相反,如果您删除最初的大括号,它会起作用。

The query should look like this instead:查询应如下所示:

const query=gql`
  user(id: 5) {
    firstName
    lastName
  }
`

The causes could be:原因可能是:

  • you are adding a "()" at the beginning for no reason您无缘无故地在开头添加“()”
  • you need to add more 'nested' parameters.您需要添加更多“嵌套”参数。

Especially if you are using an online GraphiQL editor.特别是如果您使用的是在线 GraphiQL 编辑器。 Examples:例子:

1- Wrong code (extra parenthesis) 1- 错误代码(额外括号)

{
  allFilms() {
    films {
      title
    }
  }
}

2- Wrong code (more parameters need it eg: title) 2- 错误代码(更多参数需要它,例如:标题)

{
  allFilms {
    films {         
    }
  }
}

3- Correct code 3-正确的代码

{
  allFilms {
    films {
     title         
    }
  }
}

GraphQLError: Syntax Error: Expected Name, found "$". GraphQLError:语法错误:预期名称,找到“$”。

One more example of a similar error (For other users).类似错误的另一个示例(对于其他用户)。

theErrorIsHere (Could be extra ( or { before the $varName ) added before $speakerId theErrorIsHere (可能是额外的(或在$varName之前的{ )在$speakerId speakerId 之前添加

Error code:错误代码:

const FEATURED_SPEAKER = gql`
  mutation markFeatured($speakerId: ID!, $featured: Boolean!){
    markFeatured(speaker_id: theErrorIsHere$speakerId , featured: $featured){
      id
      featured
    }
  }
`;

错误输出

Correct code:正确代码:

const FEATURED_SPEAKER = gql`
  mutation markFeatured($speakerId: ID!, $featured: Boolean!){
    markFeatured(speaker_id: $speakerId , featured: $featured){
      id
      featured
    }
  }
`;

I'm not 100% sure what the root of my problem was, but moving all the query code into a separate es6 module fixed the issue.我不是 100% 确定我的问题的根源是什么,但是将所有查询代码移动到一个单独的 es6 模块中解决了这个问题。 There must have been some kind of contamination from the surrounding code.周围的代码一定有某种污染。 For reference my query was embedded within a React component.作为参考,我的查询嵌入在 React 组件中。

This works:这有效:

import gql from 'graphql-tag'

const query = gql`
{
  user(id: 5) {
    firstName
    lastName
  }
}
`

export default query

Another cause for this error: you are referencing a type that is defined further down.导致此错误的另一个原因:您正在引用进一步定义的类型。 Move the type you are referencing up.向上移动要引用的类型。

For example:例如:

    type Launch {
        rocket: Rocket
    }

    type Rocket {
        name: String
    }

will throw an error, as Launch references Rocket before Rocket is defined.将抛出错误,因为Launch在定义Rocket Rocket

The corrected code:更正后的代码:

    type Rocket {
        name: String
    }

    type Launch {
        rocket: Rocket
    }

In NestJS framework, this error happened to me because I defiled GraphQL field in my schema.graphql file as:在 NestJS 框架中,这个错误发生在我身上,因为我将 schema.graphql 文件中的 GraphQL 字段污染为:

  lastUpdated(): Date

Instead it should be just相反,它应该只是

  lastUpdated: Date

(it doesn't take any argument) (它不需要任何论据)

I was receiving a similar error server side:我收到了类似的错误服务器端:

GraphQLError: Syntax Error: Expected Name, found ]

I realized the cause in my case was a type definition with an empty array.我意识到我的原因是一个空数组的类型定义。

This breaks:这打破了:

  type Settings {
    requires: []
  }

But this works:但这有效:

  type Settings {
    requires: [String]
  }

我遇到了这个问题,原因是双引号内带有双引号的字符串值,例如: "this "is" bad"

In my case I got the error because of the following:就我而言,由于以下原因,我收到了错误:

const GET_POSTS_OF_AUTHOR = gql`
  query GetPostsOfAuthor($authorId: Int!) {
    postsOf($authorId: Int!) {
      id
      title
    }
  }
`;

When it should have been:什么时候应该是:

const GET_POSTS_OF_AUTHOR = gql`
  query GetPostsOfAuthor($authorId: Int!) {
    postsOf(authorId: $authorId) {
      id
      title
    }
  }
`;

erroneously thought $authorId passed through identically to the function call instead of setting a property inside the function call.错误地认为 $authorId 与函数调用相同地传递,而不是在函数调用中设置属性。

This can happen if you use gql from @cl.net/apollo and in the backticks you try to inject dynamic js value.如果您使用来自@cl.net/apollo 的 gql 并在反引号中尝试注入动态 js 值,就会发生这种情况。 Remove it and replace with normal scalar and it will fix your issue.删除它并替换为普通标量,它将解决您的问题。 example:例子:

${SOME_MAX_VALUE} -> 20

On ny side the error was caused by extra {} Curly braces.在任何一方,错误是由额外的 {} 花括号引起的。 Solved by just removing them.只需删除它们即可解决。

In my case, I got the error simply because I'm adding : which I shouldn't have done.就我而言,我收到错误只是因为我添加了:我不应该这样做。

eg:例如:

const query = `
   query($id: String!) {
      getUser(id: $id) {
        user: {
          id
          name
          email
          createdAt
        }
      }
   }
`

If you pay close attention to line 4 of the code above you'll realize that I added : after the user before the curly brace, then I began to list the user's data I wanna query and THAT WAS EXACTLY WHERE THE ERROR WAS!如果您密切注意上面代码的第 4 行,您会发现我在大括号之前的用户之后添加了:然后我开始列出我要查询的用户数据,而这正是错误所在! Removing the : solve the issue!删除:解决问题!

It should be:它应该是:

user {
   id
   name
   ...
}

I was getting the same error.我遇到了同样的错误。 In my case putting the id inside double quote solved the issue as the type of id required string value.在我的情况下,将 id 放在双引号内解决了问题,因为 id 的类型需要字符串值。

 { product(id: "${id}") { name } }

Posting here in case anyone else had this problem but you also get this error if you accidentally make your query look like json with colons ( : ).如果其他人遇到此问题,请在此处发布,但如果您不小心使查询看起来像带有冒号 ( :的 json,您也会收到此错误。

ex:前任:

data {
  property {
    key: {
      deepKey
    }
  }
}

will give the same error from GQL compile将从 GQL 编译中给出相同的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM