From b7e046004cb153bfcb15592c050671843022205d Mon Sep 17 00:00:00 2001 From: David Glasser Date: Thu, 17 Jan 2019 15:43:18 -0500 Subject: [PATCH] checkpoint for full query cache --- package-lock.json | 7 ++ package.json | 1 + .../src/ApolloServerPluginFullQueryCache.ts | 64 ++++++++++++++++++- tsconfig.build.json | 1 + tsconfig.test.json | 1 + 5 files changed, 71 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index ad5a0aaf..05bee430 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2296,6 +2296,13 @@ "apollo-server-plugin-base": { "version": "file:packages/apollo-server-plugin-base" }, + "apollo-server-plugin-full-query-cache": { + "version": "file:packages/apollo-server-plugin-full-query-cache", + "requires": { + "apollo-server-caching": "file:packages/apollo-server-caching", + "apollo-server-plugin-base": "file:packages/apollo-server-plugin-base" + } + }, "apollo-server-testing": { "version": "file:packages/apollo-server-testing", "requires": { diff --git a/package.json b/package.json index ecc5aaa4..c20cd840 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "apollo-server-lambda": "file:packages/apollo-server-lambda", "apollo-server-micro": "file:packages/apollo-server-micro", "apollo-server-plugin-base": "file:packages/apollo-server-plugin-base", + "apollo-server-plugin-full-query-cache": "file:packages/apollo-server-plugin-full-query-cache", "apollo-server-testing": "file:packages/apollo-server-testing", "apollo-tracing": "file:packages/apollo-tracing", "graphql-extensions": "file:packages/graphql-extensions" diff --git a/packages/apollo-server-plugin-full-query-cache/src/ApolloServerPluginFullQueryCache.ts b/packages/apollo-server-plugin-full-query-cache/src/ApolloServerPluginFullQueryCache.ts index 690e4a9c..a559a60c 100644 --- a/packages/apollo-server-plugin-full-query-cache/src/ApolloServerPluginFullQueryCache.ts +++ b/packages/apollo-server-plugin-full-query-cache/src/ApolloServerPluginFullQueryCache.ts @@ -2,19 +2,77 @@ import { ApolloServerPlugin, GraphQLRequestListener, + GraphQLRequestContext, } from 'apollo-server-plugin-base'; // import { ApolloError } from 'apollo-server-errors'; import { KeyValueCache } from 'apollo-server-caching'; -interface Options {} +// XXX Duplicates non-exported type from apollo-server-plugin-base. +type ValueOrPromise = T | Promise; + +interface Options> { + // Underlying cache used to save results. All writes will be under keys that + // start with 'fqc:' and are followed by a fixed-size cryptographic hash of a + // JSON object with keys representing the query document, operation name, + // variables, and other keys derived from the sessionId and extraCacheKeyData + // hooks. If not provided, use the cache in the GraphQLRequestContext instead + // (ie, the cache passed to the ApolloServer constructor). + cache?: KeyValueCache; + + // Define this hook if you're setting any cache hints with scope PRIVATE. + // This should return a session ID if the user is "logged in", or null if + // there is no "logged in" user. + // + // If a cachable response has any PRIVATE nodes, then: + // - If this hook is not defined, a warning will be logged. + // - Else if this hook returns null, it will not be cached. + // - Else it will be cached under a cache key tagged with the session ID and + // mode "private". + // + // If a cachable response has no PRIVATE nodes, then: + // - If this hook is not defined or returns null, it will be cached under a cache + // key tagged with the mode "no session". + // - Else it will be cached under a cache key tagged with the mode + // "authenticated public". + // + // When reading from the cache: + // - If this hook is not defined or returns null, look in the cache under a cache + // key tagged with the mode "no session". + // - Else look in the cache under a cache key tagged with the session ID and the + // mode "private". If no response is found in the cache, then look under a cache + // key tagged with the mode "authenticated public". + // + // This allows the cache to provide different "public" results to anonymous + // users and logged in users ("no session" vs "authenticated public"). + // + // A common implementation of this hook would be to look in + // requestContext.request.http.headers for a specific authentication header or + // cookie. + // + // This hook may return a promise because, for example, you might need to + // validate a cookie against an external service. + sessionId?( + requestContext: GraphQLRequestContext, + ): ValueOrPromise; + + // Define this hook if you want the cache key to vary based on some aspect of + // the request other than the query document, operation name, variables, and + // session ID. For example, responses that include translatable text may want + // to return a string derived from + // requestContext.request.http.headers.get('Accept-Language'). + extraCacheKeyData?( + requestContext: GraphQLRequestContext, + ): ValueOrPromise; +} export default function plugin(options: Options = Object.create(null)) { - let cache: KeyValueCache; + // let cache: KeyValueCache; + console.log(options); return (): ApolloServerPlugin => ({ requestDidStart(): GraphQLRequestListener { return { - async didResolveOperation({ request: { query }, queryHash }) {}, + async didResolveOperation({}) {}, }; }, }); diff --git a/tsconfig.build.json b/tsconfig.build.json index 24a42b86..30fbff88 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -24,6 +24,7 @@ { "path": "./packages/apollo-server-lambda" }, { "path": "./packages/apollo-server-micro" }, { "path": "./packages/apollo-server-plugin-base" }, + { "path": "./packages/apollo-server-plugin-full-query-cache" }, { "path": "./packages/apollo-server-testing" }, { "path": "./packages/apollo-tracing" }, { "path": "./packages/graphql-extensions" }, diff --git a/tsconfig.test.json b/tsconfig.test.json index 1d906e03..8fe32354 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -20,5 +20,6 @@ { "path": "./packages/apollo-server-koa/src/__tests__/" }, { "path": "./packages/apollo-server-lambda/src/__tests__/" }, { "path": "./packages/apollo-server-micro/src/__tests__/" }, + { "path": "./packages/apollo-server-plugin-full-query-cache/src/__tests__/" }, ] }