Run Prettier on my TS file
VS Code wasn't running it automatically; now we've got it set up!
This commit is contained in:
parent
b58db4a629
commit
4483df2040
1 changed files with 41 additions and 34 deletions
|
@ -5,7 +5,7 @@ import {
|
||||||
GraphQLObjectType,
|
GraphQLObjectType,
|
||||||
ResponsePath,
|
ResponsePath,
|
||||||
responsePathAsArray,
|
responsePathAsArray,
|
||||||
} from 'graphql';
|
} from "graphql";
|
||||||
import { ApolloServerPlugin } from "apollo-server-plugin-base";
|
import { ApolloServerPlugin } from "apollo-server-plugin-base";
|
||||||
|
|
||||||
export interface CacheControlFormat {
|
export interface CacheControlFormat {
|
||||||
|
@ -21,8 +21,8 @@ export interface CacheHint {
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum CacheScope {
|
export enum CacheScope {
|
||||||
Public = 'PUBLIC',
|
Public = "PUBLIC",
|
||||||
Private = 'PRIVATE',
|
Private = "PRIVATE",
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CacheControlExtensionOptions {
|
export interface CacheControlExtensionOptions {
|
||||||
|
@ -36,13 +36,12 @@ export interface CacheControlExtensionOptions {
|
||||||
type MapResponsePathHints = Map<ResponsePath, CacheHint>;
|
type MapResponsePathHints = Map<ResponsePath, CacheHint>;
|
||||||
|
|
||||||
export const plugin = (
|
export const plugin = (
|
||||||
options: CacheControlExtensionOptions = Object.create(null),
|
options: CacheControlExtensionOptions = Object.create(null)
|
||||||
): ApolloServerPlugin => ({
|
): ApolloServerPlugin => ({
|
||||||
requestDidStart(requestContext) {
|
requestDidStart(requestContext) {
|
||||||
const defaultMaxAge: number = options.defaultMaxAge || 0;
|
const defaultMaxAge: number = options.defaultMaxAge || 0;
|
||||||
const hints: MapResponsePathHints = new Map();
|
const hints: MapResponsePathHints = new Map();
|
||||||
|
|
||||||
|
|
||||||
function setOverallCachePolicyWhenUnset() {
|
function setOverallCachePolicyWhenUnset() {
|
||||||
// @ts-ignore: FORK. Don't know enough TypeScript to resolve this!
|
// @ts-ignore: FORK. Don't know enough TypeScript to resolve this!
|
||||||
if (!requestContext.overallCachePolicy) {
|
if (!requestContext.overallCachePolicy) {
|
||||||
|
@ -67,7 +66,7 @@ export const plugin = (
|
||||||
if (targetType.astNode) {
|
if (targetType.astNode) {
|
||||||
hint = mergeHints(
|
hint = mergeHints(
|
||||||
hint,
|
hint,
|
||||||
cacheHintFromDirectives(targetType.astNode.directives),
|
cacheHintFromDirectives(targetType.astNode.directives)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,7 +77,7 @@ export const plugin = (
|
||||||
if (fieldDef.astNode) {
|
if (fieldDef.astNode) {
|
||||||
hint = mergeHints(
|
hint = mergeHints(
|
||||||
hint,
|
hint,
|
||||||
cacheHintFromDirectives(fieldDef.astNode.directives),
|
cacheHintFromDirectives(fieldDef.astNode.directives)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,29 +136,29 @@ export const plugin = (
|
||||||
const overallCachePolicy =
|
const overallCachePolicy =
|
||||||
overallCachePolicyOverride ||
|
overallCachePolicyOverride ||
|
||||||
// @ts-ignore: FORK. Don't know enough TypeScript to resolve this!
|
// @ts-ignore: FORK. Don't know enough TypeScript to resolve this!
|
||||||
(requestContext.overallCachePolicy =
|
(requestContext.overallCachePolicy = computeOverallCachePolicy(
|
||||||
computeOverallCachePolicy(hints));
|
hints
|
||||||
|
));
|
||||||
|
|
||||||
if (
|
if (
|
||||||
overallCachePolicy &&
|
overallCachePolicy &&
|
||||||
options.calculateHttpHeaders &&
|
options.calculateHttpHeaders &&
|
||||||
response.http
|
response.http
|
||||||
) {
|
) {
|
||||||
if (overallCachePolicy.staleWhileRevalidate) { // FORK
|
if (overallCachePolicy.staleWhileRevalidate) {
|
||||||
|
// FORK
|
||||||
response.http.headers.set(
|
response.http.headers.set(
|
||||||
'Cache-Control',
|
"Cache-Control",
|
||||||
`max-age=${
|
`max-age=${overallCachePolicy.maxAge}, stale-while-revalidate=${
|
||||||
overallCachePolicy.maxAge
|
|
||||||
}, stale-while-revalidate=${
|
|
||||||
overallCachePolicy.staleWhileRevalidate
|
overallCachePolicy.staleWhileRevalidate
|
||||||
}, ${overallCachePolicy.scope.toLowerCase()}`,
|
}, ${overallCachePolicy.scope.toLowerCase()}`
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
response.http.headers.set(
|
response.http.headers.set(
|
||||||
'Cache-Control',
|
"Cache-Control",
|
||||||
`max-age=${
|
`max-age=${
|
||||||
overallCachePolicy.maxAge
|
overallCachePolicy.maxAge
|
||||||
}, ${overallCachePolicy.scope.toLowerCase()}`,
|
}, ${overallCachePolicy.scope.toLowerCase()}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -176,7 +175,7 @@ export const plugin = (
|
||||||
const extensions =
|
const extensions =
|
||||||
response.extensions || (response.extensions = Object.create(null));
|
response.extensions || (response.extensions = Object.create(null));
|
||||||
|
|
||||||
if (typeof extensions.cacheControl !== 'undefined') {
|
if (typeof extensions.cacheControl !== "undefined") {
|
||||||
throw new Error("The cacheControl information already existed.");
|
throw new Error("The cacheControl information already existed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,31 +186,32 @@ export const plugin = (
|
||||||
...hint,
|
...hint,
|
||||||
})),
|
})),
|
||||||
};
|
};
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
function cacheHintFromDirectives(
|
function cacheHintFromDirectives(
|
||||||
directives: ReadonlyArray<DirectiveNode> | undefined,
|
directives: ReadonlyArray<DirectiveNode> | undefined
|
||||||
): CacheHint | undefined {
|
): CacheHint | undefined {
|
||||||
if (!directives) return undefined;
|
if (!directives) return undefined;
|
||||||
|
|
||||||
const cacheControlDirective = directives.find(
|
const cacheControlDirective = directives.find(
|
||||||
directive => directive.name.value === 'cacheControl',
|
(directive) => directive.name.value === "cacheControl"
|
||||||
);
|
);
|
||||||
if (!cacheControlDirective) return undefined;
|
if (!cacheControlDirective) return undefined;
|
||||||
|
|
||||||
if (!cacheControlDirective.arguments) return undefined;
|
if (!cacheControlDirective.arguments) return undefined;
|
||||||
|
|
||||||
const maxAgeArgument = cacheControlDirective.arguments.find(
|
const maxAgeArgument = cacheControlDirective.arguments.find(
|
||||||
argument => argument.name.value === 'maxAge',
|
(argument) => argument.name.value === "maxAge"
|
||||||
);
|
);
|
||||||
const staleWhileRevalidateArgument = cacheControlDirective.arguments.find( // FORK
|
const staleWhileRevalidateArgument = cacheControlDirective.arguments.find(
|
||||||
argument => argument.name.value === 'staleWhileRevalidate',
|
// FORK
|
||||||
|
(argument) => argument.name.value === "staleWhileRevalidate"
|
||||||
);
|
);
|
||||||
const scopeArgument = cacheControlDirective.arguments.find(
|
const scopeArgument = cacheControlDirective.arguments.find(
|
||||||
argument => argument.name.value === 'scope',
|
(argument) => argument.name.value === "scope"
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Add proper typechecking of arguments
|
// TODO: Add proper typechecking of arguments
|
||||||
|
@ -219,19 +219,19 @@ function cacheHintFromDirectives(
|
||||||
maxAge:
|
maxAge:
|
||||||
maxAgeArgument &&
|
maxAgeArgument &&
|
||||||
maxAgeArgument.value &&
|
maxAgeArgument.value &&
|
||||||
maxAgeArgument.value.kind === 'IntValue'
|
maxAgeArgument.value.kind === "IntValue"
|
||||||
? parseInt(maxAgeArgument.value.value)
|
? parseInt(maxAgeArgument.value.value)
|
||||||
: undefined,
|
: undefined,
|
||||||
staleWhileRevalidate:
|
staleWhileRevalidate:
|
||||||
staleWhileRevalidateArgument &&
|
staleWhileRevalidateArgument &&
|
||||||
staleWhileRevalidateArgument.value &&
|
staleWhileRevalidateArgument.value &&
|
||||||
staleWhileRevalidateArgument.value.kind === 'IntValue'
|
staleWhileRevalidateArgument.value.kind === "IntValue"
|
||||||
? parseInt(staleWhileRevalidateArgument.value.value)
|
? parseInt(staleWhileRevalidateArgument.value.value)
|
||||||
: undefined,
|
: undefined,
|
||||||
scope:
|
scope:
|
||||||
scopeArgument &&
|
scopeArgument &&
|
||||||
scopeArgument.value &&
|
scopeArgument.value &&
|
||||||
scopeArgument.value.kind === 'EnumValue'
|
scopeArgument.value.kind === "EnumValue"
|
||||||
? (scopeArgument.value.value as CacheScope)
|
? (scopeArgument.value.value as CacheScope)
|
||||||
: undefined,
|
: undefined,
|
||||||
};
|
};
|
||||||
|
@ -239,19 +239,22 @@ function cacheHintFromDirectives(
|
||||||
|
|
||||||
function mergeHints(
|
function mergeHints(
|
||||||
hint: CacheHint,
|
hint: CacheHint,
|
||||||
otherHint: CacheHint | undefined,
|
otherHint: CacheHint | undefined
|
||||||
): CacheHint {
|
): CacheHint {
|
||||||
if (!otherHint) return hint;
|
if (!otherHint) return hint;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
maxAge: otherHint.maxAge !== undefined ? otherHint.maxAge : hint.maxAge,
|
maxAge: otherHint.maxAge !== undefined ? otherHint.maxAge : hint.maxAge,
|
||||||
staleWhileRevalidate: otherHint.staleWhileRevalidate !== undefined ? otherHint.staleWhileRevalidate : hint.staleWhileRevalidate, // FORK
|
staleWhileRevalidate:
|
||||||
|
otherHint.staleWhileRevalidate !== undefined
|
||||||
|
? otherHint.staleWhileRevalidate
|
||||||
|
: hint.staleWhileRevalidate, // FORK
|
||||||
scope: otherHint.scope || hint.scope,
|
scope: otherHint.scope || hint.scope,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeOverallCachePolicy(
|
function computeOverallCachePolicy(
|
||||||
hints: MapResponsePathHints,
|
hints: MapResponsePathHints
|
||||||
): Required<CacheHint> | undefined {
|
): Required<CacheHint> | undefined {
|
||||||
let lowestMaxAge: number | undefined = undefined;
|
let lowestMaxAge: number | undefined = undefined;
|
||||||
let lowestMaxAgePlusSWR: number | undefined = undefined; // FORK
|
let lowestMaxAgePlusSWR: number | undefined = undefined; // FORK
|
||||||
|
@ -305,7 +308,11 @@ function computeOverallCachePolicy(
|
||||||
: undefined;
|
: undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addHint(hints: MapResponsePathHints, path: ResponsePath, hint: CacheHint) {
|
function addHint(
|
||||||
|
hints: MapResponsePathHints,
|
||||||
|
path: ResponsePath,
|
||||||
|
hint: CacheHint
|
||||||
|
) {
|
||||||
const existingCacheHint = hints.get(path);
|
const existingCacheHint = hints.get(path);
|
||||||
if (existingCacheHint) {
|
if (existingCacheHint) {
|
||||||
hints.set(path, mergeHints(existingCacheHint, hint));
|
hints.set(path, mergeHints(existingCacheHint, hint));
|
||||||
|
|
Loading…
Reference in a new issue