vendure-ecommerce/vendure

Pass current schema into `APIExtensionDefinition.schema` and `APIExtensionDefinition.resolvers`

Opened this issue · 0 comments

Is your feature request related to a problem? Please describe.

I am creating multiple Vendure Plugins for a private project. I want to make sure that a type is declared only once, e.g. CustomerSocialMedias and then only extended subsequent various plugins.

Example
3 Plugins are used within a Vendure instance. GooglePlusPlugin, TwitterPlugin and MastodonPlugin. Each one of those plugins will extend the CustomerSocialMedias type with own fields, but exactly one has to create the type initially so it can be extended. Duplicate declarations will result in an error in my tests. This makes it more complicated.

Describe the solution you'd like

The solution would be to provide the current schema as an argument when calling the APIExtensionDefinition Callbacks of each plugin. I think this would be extremely helpful in the future too with minimal to no impact at all. All existing plugins that use this will continue to work as JS will just call the existing methods without the argument.

export interface APIExtensionDefinition {
    schema?: DocumentNode | (() => DocumentNode | undefined) | ((schema: GraphQLSchema) => DocumentNode | undefined);

    resolvers?: Array<Type<any>> | (() => Array<Type<any>>) | ((schema: GraphQLSchema) => Array<Type<any>>);

    // ... rest of interface
}

This allows the Plugin to check if the schema already declared a type and just extend it if necessary.

Describe alternatives you've considered

Apollo Plugin
I was not able to extend the schema using Apollo Plugins. It seems that this is not possible as the schema is already defined when plugins are loaded.

"Declaration Plugin"
A plugin that declares all necessary types so other plugins can extend them. This approach will work, but the plugin itself has to be changed every time another plugins are used. This also goes against the thought that each plugin should be a module that works for itself.