Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow performance with large schemas #2105

Closed
mxstbr opened this issue Jul 4, 2019 · 9 comments
Closed

Slow performance with large schemas #2105

mxstbr opened this issue Jul 4, 2019 · 9 comments
Labels
core Related to codegen core/cli

Comments

@mxstbr
Copy link
Contributor

mxstbr commented Jul 4, 2019

Describe the bug

When generating code from large GraphQL schemas, running graphql codegen takes > 25s, which is way too slow.

To Reproduce

Generate schema types and some queries from the official GitHub GraphQL API, which is very large:

overwrite: true
schema:
  - http://api.github.com/graphql:
      headers:
        Authorization: Bearer ${GITHUB_PERSONAL_ACCESS_TOKEN}
documents: "src/**/*.graphql"
generates:
  src/graphql-schema-types.generated.ts:
    plugins:
      - typescript
    config:
      immutableTypes: false
  src/:
    preset: near-operation-file
    presetConfig:
      baseTypesPath: "graphql-schema-types.generated.ts"
      extension: ".generated.tsx"
    plugins:
      - add: |
          // eslint-disable
          // ⚠️ DO NOT EDIT ⚠️
          // This file is automatically generated, run yarn run graphql:codegen to update
      - "typescript-operations"
      - "typescript-urql"
    config:
      immutableTypes: false
      withComponent: false
      withHooks: true
  web/src/graphql-types.generated.d.ts:
      plugins:
      - add: |
          // eslint-disable
          // ⚠️ DO NOT EDIT ⚠️
          // This file is automatically generated, run yarn run graphql:codegen to update
      - "typescript-graphql-files-modules"

You are going to need to get a GitHub personal access token from your settings and insert . that where it says ${GITHUB_PERSONAL_ACCESS_TOKEN}. A potential example query could be something like

{
  viewer {
    id
  }
}

Expected behavior

Graphql codegen's performance should be related to how many queries/mutations/subscriptions it needs to generate code for, not how large the input schema is.

@dotansimha
Copy link
Owner

@mxstbr I think the issue here is related to near-operation-file. Does it work better with a single file?

@mxstbr
Copy link
Contributor Author

mxstbr commented Jul 4, 2019

You are right! Same configuration except for removing near-operation-file takes 11s. I only generate 12 files with the preset, it seems weird that going from writing one file to writing 12 files would add ~1s per file?!

11s is still too slow though, if I replace the large schema with a much smaller schema it only takes 1s, so there's still a lot of seemingly unnecessary overhead there?

@dotansimha
Copy link
Owner

I think it's related to the filtering we are doing before executing the plugin (per each file). I'll check :)

@dotansimha dotansimha added core Related to codegen core/cli enhancement labels Jul 6, 2019
@dotansimha
Copy link
Owner

dotansimha commented Jul 7, 2019

btw, @mxstbr , this PR by @mvestergaard is merged: #2073 (and available as 1.3.1-alpha-991b9d78.75).
It should avoid building the schema per each output file, so I think it might improve the performance, can you please try it?

@mxstbr
Copy link
Contributor Author

mxstbr commented Jul 8, 2019

On alpha.82, running graphql-codegen in my codebase takes ~20s (with prettify: false) compared to ~24s on latest, so it's a bit faster but not much. 😕

For reference, the schema I am working with is almost 100k LoC and the file is 1.7MB in size.

@mvestergaard
Copy link
Contributor

That's a little odd. For me there was a big improvement:

With 1.3.1:

$ gql-gen
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Parse configuration
  √ Generate outputs
Done in 10.11s.

With @canary

$ gql-gen
  √ Parse configuration
  √ Generate outputs
Done in 3.87s.

You upgraded all packages related to graphql-codegen to the alpha, i assume?

Maybe you can try running a profiler to see where the slowness comes from.
I did the following

It'll output a flamegraph html file to a folder in your project

@mvestergaard
Copy link
Contributor

Oh and with prettify: false:

$ gql-gen
  √ Parse configuration
  √ Generate outputs
Done in 2.27s.

@mxstbr
Copy link
Contributor Author

mxstbr commented Jul 8, 2019

You are right, I must have done something wrong with my last upgrades—using the latest @canary, We are down to 7.27s with prettify: false! 🎉 🎉 🎉

Still not super fast but acceptable.

@dotansimha
Copy link
Owner

Closing this for now, since @mvestergaard changes it works much better :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to codegen core/cli
Projects
None yet
Development

No branches or pull requests

3 participants