Use main cache for persisted queries by default

This commit is contained in:
Martijn Walraven 2018-07-09 19:24:37 -07:00
parent f8feca870a
commit 9e60f42845
4 changed files with 7 additions and 23 deletions

View file

@ -42,9 +42,7 @@
"graphql-tag": "^2.9.2", "graphql-tag": "^2.9.2",
"graphql-tools": "^3.0.4", "graphql-tools": "^3.0.4",
"hash.js": "^1.1.3", "hash.js": "^1.1.3",
"keyv": "^3.0.0",
"lodash": "^4.17.10", "lodash": "^4.17.10",
"quick-lru": "^1.1.0",
"subscriptions-transport-ws": "^0.9.11", "subscriptions-transport-ws": "^0.9.11",
"ws": "^5.2.0" "ws": "^5.2.0"
}, },

View file

@ -21,10 +21,6 @@ import {
ExecutionParams, ExecutionParams,
} from 'subscriptions-transport-ws'; } from 'subscriptions-transport-ws';
// use as default persisted query store
import Keyv = require('keyv');
import QuickLru = require('quick-lru');
import { formatApolloErrors } from 'apollo-server-errors'; import { formatApolloErrors } from 'apollo-server-errors';
import { import {
GraphQLServerOptions as GraphQLOptions, GraphQLServerOptions as GraphQLOptions,
@ -42,7 +38,6 @@ import {
import { FormatErrorExtension } from './formatters'; import { FormatErrorExtension } from './formatters';
import { gql } from './index'; import { gql } from './index';
import { PersistedQueryCache } from './caching';
const NoIntrospection = (context: ValidationContext) => ({ const NoIntrospection = (context: ValidationContext) => ({
Field(node: FieldDefinitionNode) { Field(node: FieldDefinitionNode) {
@ -115,14 +110,14 @@ export class ApolloServerBase {
: noIntro; : noIntro;
} }
if (!requestOptions.cache) {
requestOptions.cache = new InMemoryLRUCache();
}
if (requestOptions.persistedQueries !== false) { if (requestOptions.persistedQueries !== false) {
if (!requestOptions.persistedQueries) { if (!requestOptions.persistedQueries) {
// maxSize is the number of elements that can be stored inside of the cache
// https://github.com/withspectrum/spectrum has about 200 instances of gql`
// 300 queries seems reasonable
const lru = new QuickLru({ maxSize: 300 });
requestOptions.persistedQueries = { requestOptions.persistedQueries = {
cache: new Keyv({ store: lru }) as PersistedQueryCache, cache: requestOptions.cache!,
}; };
} }
} else { } else {
@ -130,10 +125,6 @@ export class ApolloServerBase {
delete requestOptions.persistedQueries; delete requestOptions.persistedQueries;
} }
if (!requestOptions.cache) {
requestOptions.cache = new InMemoryLRUCache();
}
this.requestOptions = requestOptions as GraphQLOptions; this.requestOptions = requestOptions as GraphQLOptions;
this.context = context; this.context = context;

View file

@ -1,11 +1,6 @@
import { ExecutionResult } from 'graphql'; import { ExecutionResult } from 'graphql';
import { CacheControlFormat } from 'apollo-cache-control'; import { CacheControlFormat } from 'apollo-cache-control';
export interface PersistedQueryCache {
set(key: string, data: string): Promise<any>;
get(key: string): Promise<string | null>;
}
export type HttpHeaderCalculation = ( export type HttpHeaderCalculation = (
responses: Array<ExecutionResult & { extensions?: Record<string, any> }>, responses: Array<ExecutionResult & { extensions?: Record<string, any> }>,
) => Record<string, string>; ) => Record<string, string>;

View file

@ -3,7 +3,7 @@ import {
ValidationContext, ValidationContext,
GraphQLFieldResolver, GraphQLFieldResolver,
} from 'graphql'; } from 'graphql';
import { PersistedQueryCache, HttpHeaderCalculation } from './caching'; import { HttpHeaderCalculation } from './caching';
import { GraphQLExtension } from 'graphql-extensions'; import { GraphQLExtension } from 'graphql-extensions';
import { CacheControlExtensionOptions } from 'apollo-cache-control'; import { CacheControlExtensionOptions } from 'apollo-cache-control';
import { KeyValueCache } from 'apollo-server-caching'; import { KeyValueCache } from 'apollo-server-caching';
@ -59,7 +59,7 @@ export type DataSources<TContext> = {
}; };
export interface PersistedQueryOptions { export interface PersistedQueryOptions {
cache: PersistedQueryCache; cache: KeyValueCache;
} }
export default GraphQLServerOptions; export default GraphQLServerOptions;