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

fix(): support multiple GraphQLModule in an app #1432

Closed
wants to merge 1 commit into from
Closed

fix(): support multiple GraphQLModule in an app #1432

wants to merge 1 commit into from

Conversation

wtho
Copy link

@wtho wtho commented Mar 10, 2021

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[X] Bugfix

What is the current behavior?

Currently, when wanting to use multiple GraphQLModules (different schemas for separate endpoints) in combination with the code-first approach, there will be an error such as Error: Schema must contain uniquely named types but contains multiple types named "User"..

Issue Number: #721

What is the new behavior?

Now it works.
During GraphQLModule initialization, basically all storages are cleared and the codebase is re-scanned from scratch including re-evaluation of annotations. Only the TypeDefinitionsStorage is an exception, which keeps references to outdated objects using the maps InputTypeDefinitionsLinks and outputTypeDefinitionsLinks.

With the new behavior, these storages are cleared as well in the GraphQLSchemaFactory during GraphQLModule initialization.

Does this PR introduce a breaking change?

[ ] Yes
[X] No

Other information

I opted to keep code changes minimal while fixing the bug. This means I mixed static method calls with intance method calls in the GraphQLSchemaFactory and extended an existing test to mimic the problem. If you want me to add an additional test or structure the code differently, just let me know.
Same goes for a Changelog or docs, so far I haven't touched any.

Closes #721

@kamilmysliwiec
Copy link
Member

Unfortunately, this is just a workaround that won't make it possible to have multiple separate apps with the code first approach. Although it may let you start your application, the final result won't be correct.

The only way to solve this issue (multiple separate endpoints) is to introduce a new feature that lets you associate types (e.g., object types) with NestJS modules, for example, using the following construction @ObjectType({ registerIn: CatsModule }). In this case, types wouldn't be global anymore and we could store them per app instead of a per process, but I don't quite busy atm and I won't have enough time to implement that. I hope to look into that in the near future, but I cannot promise anything.

@wtho wtho deleted the fix/support-multiple-graphqlmodules branch March 17, 2021 18:09
@wtho
Copy link
Author

wtho commented Mar 17, 2021

I see.

Personally I don't mind global types, there are use cases where one might want to have the same types as in another module and just one additional mutation. I think just assigning each type to one module only might solve only a part of the problem.

Why does each module have to trigger a rescan on the whole codebase? Can't we just reuse the storages? This would also reduce startup time for these use cases.

@nick4fake
Copy link

nick4fake commented Mar 20, 2021

@kamilmysliwiec it still fully covers case that @wtho has mentioned. Looking on the original issue thread (#721) almost every person wants exactly this. Could you please merge this PR? While approach that you want to implement in future is not covered it still closes very annoying blocking bug.

@nestjs nestjs locked as off-topic and limited conversation to collaborators Mar 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GraphQl Multiple Endpoints not working
3 participants