Type-graphql: Dynamic schema generation

Created on 18 Sep 2019  路  7Comments  路  Source: MichalLytek/type-graphql

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?

Community Question Solved

All 7 comments

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 :wink:

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 馃槈

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

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 馃槙

https://github.com/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

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.

馃う鈥嶁檪锔廔'm an idiot. This was an error on my part

Thanks for the help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maplesteve picture maplesteve  路  3Comments

glentakahashi picture glentakahashi  路  3Comments

memark picture memark  路  3Comments

tafelito picture tafelito  路  3Comments

itsgracian picture itsgracian  路  3Comments