syntax-errorgraphqlapolloreact-apollographql-tag

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:

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

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


Solution

  • 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.

    This works:

    import gql from 'graphql-tag'
    
    const query = gql`
    {
      user(id: 5) {
        firstName
        lastName
      }
    }
    `
    
    export default query