-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
Interfaces.ts
313 lines (272 loc) · 8.65 KB
/
Interfaces.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import {
GraphQLSchema,
GraphQLField,
ExecutionResult,
GraphQLInputType,
GraphQLType,
GraphQLNamedType,
GraphQLFieldResolver,
GraphQLResolveInfo,
GraphQLIsTypeOfFn,
GraphQLTypeResolver,
GraphQLScalarType,
DocumentNode,
GraphQLEnumValue,
GraphQLEnumType,
GraphQLUnionType,
GraphQLArgument,
GraphQLInputField,
GraphQLInputObjectType,
GraphQLInterfaceType,
GraphQLObjectType,
} from 'graphql';
import { SchemaDirectiveVisitor } from './utils/SchemaDirectiveVisitor';
import { SchemaVisitor } from './utils/SchemaVisitor';
import { ApolloLink } from 'apollo-link';
/* TODO: Add documentation */
export type UnitOrList<Type> = Type | Array<Type>;
export interface IResolverValidationOptions {
requireResolversForArgs?: boolean;
requireResolversForNonScalar?: boolean;
requireResolversForAllFields?: boolean;
requireResolversForResolveType?: boolean;
allowResolversNotInSchema?: boolean;
}
export interface IAddResolveFunctionsToSchemaOptions {
schema: GraphQLSchema;
resolvers: IResolvers;
defaultFieldResolver?: IFieldResolver<any, any>;
resolverValidationOptions?: IResolverValidationOptions;
inheritResolversFromInterfaces?: boolean;
}
export interface IResolverOptions<TSource = any, TContext = any, TArgs = any> {
fragment?: string;
resolve?: IFieldResolver<TSource, TContext, TArgs>;
subscribe?: IFieldResolver<TSource, TContext, TArgs>;
__resolveType?: GraphQLTypeResolver<TSource, TContext>;
__isTypeOf?: GraphQLIsTypeOfFn<TSource, TContext>;
}
export type Transform = {
transformSchema?: (schema: GraphQLSchema) => GraphQLSchema;
transformRequest?: (originalRequest: Request) => Request;
transformResult?: (result: Result) => Result;
};
export interface IGraphQLToolsResolveInfo extends GraphQLResolveInfo {
mergeInfo?: MergeInfo;
}
export type Fetcher = (operation: IFetcherOperation) => Promise<ExecutionResult>;
export interface IFetcherOperation {
query: DocumentNode;
operationName?: string;
variables?: { [key: string]: any };
context?: { [key: string]: any };
}
export type Dispatcher = (context: any) => ApolloLink | Fetcher;
export type SubschemaConfig = {
schema: GraphQLSchemaWithTransforms;
rootValue?: Record<string, any>;
executor?: Delegator;
subscriber?: Delegator;
link?: ApolloLink;
fetcher?: Fetcher;
dispatcher?: Dispatcher;
transforms?: Array<Transform>;
};
export type GraphQLSchemaWithTransforms = GraphQLSchema & { transforms?: Array<Transform> };
export type SchemaLikeObject =
SubschemaConfig |
GraphQLSchema |
string |
DocumentNode |
Array<GraphQLNamedType>;
export function isSubschemaConfig(value: SchemaLikeObject): value is SubschemaConfig {
return !!(value as SubschemaConfig).schema;
}
export interface IDelegateToSchemaOptions<TContext = { [key: string]: any }> {
schema: GraphQLSchema | SubschemaConfig;
operation: Operation;
fieldName: string;
args?: { [key: string]: any };
context: TContext;
info: IGraphQLToolsResolveInfo;
rootValue?: Record<string, any>;
transforms?: Array<Transform>;
skipValidation?: boolean;
}
export type Delegator = ({ document, context, variables }: {
document: DocumentNode;
context?: { [key: string]: any };
variables?: { [key: string]: any };
}) => any;
export type MergeInfo = {
delegate: (
type: 'query' | 'mutation' | 'subscription',
fieldName: string,
args: { [key: string]: any },
context: { [key: string]: any },
info: GraphQLResolveInfo,
transforms?: Array<Transform>,
) => any;
fragments: Array<{
field: string;
fragment: string;
}>;
delegateToSchema<TContext>(options: IDelegateToSchemaOptions<TContext>): any;
};
export type IFieldResolver<TSource, TContext, TArgs = Record<string, any>> = (
source: TSource,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo & { mergeInfo: MergeInfo },
) => any;
export type ITypedef = (() => ITypedef[]) | string | DocumentNode;
export type ITypeDefinitions = ITypedef | ITypedef[];
export type IResolverObject<TSource = any, TContext = any, TArgs = any> = {
[key: string]:
| IFieldResolver<TSource, TContext, TArgs>
| IResolverOptions<TSource, TContext>
| IResolverObject<TSource, TContext>;
};
export type IEnumResolver = { [key: string]: string | number };
export interface IResolvers<TSource = any, TContext = any> {
[key: string]:
| (() => any)
| IResolverObject<TSource, TContext>
| IResolverOptions<TSource, TContext>
| GraphQLScalarType
| IEnumResolver;
}
export type IResolversParameter =
| Array<IResolvers | ((mergeInfo: MergeInfo) => IResolvers)>
| IResolvers
| ((mergeInfo: MergeInfo) => IResolvers);
export interface ILogger {
log: (message: string | Error) => void;
}
export interface IConnectorCls<TContext = any> {
new (context?: TContext): any;
}
export type IConnectorFn<TContext = any> = (context?: TContext) => any;
export type IConnector<TContext = any> =
| IConnectorCls<TContext>
| IConnectorFn<TContext>;
export type IConnectors<TContext = any> = {
[key: string]: IConnector<TContext>;
};
export interface IExecutableSchemaDefinition<TContext = any> {
typeDefs: ITypeDefinitions;
resolvers?: IResolvers<any, TContext> | Array<IResolvers<any, TContext>>;
connectors?: IConnectors<TContext>;
logger?: ILogger;
allowUndefinedInResolve?: boolean;
resolverValidationOptions?: IResolverValidationOptions;
directiveResolvers?: IDirectiveResolvers<any, TContext>;
schemaDirectives?: { [name: string]: typeof SchemaDirectiveVisitor };
parseOptions?: GraphQLParseOptions;
inheritResolversFromInterfaces?: boolean;
}
export type IFieldIteratorFn = (
fieldDef: GraphQLField<any, any>,
typeName: string,
fieldName: string,
) => void;
export type IDefaultValueIteratorFn = (
type: GraphQLInputType,
value: any,
) => void;
export type NextResolverFn = () => Promise<any>;
export type DirectiveResolverFn<TSource = any, TContext = any> = (
next: NextResolverFn,
source: TSource,
args: { [argName: string]: any },
context: TContext,
info: GraphQLResolveInfo,
) => any;
export interface IDirectiveResolvers<TSource = any, TContext = any> {
[directiveName: string]: DirectiveResolverFn<TSource, TContext>;
}
/* XXX on mocks, args are optional, Not sure if a bug. */
export type IMockFn = GraphQLFieldResolver<any, any>;
export type IMocks = { [key: string]: IMockFn };
export type IMockTypeFn = (
type: GraphQLType,
typeName?: string,
fieldName?: string,
) => GraphQLFieldResolver<any, any>;
export interface IMockOptions {
schema: GraphQLSchema;
mocks?: IMocks;
preserveResolvers?: boolean;
}
export interface IMockServer {
query: (
query: string,
vars?: { [key: string]: any },
) => Promise<ExecutionResult>;
}
export type OnTypeConflict = (
left: GraphQLNamedType,
right: GraphQLNamedType,
info?: {
left: {
schema?: GraphQLSchema;
};
right: {
schema?: GraphQLSchema;
};
},
) => GraphQLNamedType;
export type Operation = 'query' | 'mutation' | 'subscription';
export type Request = {
document: DocumentNode;
variables: Record<string, any>;
extensions?: Record<string, any>;
};
export type Result = ExecutionResult & {
extensions?: Record<string, any>;
};
export type ResolveType<T extends GraphQLType> = (type: T) => T;
export type GraphQLParseOptions = {
noLocation?: boolean;
allowLegacySDLEmptyFields?: boolean;
allowLegacySDLImplementsInterfaces?: boolean;
experimentalFragmentVariables?: boolean;
};
export type IndexedObject<V> = { [key: string]: V } | ReadonlyArray<V>;
export type VisitableSchemaType =
GraphQLSchema
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLInputObjectType
| GraphQLNamedType
| GraphQLScalarType
| GraphQLField<any, any>
| GraphQLInputField
| GraphQLArgument
| GraphQLUnionType
| GraphQLEnumType
| GraphQLEnumValue;
export type VisitorSelector = (
type: VisitableSchemaType,
methodName: string,
) => Array<SchemaVisitor | SchemaVisitorMap>;
export enum VisitSchemaKind {
TYPE = 'VisitSchemaKind.TYPE',
SCALAR_TYPE = 'VisitSchemaKind.SCALAR_TYPE',
ENUM_TYPE = 'VisitSchemaKind.ENUM_TYPE',
COMPOSITE_TYPE = 'VisitSchemaKind.COMPOSITE_TYPE',
OBJECT_TYPE = 'VisitSchemaKind.OBJECT_TYPE',
INPUT_OBJECT_TYPE = 'VisitSchemaKind.INPUT_OBJECT_TYPE',
ABSTRACT_TYPE = 'VisitSchemaKind.ABSTRACT_TYPE',
UNION_TYPE = 'VisitSchemaKind.UNION_TYPE',
INTERFACE_TYPE = 'VisitSchemaKind.INTERFACE_TYPE',
ROOT_OBJECT = 'VisitSchemaKind.ROOT_OBJECT',
QUERY = 'VisitSchemaKind.QUERY',
MUTATION = 'VisitSchemaKind.MUTATION',
SUBSCRIPTION = 'VisitSchemaKind.SUBSCRIPTION',
}
export type SchemaVisitorMap = { [key in VisitSchemaKind]?: TypeVisitor };
export type TypeVisitor = (
type: GraphQLType,
schema: GraphQLSchema,
) => GraphQLNamedType | null | undefined;