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

Dynamic schema generation #423

Closed
joemckie opened this issue Sep 18, 2019 · 7 comments
Closed

Dynamic schema generation #423

joemckie opened this issue Sep 18, 2019 · 7 comments
Labels
Community 👨‍👧 Something initiated by a community Question ❔ Not future request, proposal or bug issue Solved ✔️ The issue has been solved

Comments

@joemckie
Copy link

joemckie commented Sep 18, 2019

It would be great to be able to completely dynamically generate the schema. I am currently building a framework that allows users to add plugins which will add or remove parts of the schema, so I will never know what the schema will be at any given time. I have read through #110 and tested out the beta branch published with #415, however the feature set still seems limiting.

Currently (as of the beta version), the resolvers must be manually maintained as the type is [Function, ...Function[]]. This doesn't allow for the resolvers array to be generated using .map() or some other method. Also, once the resolver has been imported anywhere in the codebase, it automatically gets picked up by buildSchema and added to the final schema. I have managed to get around that final part by dynamically importing activated resolvers, but it only works one way, i.e. adding it to the schema works but removing it does not.

I'm more than happy to submit a PR for this myself if it's trivial?

@MichalLytek
Copy link
Owner

This doesn't allow for the resolvers array to be generated using .map() or some other method.

If you are sure that the .map() doesn't return an empty array, just make an assertion as NonEmptyArray<ClassType> for the TS compiler happiness 😉

Also, once the resolver has been imported anywhere in the codebase, it automatically gets picked up by buildSchema and added to the final schema.

It should only apply to the "resolvers glob path" feature as I don't have any control even about which files will be required, so no array of class to compare and filter resolvers.

I'm more than happy to submit a PR for this myself if it's trivial?

It's not as trivial, please discuss that first 😉

@MichalLytek MichalLytek added Community 👨‍👧 Something initiated by a community Discussion 💬 Brainstorm about the idea Enhancement 🆕 New feature or request labels Sep 18, 2019
@joemckie
Copy link
Author

If you are sure that the .map() doesn't return an empty array, just make an assertion as NonEmptyArray for the TS compiler happiness 😉

Good idea

It should only apply to the "resolvers glob path" feature as I don't have any control even about which files will be required, so no array of class to compare and filter resolvers.

I'm using an array of classes. The array is being updated (i.e. resolver removed), but the schema stays the same

@MichalLytek
Copy link
Owner

The array is being updated

Show me how and when you are "updating" the array - when it's after the schema is built, it's too late 😕

@joemckie
Copy link
Author

https:/esunajs/esuna/blob/tgql-demo/packages/core/src/app.ts

Brief overview of what's happening:

  1. A mutation hits deactivatePlugin. This simply flips the active bool from true to false.
  2. deactivatePlugin calls updateSchema, which rebuilds the schema (based on the new set of plugins). This is where I can see the array being changed, as it is being passed into the resolvers property of buildSchema.
  3. An event is emitted which is picked up by Apollo's gateway, which applies the new schema to the server.

All of the logic for activating and deactivating seems to work on my end, but the generated schema still pulls in irrelevant resolvers

@MichalLytek
Copy link
Owner

but the generated schema still pulls in irrelevant resolvers

This shouldn't happen, I can create two apollo server with different resolvers array and have no schema overlap.

This is where I can see the array being changed, as it is being passed into the resolvers property of buildSchema.

Looks like you have a problem in your code - please log carefully the mapped resolvers array, maybe you are doing it too soon, some asynchronous goes into this, etc.

@joemckie
Copy link
Author

🤦‍♂️I'm an idiot. This was an error on my part

@joemckie
Copy link
Author

Thanks for the help!

@MichalLytek MichalLytek added Question ❔ Not future request, proposal or bug issue Solved ✔️ The issue has been solved and removed Discussion 💬 Brainstorm about the idea Enhancement 🆕 New feature or request labels Sep 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Question ❔ Not future request, proposal or bug issue Solved ✔️ The issue has been solved
Projects
None yet
Development

No branches or pull requests

2 participants