-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Introduce "cache-control" plugin and switch req. pipeline t…
…o it. Similar to 6009d8a, which migrated the tracing package to the plugin API, this commit introduces a `plugin` (named) export from the `apollo-cache-control` module. It similarly accomplishes that by duplicating the behavior of the `CacheControlExtension` which leveraged the soon-to-be-deprecated `graphql-extensions`. The functionality, again, is intended to be identical in spirit. Since the delta of the commits was otherwise even _more_ confusing, I've left the `CacheControlExtension` present and exported and will remove it in a subsequent commit. Briefly summarizing what the necessary changes were: 1. We no longer use a class instance to house the extension, which was necessitated by the `graphql-extensions` API. This means that uses of `this` have been replaced with function scoped variables by the same name. 2. The logic which actually does the formatting (previously handled by the `format` method in `graphql-extension`, now takes place within the plugin API's `willSendResponse` method. 3. Rather than leaning on plugin-specific behavior from within the request pipeline, the `apollo-cache-control` plugin now makes a number of assertions throughout its various life-cycle methods to ensure that the `overallCachePolicy` is calculated. 4. Switch tests to use the new `pluginTestHarness` method for testing plugins which was introduced in eec87a6 (#3990).
- Loading branch information
Showing
9 changed files
with
353 additions
and
124 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 18 additions & 15 deletions
33
packages/apollo-cache-control/src/__tests__/collectCacheControlHints.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,41 @@ | ||
import { GraphQLSchema, graphql } from 'graphql'; | ||
|
||
import { | ||
enableGraphQLExtensions, | ||
GraphQLExtensionStack, | ||
} from 'graphql-extensions'; | ||
import { | ||
CacheControlExtension, | ||
CacheHint, | ||
CacheControlExtensionOptions, | ||
plugin, | ||
} from '../'; | ||
import pluginTestHarness from 'apollo-server-core/dist/utils/pluginTestHarness'; | ||
|
||
export async function collectCacheControlHints( | ||
schema: GraphQLSchema, | ||
source: string, | ||
options?: CacheControlExtensionOptions, | ||
): Promise<CacheHint[]> { | ||
enableGraphQLExtensions(schema); | ||
|
||
// Because this test helper looks at the formatted extensions, we always want | ||
// to include them. | ||
const cacheControlExtension = new CacheControlExtension({ | ||
// to include them in the response rather than allow them to be stripped | ||
// out. | ||
const pluginInstance = plugin({ | ||
...options, | ||
stripFormattedExtensions: false, | ||
}); | ||
|
||
const response = await graphql({ | ||
const requestContext = await pluginTestHarness({ | ||
pluginInstance, | ||
schema, | ||
source, | ||
contextValue: { | ||
_extensionStack: new GraphQLExtensionStack([cacheControlExtension]), | ||
graphqlRequest: { | ||
query: source, | ||
}, | ||
executor: async (requestContext) => { | ||
return await graphql({ | ||
schema, | ||
source: requestContext.request.query, | ||
contextValue: requestContext.context, | ||
}); | ||
} | ||
}); | ||
|
||
expect(response.errors).toBeUndefined(); | ||
expect(requestContext.response.errors).toBeUndefined(); | ||
|
||
return cacheControlExtension.format()[1].hints; | ||
return requestContext.response.extensions!.cacheControl.hints; | ||
} |
Oops, something went wrong.