Graphql-code-generator: SyntaxError: 'return' outside of function

Created on 22 Apr 2020  ·  9Comments  ·  Source: dotansimha/graphql-code-generator

Describe the bug
After regenerating package-lock started getting this error

> graphql-codegen --config codegen.yml

  ✔ Parse configuration
  ❯ Generate outputs
    ❯ Generate ./src/types/generated/graphql.ts
      ✔ Load GraphQL schemas
      ✖ Load GraphQL documents
        → 'return' outside of function (13:4)
        Generate


 Found 1 error

  ✖ ./src/types/generated/graphql.ts
    SyntaxError: 'return' outside of function (13:4)
        at Object._raise (/Users/path-to-project/node_modules/@babel/parser/lib/index.js:742:17)
        at Object.raiseWithData (/Users/path-to-project/node_modules/@babel/parser/lib/index.js:735:17)

To Reproduce
Steps to reproduce the behavior:

I'm using graphql-code-generator with Gatsby

overwrite: true
schema: "http://localhost:8000/___graphql"
documents:
  - ./src/**/*.{ts,tsx}
  - ./node_modules/gatsby-*/**/*.js
generates:
  ./src/types/generated/graphql.ts:
    hooks:
      afterOneFileWrite:
        - prettier --write
    plugins:
      - typescript
      - typescript-operations
config:
  namingConvention:
    enumValues: keep
  avoidOptionals: true
  immutableTypes: true
  maybeValue: T

Environment:

System:
OS: macOS Mojave 10.14
CPU: (4) x64 Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
Shell: 5.3 - /bin/zsh
Binaries:
Node: 12.14.1 - /usr/local/bin/node
Yarn: 1.21.1 - /usr/local/bin/yarn
npm: 6.13.4 - /usr/local/bin/npm
Languages:
Python: 2.7.10 - /usr/bin/python
Browsers:
Chrome: 81.0.4044.113
Firefox: 72.0.2
Safari: 12.0
npmPackages:
gatsby: 2.20.23
@graphql-codegen/cli: 1.13.2
@graphql-codegen/introspection: 1.13.2
@graphql-codegen/typescript: 1.13.2
@graphql-codegen/typescript-operations: 1.13.2

bug

Most helpful comment

Ok, actually replacing this line with officially recommended
- ./node_modules/gatsby*/!(node_modules)/**/*.js fixes the issue.

All 9 comments

Could you share a reproduction repo or CodeSandbox?

I'd love to, but not quite sure how to do it

l can try to run clean Gatsby project with latest gatsby and graphql-code-generator. Let's see if it results in error.

Reproduction https://github.com/smashercosmo/graphql-code-generator-issue

  1. run npm run develop
  2. then in another terminal npm run graphql2ts

@smashercosmo ./src/**/*.{ts,tsx} is too wide so every single file is getting parsed to find a valid GraphQL operation, so I recommend you to narrow it down a little bit.

@ardatan I removed almost all the tsx files. But I think the error is caused by gatsby files.
If I remove this line https://github.com/smashercosmo/graphql-code-generator-issue/blob/master/codegen.yml#L5 error goes away

Ok, actually replacing this line with officially recommended
- ./node_modules/gatsby*/!(node_modules)/**/*.js fixes the issue.

So we can close the issue, right?

Yes, thx

Was this page helpful?
0 / 5 - 0 ratings