-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathindex.ts
215 lines (186 loc) · 6.89 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { Request, Response } from 'apollo-server-env';
import {
GraphQLSchema,
ValidationContext,
ASTVisitor,
GraphQLFormattedError,
OperationDefinitionNode,
DocumentNode,
GraphQLError,
GraphQLResolveInfo,
} from 'graphql';
// This seems like it could live in this package too.
import { KeyValueCache } from 'apollo-server-caching';
import { Trace } from 'apollo-engine-reporting-protobuf';
export type BaseContext = Record<string, any>;
export type ValueOrPromise<T> = T | Promise<T>;
export type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
/**
* It is not recommended to use this `AnyFunction` type further.
*
* This is a legacy type which aims to do what its name suggests (be the type
* for _any_ function) but it should be replaced with something from the
* TypeScript standard lib. It doesn't truly cover "any" function right now,
* and in particular doesn't consider `this`. For now, it has been brought
* here from the Apollo Server `Dispatcher`, where it was first utilized.
*/
export type AnyFunction = (...args: any[]) => any;
/**
* A map of `AnyFunction`s which are the interface for our plugin API's
* request listeners. (e.g. `GraphQLRequestListener`s).
*/
export type AnyFunctionMap = { [key: string]: AnyFunction | undefined };
type Mutable<T> = { -readonly [P in keyof T]: T[P] };
// By default, TypeScript uses structural typing (as opposed to nominal typing)
// Put another way, if it looks like the type and walks like that type, then
// TypeScript lets it be a type.
//
// That's often okay, but it leaves a lot to be desired since a `string` of one
// type can just be passed in as `string` for that type and TypeScript won't
// complain. Flow offers opaque types which solve this, but TypeScript doesn't
// offer this (yet?). This Faux-paque type can be used to gain nominal-esque
// typing, which is incredibly beneficial during re-factors!
type Fauxpaque<K, T> = K & { __fauxpaque: T };
export type SchemaHash = Fauxpaque<string, 'SchemaHash'>;
export interface GraphQLServiceContext {
logger: Logger;
schema: GraphQLSchema;
schemaHash: SchemaHash;
engine: {
serviceID?: string;
apiKeyHash?: string;
};
persistedQueries?: {
cache: KeyValueCache;
};
}
export interface GraphQLRequest {
query?: string;
operationName?: string;
variables?: VariableValues;
extensions?: Record<string, any>;
http?: Pick<Request, 'url' | 'method' | 'headers'>;
}
export type VariableValues = { [name: string]: any };
export interface GraphQLResponse {
data?: Record<string, any> | null;
errors?: ReadonlyArray<GraphQLFormattedError>;
extensions?: Record<string, any>;
http?: Pick<Response, 'headers'> & Partial<Pick<Mutable<Response>, 'status'>>;
}
export interface GraphQLRequestMetrics {
captureTraces?: boolean;
persistedQueryHit?: boolean;
persistedQueryRegister?: boolean;
responseCacheHit?: boolean;
forbiddenOperation?: boolean;
registeredOperation?: boolean;
startHrTime?: [number, number];
queryPlanTrace?: Trace.QueryPlanNode;
}
export interface GraphQLRequestContext<TContext = Record<string, any>> {
readonly request: GraphQLRequest;
readonly response?: GraphQLResponse;
logger: Logger;
readonly context: TContext;
readonly cache: KeyValueCache;
readonly queryHash?: string;
readonly document?: DocumentNode;
readonly source?: string;
// `operationName` is set based on the operation AST, so it is defined even if
// no `request.operationName` was passed in. It will be set to `null` for an
// anonymous operation, or if `requestName.operationName` was passed in but
// doesn't resolve to an operation in the document.
readonly operationName?: string | null;
readonly operation?: OperationDefinitionNode;
/**
* Unformatted errors which have occurred during the request. Note that these
* are present earlier in the request pipeline and differ from **formatted**
* errors which are the result of running the user-configurable `formatError`
* transformation function over specific errors.
*/
readonly errors?: ReadonlyArray<GraphQLError>;
readonly metrics?: GraphQLRequestMetrics;
debug?: boolean;
}
export type ValidationRule = (context: ValidationContext) => ASTVisitor;
export class InvalidGraphQLRequestError extends GraphQLError {}
export type GraphQLExecutor<TContext = Record<string, any>> = (
requestContext: GraphQLRequestContextExecutionDidStart<TContext>,
) => ValueOrPromise<GraphQLExecutionResult>;
export type GraphQLExecutionResult = {
data?: Record<string, any> | null;
errors?: ReadonlyArray<GraphQLError>;
extensions?: Record<string, any>;
};
export type Logger = {
// Ordered from least-severe to most-severe.
debug(message?: any): void;
info(message?: any): void;
warn(message?: any): void;
error(message?: any): void;
}
/**
* This is an object form of the parameters received by typical
* `graphql-js` resolvers. The function type is `GraphQLFieldResolver`
* and normally uses positional parameters. In order to facilitate better
* ergonomics in the Apollo Server plugin API, these have been converted to
* named properties on the object using their names from the upstream
* `GraphQLFieldResolver` type signature. Ergonomic wins, in this case,
* include not needing to have three unused variables in scope just because
* there was a need to access the `info` property in a wrapped plugin.
*/
export type GraphQLFieldResolverParams<
TSource,
TContext,
TArgs = { [argName: string]: any }
> = {
source: TSource;
args: TArgs;
context: TContext;
info: GraphQLResolveInfo;
};
export type GraphQLRequestContextDidResolveSource<TContext> =
WithRequired<GraphQLRequestContext<TContext>,
| 'metrics'
| 'source'
| 'queryHash'
>;
export type GraphQLRequestContextParsingDidStart<TContext> =
GraphQLRequestContextDidResolveSource<TContext>;
export type GraphQLRequestContextValidationDidStart<TContext> =
GraphQLRequestContextParsingDidStart<TContext> &
WithRequired<GraphQLRequestContext<TContext>,
| 'document'
>;
export type GraphQLRequestContextDidResolveOperation<TContext> =
GraphQLRequestContextValidationDidStart<TContext> &
WithRequired<GraphQLRequestContext<TContext>,
| 'operation'
| 'operationName'
>;
export type GraphQLRequestContextDidEncounterErrors<TContext> =
WithRequired<GraphQLRequestContext<TContext>,
| 'metrics'
| 'errors'
>;
export type GraphQLRequestContextResponseForOperation<TContext> =
WithRequired<GraphQLRequestContext<TContext>,
| 'metrics'
| 'source'
| 'document'
| 'operation'
| 'operationName'
>;
export type GraphQLRequestContextExecutionDidStart<TContext> =
GraphQLRequestContextParsingDidStart<TContext> &
WithRequired<GraphQLRequestContext<TContext>,
| 'document'
| 'operation'
| 'operationName'
>;
export type GraphQLRequestContextWillSendResponse<TContext> =
WithRequired<GraphQLRequestContext<TContext>,
| 'metrics'
| 'response'
>;