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

useAsyncSchema does not await the schema promise #1110

Closed
thomasheyenbrock opened this issue Dec 15, 2021 · 1 comment
Closed

useAsyncSchema does not await the schema promise #1110

thomasheyenbrock opened this issue Dec 15, 2021 · 1 comment
Assignees
Labels
documentation Improvements or additions to documentation kind/discussion Improvements or additions to documentation
Milestone

Comments

@thomasheyenbrock
Copy link

Describe the bug

The useAsyncSchema accepts a promise that resolves to a GraphQL schema as argument. It chains a .then() on this promise (inside the call to setSchema happens), but since the envelop initialisation is synchronous, it might happen that the GraphQL server is handling the first request before the schema promise has resolved. (Or rejected, which is also not handled btw 🤔 )

To Reproduce
Steps to reproduce the behavior:

  • This codesandbox demonstrates the issue by throwing in an artificial setTimeout of one second.

Expected behavior

  1. When loading an async schema, no request should be handled before the schema is loaded
  2. Rejection of the schema promise is handled gracefully

Environment:

  • OS: macOS Monterey 12.0.1
  • @envelop/core: latest
  • NodeJS: 16.13.0

Additional context

I looked a bit into the codebase, here are some loose thoughts around how to achieve the first point I noted under "expected behavior":

  • Making the onPluginInit hook async would easily solve this, but it would be a major change (atm the plugin initialisation and the call to envelop is sync). "Remembering" promises returned from that hook and awaiting them when calling getEnveloped would also be a breaking change.
  • Maybe this could potentially even break other plugins that call setSchema, because it's non-deterministic in which order these calls will happen?
  • I also found this comment to be kinda related, but awaiting the schema to be loaded after calling envelop defeats the purpose of useAsyncSchema (you could just await before and use the sync useSchema)
  • Maybe this can also be "solved" by more extensive documentation, i.e. "you need to make sure the schema promise is resolved before starting to handle request.

Would be open to help with this, but right now I'm not what the way forward would be 😄

@n1ru4l n1ru4l added documentation Improvements or additions to documentation kind/discussion Improvements or additions to documentation labels Dec 16, 2021
@saihaj saihaj added this to the v3 milestone Sep 5, 2022
@saihaj saihaj self-assigned this Sep 5, 2022
@saihaj
Copy link
Collaborator

saihaj commented Sep 5, 2022

This was a mistake to add since validate and parse from graphql-js as synchronous in nature. Dropping this plugin in next major release #1506

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation kind/discussion Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants