Skip to content

Commit

Permalink
code review from @trevor-scheer
Browse files Browse the repository at this point in the history
  • Loading branch information
glasser committed Oct 31, 2022
1 parent 0d3a5ea commit f0e9a9c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 54 deletions.
2 changes: 1 addition & 1 deletion docs/source/api/apollo-server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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/).

</td>
</tr>
Expand Down
37 changes: 16 additions & 21 deletions packages/server/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ export class ApolloServer<in out TContext extends BaseContext = BaseContext> {

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
Expand All @@ -821,32 +821,27 @@ export class ApolloServer<in out TContext extends BaseContext = BaseContext> {
>();
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.`,
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/internalPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export interface InternalApolloServerPlugin<TContext extends BaseContext>
extends ApolloServerPlugin<TContext> {
// 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
Expand Down
8 changes: 2 additions & 6 deletions packages/server/src/plugin/cacheControl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions packages/server/src/plugin/disabled/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ import type {

function disabledPlugin(id: InternalPluginId): ApolloServerPlugin {
const plugin: InternalApolloServerPlugin<BaseContext> = {
__internal_plugin_id__() {
return id;
},
__is_disabled_plugin__() {
return true;
},
__internal_plugin_id__: id,
__is_disabled_plugin__: true,
};
return plugin;
}
Expand Down
8 changes: 2 additions & 6 deletions packages/server/src/plugin/inlineTrace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions packages/server/src/plugin/schemaReporting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 2 additions & 6 deletions packages/server/src/plugin/usageReporting/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ export function ApolloServerPluginUsageReporting<TContext extends BaseContext>(
requestContext: GraphQLRequestContext<TContext>,
) => GraphQLRequestListener<TContext>;
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
Expand Down

0 comments on commit f0e9a9c

Please sign in to comment.