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

Check for gql documents before running codegen #4728

Merged
merged 21 commits into from
Mar 21, 2022

Conversation

Tobbe
Copy link
Member

@Tobbe Tobbe commented Mar 12, 2022

Fixes #4393

Recap

This is the error we're printing

gen | Generating TypeScript definitions and GraphQL schemas...
gen |
gen |       Unable to find any GraphQL type definitions for the following pointers:
gen |
gen |           - ./web/src/**/!(*.d).{ts,tsx,js,jsx}
gen |
gen |
gen |       Unable to find any GraphQL type definitions for the following pointers:
gen |
gen |           - ./web/src/**/!(*.d).{ts,tsx,js,jsx}
gen |
gen |
gen | Error: Could not generate GraphQL type definitions (web)
gen |
gen | 14 files generated

The "Unable to find..." error message comes from here: https://github.com/ardatan/graphql-tools/blob/2207fbcdacf708ee9836ee72d0c0f92dba0d0942/packages/load/src/load-typedefs.ts#L120

The "Error: Could not generate..." message is our own.

Fix

This PR uses the loadDocuments() function from @graphql-tools to first try and load all gql documents. If no documents are found we don't run the codegen, and thus don't get the error messages shown above.

In a recent meeting with The Guild there was some talk about performance issues. Especially when reading from disk as we're doing here. So I did some rudimentary timing checks on this. From what I can tell this extra check adds less than 0.1s to the total runtime for yarn rw g types.

This is an alternative fix to #4491. That PR didn't do anything about the running of the codegen, it only changed the "Error: ..." message.

Before

image

After

image

@Tobbe Tobbe added the release:fix This PR is a fix label Mar 12, 2022
@Tobbe Tobbe requested a review from dac09 March 12, 2022 13:32
@Tobbe
Copy link
Member Author

Tobbe commented Mar 14, 2022

@dac09 Just for you 🙂

Check for gql documents before running codegen by Tobbe · Pull Request #4728 · redwoodjs/redwood - Vivaldi

Img

Copy link
Contributor

@dac09 dac09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting clarification and minor changes, comments in the files

@Tobbe
Copy link
Member Author

Tobbe commented Mar 15, 2022

@dac09 Thanks for your review. If I understood you correctly the biggest concern was that we might be catching too many errors when calling loadDocuments. I tried to write some tests for that. I'm not super happy with the tests because 1) I had to introduce a function param only used for tests, and 2) I'm testing for strings inside error messages, which can make the tests unnecessarily brittle. But I do at least think they cover the concerns you had.

@dac09
Copy link
Contributor

dac09 commented Mar 15, 2022

  1. I had to introduce a function param only used for tests, and But I do at least think they cover the concerns you had

Yes they do for sure. I think its worth the compromise, I'm sure you would've done it another way if possible!

  1. I'm testing for strings inside error messages, which can make the tests unnecessarily brittle

Maybe switch this to use inline snapshots? May not address the brittle problem, but will be much easier to maintain!

Something like:

console.error = jest.fn() 

expect(console.error.mock.lastCall).toMatchInlineSnapshot()

Watcha think?

@dac09
Copy link
Contributor

dac09 commented Mar 16, 2022

Another thing to check based on our conversation yesterday:

  • Can we catch the specific error for when a model is missing? (i.e. relations) when we loadSchema

@thedavidprice
Copy link
Contributor

#want this is v1 so so bad 🥺🙏

Copy link
Member Author

Tobbe commented Mar 18, 2022

I'll do my best

@thedavidprice
Copy link
Contributor

I'll do my best

Thanks @Tobbe I know you are and you will. Excited to have this far along!!

@Tobbe Tobbe force-pushed the tobbe-codegen-check-for-files branch from 1586f6b to c572e87 Compare March 20, 2022 09:28
@netlify
Copy link

netlify bot commented Mar 20, 2022

✅ Deploy Preview for redwoodjs-docs ready!

🔨 Explore the source changes: 793dd10

🔍 Inspect the deploy log: https://app.netlify.com/sites/redwoodjs-docs/deploys/6238f40eb47b6400096c1109

😎 Browse the preview: https://deploy-preview-4728--redwoodjs-docs.netlify.app

@Tobbe
Copy link
Member Author

Tobbe commented Mar 20, 2022

Another thing to check based on our conversation yesterday:

  • Can we catch the specific error for when a model is missing? (i.e. relations) when we loadSchema

This would be inside generateGraphQLSchema. I didn't plan to touch that file in this PR

@Tobbe
Copy link
Member Author

Tobbe commented Mar 20, 2022

I couldn't figure out how to fix this test error:
image
Why is it generating snapshots inside /dist? And why is this working locally, but not here?

Anyway, I stoped using snapshot tests (for now). If anyone knows how to make them work, please let me know. Otherwise I'm good with just leaving the tests as they are now.

@Tobbe Tobbe requested a review from dac09 March 20, 2022 17:09
@Tobbe
Copy link
Member Author

Tobbe commented Mar 20, 2022

@dac09 Ready for another review

  • I moved all the graphql codegen stuff out of typeDefinitions.ts into graphqlCodeGen.ts. Same for tests.
  • It now uses codegen from @graphql-codegen/core instead of generate from @graphql-codegen/cli
  • I've only done this for graphql documents. I have not touched graphqlSchema.ts. I could switch that one over as well, but would prefer to do so in a separate PR.
  • I'm not using any snapshot tests. For some reason jest was putting snapshots in both src/__tests__/__snapshots__ and dist/__tests__/__snapshots__ (and some other test files too in /dist). This made the testcases fail on Github.
  • Users can supply their own codegen.yml that will be merged with RW's internal codegen config. That file is mainly for codegen/cli. Because of that the users don't have as much control over the codegen process. Only root level config is merged. I think I can improve this a little bit if need, but likely not make it as powerful as it was previously. The original testcase that was written for this still passes.

await generateGraphQLSchema()

const webPaths = await generateTypeDefGraphQLWeb()
const gqlTypesWebOutput = fs.readFileSync(webPaths[0], 'utf-8')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion. what do you think of mocking fs.writeFileSync here instead of using the index here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this idea. Saves on disk I/O for the test case which is always a good idea.

Copy link
Contributor

@dac09 dac09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! Thank you @Tobbe for bearing with me.

Looks like it should be possible to catch the "unknown type" error too, which is awesome. And no problem doing this in another PR

Left a few small comments that I would appreciate if you addressed

}
})

test("Doesn't swallow legit errors - missingType", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could:
a) Move this into a separate describe block
b) Before each, delete all generated .d.ts files, to make sure the old ts files in the fixture aren't picked up
c) Make sure the generate .d.ts isn't commited in the missingType fixture

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a) done
b) I've (manually) deleted all .d.ts files. No "old" files exist
c) No files are generated as part of the tests, because the generation bails on the (expected) errors. If, for some reason, files starts being generated hopefully that's a red flag for the developer so he/she doesn't just blindly commit them...

@thedavidprice thedavidprice merged commit 41aa2ab into redwoodjs:main Mar 21, 2022
@jtoar jtoar added this to the next-release milestone Mar 21, 2022
@Tobbe Tobbe deleted the tobbe-codegen-check-for-files branch March 22, 2022 06:15
dac09 added a commit to dac09/redwood that referenced this pull request Mar 22, 2022
…postprocessing-nested-api-functions

* 'main' of github.com:redwoodjs/redwood:
  Stying updates on docs site
  fix(deps): update typescript-eslint monorepo to v5.16.0 (redwoodjs#4862)
  fix(deps): update dependency cross-undici-fetch to v0.1.27 (redwoodjs#4858)
  fix(deps): update dependency @graphql-yoga/common to v0.1.0-canary-bfd2627.0 (redwoodjs#4857)
  chore(deps): update dependency zx to v6.0.7 (redwoodjs#4861)
  (docs) add Flightcontrol.dev Deploy  (redwoodjs#4826)
  Handle case when requestContext is undefined getting protocol (redwoodjs#4864)
  Check for gql documents before running codegen (redwoodjs#4728)
  change flightcontrol setup to use yarn v3 and change cookie config (redwoodjs#4843)
  Forward -> Foreward
  Forward -> Foreward
  Typo
  Reorgnize tutorial into chapters (redwoodjs#4855)
  Fix linting warnings on new gql function (redwoodjs#4859)
  Use GraphQL Yoga (redwoodjs#4712)
  Fix url for tutorial path (redwoodjs#4852)
  chore(deps): update actions/cache action to v3 (redwoodjs#4847)
  Update link to tutorial deployment
  try learn's algolia config (redwoodjs#4851)
@thedavidprice thedavidprice modified the milestones: next-release, v0.50.0 Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix This PR is a fix
Projects
No open projects
Status: Archived
Development

Successfully merging this pull request may close these issues.

Errors on initial yarn rw dev
4 participants