Skip to content

Commit

Permalink
Add deprecation warnings for GraphQLExtension usage.
Browse files Browse the repository at this point in the history
This introduces a deprecation warning which will be emitted once per
extension which is defined in the `extensions` parameter of an Apollo Server
configuration.

With the introduction of the last missing integration point
(`willResolveField`) via the recently landed
#3988, the new [[Plugin
API]] should now have all of the abilities (a super-set, in fact) of the
prior `graphql-extensions` API.

Furthermore, rather than being undocumented, rather untested and largely
experimental, the new plugin API is considered stable and well-understood in
terms of what it aims to support.  Its documentation and API are now
considered part of the Apollo Server API and we will do our best to maintain
first-class support for it, including the addition of new functionality as
deemed necessary.

As of this commit, we will now print deprecation warnings one time per
server start-up for _each_ legacy extension.  Since extensions were often
defined as factory functions which were invoked on each request - and
expected to return an instance of the extension by calling `new` on it - we
limit this deprecation warning to once per start-up by attaching a `Symbol`
to the `constructor` and skipping the warning when the `Symbol` is already
present.

An alternative design might use a `Map` to track the `constructor`s at the
module-level within `requestPipeline.ts`, but I believe this should be
functionally the same.

[Plugin API]: https://www.apollographql.com/docs/apollo-server/integrations/plugins/
  • Loading branch information
abernix committed May 18, 2020
1 parent ba58cc4 commit 9e3a46a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/apollo-server-core/src/__tests__/runQuery.test.ts
Expand Up @@ -399,6 +399,27 @@ describe('runQuery', () => {
}
}

it('warns that they are deprecated', async () => {
const queryString = `{ testString }`;
const logger = {
warn: jest.fn(console.warn),
info: console.info,
debug: console.debug,
error: console.error,
};
const extensions = [() => new CustomExtension()];
await runQuery({
schema,
queryString,
extensions,
request: new MockReq(),
}, {
logger,
});
expect(logger.warn).toHaveBeenCalledWith(
expect.stringMatching(/^\[deprecated\] A "CustomExtension" was/));
});

it('creates the extension stack', async () => {
const queryString = `{ testString }`;
const extensions = [() => new CustomExtension()];
Expand Down
41 changes: 41 additions & 0 deletions packages/apollo-server-core/src/requestPipeline.ts
Expand Up @@ -113,6 +113,14 @@ export type DataSources<TContext> = {

type Mutable<T> = { -readonly [P in keyof T]: T[P] };

/**
* We attach this symbol to the constructor of extensions to mark that we've
* already warned about the deprecation of the `graphql-extensions` API for that
* particular definition.
*/
const symbolExtensionDeprecationDone =
Symbol("apolloServerExtensionDeprecationDone");

export async function processGraphQLRequest<TContext>(
config: GraphQLRequestPipelineConfig<TContext>,
requestContext: Mutable<GraphQLRequestContext<TContext>>,
Expand Down Expand Up @@ -634,6 +642,39 @@ export async function processGraphQLRequest<TContext>(
// objects.
const extensions = config.extensions ? config.extensions.map(f => f()) : [];

// Warn about usage of (deprecated) `graphql-extensions` implementations.
// Since extensions are often provided as factory functions which
// instantiate an extension on each request, we'll attach a symbol to the
// constructor after we've warned to ensure that we don't do it on each
// request. Another option here might be to keep a `Map` of constructor
// instances within this module, but I hope this will do the trick.
const hasOwn = Object.prototype.hasOwnProperty;
extensions.forEach((extension) => {
// Using `hasOwn` just in case there is a user-land `hasOwnProperty`
// defined on the `constructor` object.
if (hasOwn.call(extension.constructor, symbolExtensionDeprecationDone)) {
return;
}

Object.defineProperty(
extension.constructor,
symbolExtensionDeprecationDone,
{ value: true }
);

const extensionName = extension.constructor.name;
logger.warn(
'[deprecated] ' +
('A "' + extensionName + '" ' || 'An anonymous extension ') +
'was defined within the "extensions" configuration for ' +
'Apollo Server. The API on which this extension is built ' +
'("graphql-extensions") is being deprecated in the next major ' +
'version of Apollo Server in favor of the new plugin API. See ' +
'https://go.apollo.dev/s/plugins for the documentation on how ' +
'these plugins are to be defined and used.',
);
});

return new GraphQLExtensionStack(extensions);
}

Expand Down

0 comments on commit 9e3a46a

Please sign in to comment.