GraphQL gql 语法错误:预期名称,已找到 } [英] GraphQL gql Syntax Error: Expected Name, found }

查看:28
本文介绍了GraphQL gql 语法错误:预期名称,已找到 }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在新的 React 项目中设置 Apollo GraphQL 支持,但是当我尝试使用 gql 编译查询时,我不断收到错误消息:

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:

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

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)

我将此代码基于此处找到的示例代码:https://github.com/apollographql/graphql-tag

I'm basing this code off the example code found here: https://github.com/apollographql/graphql-tag

错误消息中引用的 Name 是什么?有谁知道我在这里做错了什么?

What is the Name referred to in the error message? Does anyone know what I'm doing wrong here?

推荐答案

我不是 100% 确定我的问题的根源是什么,但是将所有查询代码移动到一个单独的 es6 模块中解决了这个问题.周围的代码一定有某种污染.作为参考,我的查询嵌入在 React 组件中.

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. There must have been some kind of contamination from the surrounding code. For reference my query was embedded within a React component.

这有效:

import gql from 'graphql-tag'

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

export default query

这篇关于GraphQL gql 语法错误:预期名称,已找到 }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆