diff --git a/docs/source/api/apollo-server.mdx b/docs/source/api/apollo-server.mdx index eafdb0c5b8f..53e6d5d2d03 100644 --- a/docs/source/api/apollo-server.mdx +++ b/docs/source/api/apollo-server.mdx @@ -374,7 +374,7 @@ An array of [plugins](../integrations/plugins) to install in your server instanc You can _also_ add plugins to your server before you start it using the [`addPlugin`](#addplugin) method. -Apollo Server comes with several plugins that it installs automatically in their default configuration if certain conditions are met. For example, the usage reporting plugin is installed if you provide a graph API key and a graph ref. Apollo Server skips this automatic installation if manually provide the plugin (in the `plugins` array or with the `addPlugin` method), or if you provide the plugin's corresponding "disabled" plugin (such as `ApolloServerPluginUsageReportingDisabled()` for `ApolloServerPluginUsageReporting`). For details, see the API references for these plugins: [usage reporting](./plugin/usage-reporting/), [schema reporting](./plugin/schema-reporting/), [landing page](./plugin/landing-pages/), [cache control](./plugin/cache-control/), and [inline trace](./plugin/inline-trace/). +Apollo Server comes with several plugins that it installs automatically in their default configuration if certain conditions are met. For example, the usage reporting plugin is installed if you provide a graph API key and a graph ref. Apollo Server skips this automatic installation if you manually provide the plugin (in the `plugins` array or with the `addPlugin` method), or if you provide the plugin's corresponding "disabled" plugin (such as `ApolloServerPluginUsageReportingDisabled()` for `ApolloServerPluginUsageReporting`). For details, see the API references for these plugins: [usage reporting](./plugin/usage-reporting/), [schema reporting](./plugin/schema-reporting/), [landing page](./plugin/landing-pages/), [cache control](./plugin/cache-control/), and [inline trace](./plugin/inline-trace/). diff --git a/packages/server/src/ApolloServer.ts b/packages/server/src/ApolloServer.ts index f831a13338b..6d93a1e836f 100644 --- a/packages/server/src/ApolloServer.ts +++ b/packages/server/src/ApolloServer.ts @@ -804,7 +804,7 @@ export class ApolloServer { const alreadyHavePluginWithInternalId = (id: InternalPluginId) => plugins.some( - (p) => pluginIsInternal(p) && p.__internal_plugin_id__() === id, + (p) => pluginIsInternal(p) && p.__internal_plugin_id__ === id, ); // Make sure we're not trying to explicitly enable and disable the same @@ -821,32 +821,27 @@ export class ApolloServer { >(); for (const p of plugins) { if (pluginIsInternal(p)) { - if (!pluginsByInternalID.has(p.__internal_plugin_id__())) { - pluginsByInternalID.set(p.__internal_plugin_id__(), { + if (!pluginsByInternalID.has(p.__internal_plugin_id__)) { + pluginsByInternalID.set(p.__internal_plugin_id__, { sawDisabled: false, sawNonDisabled: false, }); } - if (p.__is_disabled_plugin__()) { - pluginsByInternalID.get(p.__internal_plugin_id__())!.sawDisabled = - true; + const seen = pluginsByInternalID.get(p.__internal_plugin_id__)!; + if (p.__is_disabled_plugin__) { + seen.sawDisabled = true; } else { - pluginsByInternalID.get(p.__internal_plugin_id__())!.sawNonDisabled = - true; + seen.sawNonDisabled = true; + } + + if (seen.sawDisabled && seen.sawNonDisabled) { + throw new Error( + `You have tried to install both ApolloServerPlugin${id} and ` + + `ApolloServerPlugin${id}Disabled in your server. Please choose ` + + `whether or not you want to disable the feature and install the ` + + `appropriate plugin for your use case.`, + ); } - } - } - for (const [ - id, - { sawDisabled, sawNonDisabled }, - ] of pluginsByInternalID.entries()) { - if (sawDisabled && sawNonDisabled) { - throw new Error( - `You have tried to install both ApolloServerPlugin${id} and ` + - `ApolloServerPlugin${id}Disabled in your server. Please choose ` + - `whether or not you want to disable the feature and install the ` + - `appropriate plugin for your use case.`, - ); } } diff --git a/packages/server/src/internalPlugin.ts b/packages/server/src/internalPlugin.ts index 3ad2a824739..206f6a32671 100644 --- a/packages/server/src/internalPlugin.ts +++ b/packages/server/src/internalPlugin.ts @@ -11,8 +11,8 @@ export interface InternalApolloServerPlugin extends ApolloServerPlugin { // Used to identify a few specific plugins that are instantiated // by default if not explicitly used or disabled. - __internal_plugin_id__(): InternalPluginId; - __is_disabled_plugin__(): boolean; + __internal_plugin_id__: InternalPluginId; + __is_disabled_plugin__: boolean; } // Helper function for writing internal plugins which lets you write an object diff --git a/packages/server/src/plugin/cacheControl/index.ts b/packages/server/src/plugin/cacheControl/index.ts index 9711510250d..8462e66f573 100644 --- a/packages/server/src/plugin/cacheControl/index.ts +++ b/packages/server/src/plugin/cacheControl/index.ts @@ -63,12 +63,8 @@ export function ApolloServerPluginCacheControl( >; return internalPlugin({ - __internal_plugin_id__() { - return 'CacheControl'; - }, - __is_disabled_plugin__() { - return false; - }, + __internal_plugin_id__: 'CacheControl', + __is_disabled_plugin__: false, async serverWillStart({ schema }) { // Set the size of the caches to be equal to the number of composite types diff --git a/packages/server/src/plugin/disabled/index.ts b/packages/server/src/plugin/disabled/index.ts index e507365d694..b427fef6d90 100644 --- a/packages/server/src/plugin/disabled/index.ts +++ b/packages/server/src/plugin/disabled/index.ts @@ -14,12 +14,8 @@ import type { function disabledPlugin(id: InternalPluginId): ApolloServerPlugin { const plugin: InternalApolloServerPlugin = { - __internal_plugin_id__() { - return id; - }, - __is_disabled_plugin__() { - return true; - }, + __internal_plugin_id__: id, + __is_disabled_plugin__: true, }; return plugin; } diff --git a/packages/server/src/plugin/inlineTrace/index.ts b/packages/server/src/plugin/inlineTrace/index.ts index 393ea790ac3..fa2de0215b8 100644 --- a/packages/server/src/plugin/inlineTrace/index.ts +++ b/packages/server/src/plugin/inlineTrace/index.ts @@ -49,12 +49,8 @@ export function ApolloServerPluginInlineTrace( ): ApolloServerPlugin { let enabled: boolean | null = options.__onlyIfSchemaIsFederated ? null : true; return internalPlugin({ - __internal_plugin_id__() { - return 'InlineTrace'; - }, - __is_disabled_plugin__() { - return false; - }, + __internal_plugin_id__: 'InlineTrace', + __is_disabled_plugin__: false, async serverWillStart({ schema, logger }) { // Handle the case that the plugin was implicitly installed. We only want it // to actually be active if the schema appears to be federated. If you don't diff --git a/packages/server/src/plugin/schemaReporting/index.ts b/packages/server/src/plugin/schemaReporting/index.ts index 44d5d88da98..3ff24e86ccd 100644 --- a/packages/server/src/plugin/schemaReporting/index.ts +++ b/packages/server/src/plugin/schemaReporting/index.ts @@ -69,12 +69,8 @@ export function ApolloServerPluginSchemaReporting( const bootId = uuidv4(); return internalPlugin({ - __internal_plugin_id__() { - return 'SchemaReporting'; - }, - __is_disabled_plugin__() { - return false; - }, + __internal_plugin_id__: 'SchemaReporting', + __is_disabled_plugin__: false, async serverWillStart({ apollo, schema, logger }) { const { key, graphRef } = apollo; if (!key) { diff --git a/packages/server/src/plugin/usageReporting/plugin.ts b/packages/server/src/plugin/usageReporting/plugin.ts index 65e0e9ed593..02c4962635a 100644 --- a/packages/server/src/plugin/usageReporting/plugin.ts +++ b/packages/server/src/plugin/usageReporting/plugin.ts @@ -70,12 +70,8 @@ export function ApolloServerPluginUsageReporting( requestContext: GraphQLRequestContext, ) => GraphQLRequestListener; return internalPlugin({ - __internal_plugin_id__() { - return 'UsageReporting'; - }, - __is_disabled_plugin__() { - return false; - }, + __internal_plugin_id__: 'UsageReporting', + __is_disabled_plugin__: false, // We want to be able to access locals from `serverWillStart` in our `requestDidStart`, thus // this little hack. (Perhaps we should also allow GraphQLServerListener to contain