Browse Source

Pass schema into api extension definition (#3261)

Philipp Sonntag 7 months ago
parent
commit
a60fd70ac4

+ 1 - 1
packages/core/src/api/config/get-final-vendure-schema.ts

@@ -124,7 +124,7 @@ function extendSchemaWithPluginApiExtensions(
     apiType: 'admin' | 'shop',
 ) {
     getPluginAPIExtensions(plugins, apiType)
-        .map(e => (typeof e.schema === 'function' ? e.schema() : e.schema))
+        .map(e => (typeof e.schema === 'function' ? e.schema(schema) : e.schema))
         .filter(notNullOrUndefined)
         .forEach(documentNode => (schema = extendSchema(schema, documentNode)));
     return schema;

+ 4 - 3
packages/core/src/plugin/vendure-plugin.ts

@@ -1,10 +1,10 @@
-import { Module, Provider, Type as NestType } from '@nestjs/common';
+import { Module, Type as NestType, Provider } from '@nestjs/common';
 import { MODULE_METADATA } from '@nestjs/common/constants';
 import { ModuleMetadata } from '@nestjs/common/interfaces';
 import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
 import { pick } from '@vendure/common/lib/pick';
 import { Type } from '@vendure/common/lib/shared-types';
-import { DocumentNode, GraphQLScalarType } from 'graphql';
+import { DocumentNode, GraphQLScalarType, GraphQLSchema } from 'graphql';
 
 import { RuntimeVendureConfig } from '../config/vendure-config';
 
@@ -81,6 +81,7 @@ export interface APIExtensionDefinition {
     /**
      * @description
      * Extensions to the schema.
+     * Passes the current schema as an optional argument, allowing the extension to be based on the existing schema.
      *
      * @example
      * ```ts
@@ -90,7 +91,7 @@ export interface APIExtensionDefinition {
      * }`;
      * ```
      */
-    schema?: DocumentNode | (() => DocumentNode | undefined);
+    schema?: DocumentNode | ((schema?: GraphQLSchema) => DocumentNode | undefined);
     /**
      * @description
      * An array of resolvers for the schema extensions. Should be defined as [Nestjs GraphQL resolver](https://docs.nestjs.com/graphql/resolvers-map)