Graphql-code-generator: Syntax Error: Unexpected Name "declare"

Created on 11 Jun 2019  ·  6Comments  ·  Source: dotansimha/graphql-code-generator

Describe the bug

graphql-codegen --config codegen.yml

  ✔ Parse configuration
  ❯ Generate outputs
    ❯ Generate src/generated/graphql.ts
      ✔ Load GraphQL schemas
      ✖ Load GraphQL documents
        → Syntax Error: Unexpected Name "declare"
        Generate
    ❯ Generate ./graphql.schema.json
      ✔ Load GraphQL schemas
      ✖ Load GraphQL documents
        → Syntax Error: Unexpected Name "declare"
        Generate


 Found 2 errors

  ✖ node_modules/graphql-toolkit/types/asyncro.d.graphql
    Syntax Error: Unexpected Name "declare"

    node_modules/graphql-toolkit/types/asyncro.d.graphql (1:1)
    1: declare module 'asyncro' {
       ^
    2:   type AsyncFn<T> = () => PromiseLike<T>;

  ✖ node_modules/graphql-toolkit/types/asyncro.d.graphql
    Syntax Error: Unexpected Name "declare"

    node_modules/graphql-toolkit/types/asyncro.d.graphql (1:1)
    1: declare module 'asyncro' {
       ^
    2:   type AsyncFn<T> = () => PromiseLike<T>;

To Reproduce
Steps to reproduce the behavior:

  1. My GraphQL schema:
    happens with any schema/service

  2. My GraphQL operations:
    happens with any operations

  3. My codegen.yml config file:

overwrite: true
schema: "http://localhost:4000"
documents: "**/*.graphql"
generates:
  src/generated/graphql.ts:
    plugins:
      - "typescript-react-apollo"
  ./graphql.schema.json:
    plugins:
      - "introspection"

Expected behavior
Successful script run.

Environment:

  • OS:
  • @graphql-codegen/...:
  • NodeJS: tried v12 and v10

Additional context

bug core waiting-for-release

Most helpful comment

You are trying to parse a Typescript module definition as a GraphQL document.

Are you sure you really want to parse all .graphql files in node_modules? If not, you can either specify your .graphql files explicitly, put them in a subfolder and parse only that folder, or exclude node_modules with:

documents: "{,!(node_modules)/**/}*.graphql"

All 6 comments

Have the same problem with v1.2.1, but v1.2.0 works

You are trying to parse a Typescript module definition as a GraphQL document.

Are you sure you really want to parse all .graphql files in node_modules? If not, you can either specify your .graphql files explicitly, put them in a subfolder and parse only that folder, or exclude node_modules with:

documents: "{,!(node_modules)/**/}*.graphql"

@pnordhus Thanks, that works (seeing a different error now though). This yml was given to me by the init function so I expected sensible defaults in an empty CRA app... 🤔

Same here—encountered the same issue also using a file generated by the wizard. Error didn’t make it clear it was a misconfiguration on my part

The init suggests to use **/*.graphql, but the default should be src/**/*.graphql to avoid including node_modules as default.
Updated it in: https://github.com/dotansimha/graphql-code-generator/pull/2012

Fixed in 1.3.0 🎉

Was this page helpful?
0 / 5 - 0 ratings