mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 02:01:40 -05:00
Add support for Apollo Cache Control
This commit is contained in:
parent
e8493167c9
commit
818ddfa3bd
5 changed files with 36 additions and 24 deletions
|
@ -5,6 +5,7 @@
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"compile": "tsc",
|
"compile": "tsc",
|
||||||
|
"watch": "tsc -w",
|
||||||
"prepublish": "npm run compile"
|
"prepublish": "npm run compile"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
|
@ -27,7 +28,8 @@
|
||||||
"@types/fibers": "0.0.29",
|
"@types/fibers": "0.0.29",
|
||||||
"@types/graphql": "0.11.5",
|
"@types/graphql": "0.11.5",
|
||||||
"fibers": "1.0.15",
|
"fibers": "1.0.15",
|
||||||
"meteor-promise": "0.8.6"
|
"meteor-promise": "0.8.6",
|
||||||
|
"typescript": "^2.5.3"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"graphql": "^0.9.0 || ^0.10.0 || ^0.11.0"
|
"graphql": "^0.9.0 || ^0.10.0 || ^0.11.0"
|
||||||
|
@ -37,6 +39,8 @@
|
||||||
"definition": "dist/index.d.ts"
|
"definition": "dist/index.d.ts"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"apollo-tracing": "^0.0.x"
|
"graphql-extensions": "^0.0.x",
|
||||||
|
"apollo-tracing": "^0.1.0",
|
||||||
|
"apollo-cache-control": "^0.0.x"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { GraphQLSchema, ValidationContext, GraphQLFieldResolver } from 'graphql';
|
import { GraphQLSchema, ValidationContext, GraphQLFieldResolver } from 'graphql';
|
||||||
import { LogFunction } from './runQuery';
|
import { LogFunction } from './runQuery';
|
||||||
|
import { GraphQLExtension } from 'graphql-extensions';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* GraphQLServerOptions
|
* GraphQLServerOptions
|
||||||
|
@ -28,6 +29,7 @@ export interface GraphQLServerOptions {
|
||||||
fieldResolver?: GraphQLFieldResolver<any, any>;
|
fieldResolver?: GraphQLFieldResolver<any, any>;
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
tracing?: boolean;
|
tracing?: boolean;
|
||||||
|
cacheControl?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default GraphQLServerOptions;
|
export default GraphQLServerOptions;
|
||||||
|
|
|
@ -119,6 +119,7 @@ export async function runHttpQuery(handlerArguments: Array<any>, request: HttpQu
|
||||||
fieldResolver: optionsObject.fieldResolver,
|
fieldResolver: optionsObject.fieldResolver,
|
||||||
debug: optionsObject.debug,
|
debug: optionsObject.debug,
|
||||||
tracing: optionsObject.tracing,
|
tracing: optionsObject.tracing,
|
||||||
|
cacheControl: optionsObject.cacheControl,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (optionsObject.formatParams) {
|
if (optionsObject.formatParams) {
|
||||||
|
|
|
@ -13,11 +13,9 @@ import {
|
||||||
ValidationContext,
|
ValidationContext,
|
||||||
} from 'graphql';
|
} from 'graphql';
|
||||||
|
|
||||||
import {
|
import { enableGraphQLExtensions, GraphQLExtension, GraphQLExtensionStack } from 'graphql-extensions';
|
||||||
TraceCollector,
|
import { TracingExtension } from 'apollo-tracing';
|
||||||
instrumentSchemaForTracing,
|
import { CacheControlExtension } from 'apollo-cache-control';
|
||||||
formatTraceData,
|
|
||||||
} from 'apollo-tracing';
|
|
||||||
|
|
||||||
export interface GraphQLResponse {
|
export interface GraphQLResponse {
|
||||||
data?: object;
|
data?: object;
|
||||||
|
@ -61,6 +59,7 @@ export interface QueryOptions {
|
||||||
formatResponse?: Function;
|
formatResponse?: Function;
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
tracing?: boolean;
|
tracing?: boolean;
|
||||||
|
cacheControl?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolvedPromise = Promise.resolve();
|
const resolvedPromise = Promise.resolve();
|
||||||
|
@ -80,13 +79,20 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
|
||||||
logFunction({action: LogAction.request, step: LogStep.start});
|
logFunction({action: LogAction.request, step: LogStep.start});
|
||||||
|
|
||||||
const context = options.context || {};
|
const context = options.context || {};
|
||||||
|
let extensions = [];
|
||||||
let traceCollector: TraceCollector;
|
|
||||||
if (options.tracing) {
|
if (options.tracing) {
|
||||||
traceCollector = new TraceCollector();
|
extensions.push(TracingExtension);
|
||||||
context._traceCollector = traceCollector;
|
}
|
||||||
traceCollector.requestDidStart();
|
if (options.cacheControl) {
|
||||||
instrumentSchemaForTracing(options.schema);
|
extensions.push(CacheControlExtension);
|
||||||
|
}
|
||||||
|
const extensionStack = extensions.length > 0 && new GraphQLExtensionStack(extensions);
|
||||||
|
|
||||||
|
if (extensionStack) {
|
||||||
|
context._extensionStack = extensionStack;
|
||||||
|
enableGraphQLExtensions(options.schema);
|
||||||
|
|
||||||
|
extensionStack.requestDidStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
function format(errors: Array<Error>): Array<Error> {
|
function format(errors: Array<Error>): Array<Error> {
|
||||||
|
@ -140,8 +146,8 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
|
||||||
return Promise.resolve({ errors: format(validationErrors) });
|
return Promise.resolve({ errors: format(validationErrors) });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (traceCollector) {
|
if (extensionStack) {
|
||||||
traceCollector.executionDidStart();
|
extensionStack.executionDidStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -169,11 +175,10 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (traceCollector) {
|
if (extensionStack) {
|
||||||
traceCollector.requestDidEnd();
|
extensionStack.executionDidEnd();
|
||||||
response.extensions = {
|
extensionStack.requestDidEnd();
|
||||||
'tracing': formatTraceData(traceCollector),
|
response.extensions = extensionStack.format();
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.formatResponse) {
|
if (options.formatResponse) {
|
||||||
|
|
|
@ -2,11 +2,11 @@
|
||||||
"extends": "../../tsconfig",
|
"extends": "../../tsconfig",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"rootDir": "./src",
|
"rootDir": "./src",
|
||||||
"outDir": "./dist",
|
"outDir": "./dist"
|
||||||
"typeRoots": [
|
|
||||||
"node_modules/@types"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
|
"include" : [
|
||||||
|
"src/**/*"
|
||||||
|
],
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"node_modules",
|
"node_modules",
|
||||||
"dist"
|
"dist"
|
||||||
|
|
Loading…
Add table
Reference in a new issue