/
pluginTestHarness.ts
152 lines (130 loc) 路 4.28 KB
/
pluginTestHarness.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
WithRequired,
GraphQLRequest,
GraphQLRequestContextExecutionDidStart,
GraphQLResponse,
ValueOrPromise,
GraphQLRequestContextWillSendResponse,
GraphQLRequestContext,
Logger,
} from 'apollo-server-types';
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql/type';
import { CacheHint } from 'apollo-cache-control';
import {
enablePluginsForSchemaResolvers,
symbolRequestListenerDispatcher,
} from './schemaInstrumentation';
import { ApolloServerPlugin } from 'apollo-server-plugin-base';
import { InMemoryLRUCache } from 'apollo-server-caching';
import { Dispatcher } from './dispatcher';
// This test harness guarantees the presence of `query`.
type IPluginTestHarnessGraphqlRequest = WithRequired<GraphQLRequest, 'query'>;
type IPluginTestHarnessExecutionDidStart<TContext> =
GraphQLRequestContextExecutionDidStart<TContext> & {
request: IPluginTestHarnessGraphqlRequest,
};
export default async function pluginTestHarness<TContext>({
pluginInstance,
schema,
logger,
graphqlRequest,
overallCachePolicy,
executor,
context = Object.create(null)
}: {
/**
* An instance of the plugin to test.
*/
pluginInstance: ApolloServerPlugin<TContext>,
/**
* The optional schema that will be received by the executor. If not
* specified, a simple default schema will be created. In either case,
* the schema will be mutated by wrapping the resolvers with the
* `willResolveField` instrumentation that will allow it to respond to
* that lifecycle hook's implementations plugins.
*/
schema?: GraphQLSchema;
/**
* An optional logger (Defaults to `console`)
*/
logger?: Logger;
/**
* The `GraphQLRequest` which will be received by the `executor`. The
* `query` is required, and this doesn't support anything more exotic,
* like automated persisted queries (APQ).
*/
graphqlRequest: IPluginTestHarnessGraphqlRequest;
/**
* Overall cache control policy.
*/
overallCachePolicy?: Required<CacheHint>;
/**
* This method will be executed to retrieve the response.
*/
executor: (
requestContext: IPluginTestHarnessExecutionDidStart<TContext>,
) => ValueOrPromise<GraphQLResponse>;
/**
* (optional) To provide a user context, if necessary.
*/
context?: TContext;
}): Promise<GraphQLRequestContextWillSendResponse<TContext>> {
if (!schema) {
schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
resolve() {
return 'hello world';
}
}
}
})
});
}
enablePluginsForSchemaResolvers(schema);
const requestContext: GraphQLRequestContext<TContext> = {
logger: logger || console,
request: graphqlRequest,
metrics: Object.create(null),
source: graphqlRequest.query,
cache: new InMemoryLRUCache(),
context,
};
requestContext.overallCachePolicy = overallCachePolicy;
if (typeof pluginInstance.requestDidStart !== "function") {
throw new Error("Should be impossible as the plugin is defined.");
}
const listener = pluginInstance.requestDidStart(requestContext);
if (!listener) {
throw new Error("Should be impossible to not have a listener.");
}
if (typeof listener.willResolveField !== 'function') {
throw new Error("Should be impossible to not have 'willResolveField'.");
}
const dispatcher = new Dispatcher([listener]);
// Put the dispatcher on the context so `willResolveField` can access it.
Object.defineProperty(requestContext.context, symbolRequestListenerDispatcher, {
value: dispatcher,
});
const executionDidEnd = dispatcher.invokeDidStartHook(
"executionDidStart",
requestContext as IPluginTestHarnessExecutionDidStart<TContext>,
);
try {
// `response` is readonly, so we'll cast to `any` to assign to it.
(requestContext.response as any) = await executor(
requestContext as IPluginTestHarnessExecutionDidStart<TContext>,
);
executionDidEnd();
} catch (executionError) {
executionDidEnd(executionError);
}
await dispatcher.invokeHookAsync(
"willSendResponse",
requestContext as GraphQLRequestContextWillSendResponse<TContext>,
);
return requestContext as GraphQLRequestContextWillSendResponse<TContext>;
}