-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
introspection.ts
220 lines (190 loc) · 5.81 KB
/
introspection.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
import type { obj } from './utils';
/** Format of introspection data queryied from your schema.
*
* @remarks
* You must provide your introspected schema in the standard introspection
* format (as represented by this type) to `setupSchema` to configure this
* library to use your types.
*
* @see {@link setupSchema} for where to use this data.
*/
export interface IntrospectionQuery {
readonly __schema: IntrospectionSchema;
}
interface IntrospectionSchema {
readonly queryType: IntrospectionNamedTypeRef;
readonly mutationType?: IntrospectionNamedTypeRef | null;
readonly subscriptionType?: IntrospectionNamedTypeRef | null;
readonly types: readonly IntrospectionType[];
}
export type IntrospectionType =
| IntrospectionScalarType
| IntrospectionObjectType
| IntrospectionInterfaceType
| IntrospectionUnionType
| IntrospectionEnumType
| IntrospectionInputObjectType;
interface IntrospectionScalarType {
readonly kind: 'SCALAR';
readonly name: string;
readonly specifiedByURL?: string | null;
}
export interface IntrospectionObjectType {
readonly kind: 'OBJECT';
readonly name: string;
readonly fields: readonly IntrospectionField[];
readonly interfaces: readonly IntrospectionNamedTypeRef[] | never;
}
interface IntrospectionInterfaceType {
readonly kind: 'INTERFACE';
readonly name: string;
readonly fields: readonly IntrospectionField[];
readonly possibleTypes: readonly IntrospectionNamedTypeRef[];
readonly interfaces?: readonly IntrospectionNamedTypeRef[] | null;
}
interface IntrospectionUnionType {
readonly kind: 'UNION';
readonly name: string;
readonly possibleTypes: readonly IntrospectionNamedTypeRef[];
}
interface IntrospectionEnumValue {
readonly name: string;
}
interface IntrospectionEnumType {
readonly kind: 'ENUM';
readonly name: string;
readonly enumValues: readonly IntrospectionEnumValue[];
}
interface IntrospectionInputObjectType {
readonly kind: 'INPUT_OBJECT';
readonly name: string;
readonly inputFields: readonly IntrospectionInputValue[];
}
export interface IntrospectionListTypeRef {
readonly kind: 'LIST';
readonly ofType: IntrospectionTypeRef;
}
export interface IntrospectionNonNullTypeRef {
readonly kind: 'NON_NULL';
readonly ofType: IntrospectionTypeRef;
}
export type IntrospectionTypeRef =
| IntrospectionNamedTypeRef
| IntrospectionListTypeRef
| IntrospectionNonNullTypeRef;
export interface IntrospectionNamedTypeRef {
readonly name: string;
}
export interface IntrospectionField {
readonly name: string;
readonly args: readonly IntrospectionInputValue[];
readonly type: IntrospectionTypeRef;
}
interface IntrospectionInputValue {
readonly name: string;
readonly type: IntrospectionTypeRef;
readonly defaultValue?: string | null;
}
interface DefaultScalars {
ID: number | string;
Boolean: boolean;
String: string;
Float: number;
Int: number;
}
type mapNames<T extends readonly any[]> = obj<{
[P in T[number]['name']]: T[number] extends infer Value
? Value extends { readonly name: P }
? obj<Value>
: never
: never;
}>;
type mapScalar<
Type extends IntrospectionScalarType,
Scalars extends ScalarsLike = DefaultScalars,
> = {
kind: 'SCALAR';
type: Type['name'] extends keyof Scalars
? Scalars[Type['name']]
: Type['name'] extends keyof DefaultScalars
? DefaultScalars[Type['name']]
: unknown;
};
type mapEnum<T extends IntrospectionEnumType> = {
kind: 'ENUM';
name: T['name'];
type: T['enumValues'][number]['name'];
};
export type mapObject<T extends IntrospectionObjectType> = {
kind: 'OBJECT';
name: T['name'];
interfaces: T['interfaces'][number]['name'];
fields: obj<mapNames<T['fields']>>;
};
export type mapInputObject<T extends IntrospectionInputObjectType> = {
kind: 'INPUT_OBJECT';
name: T['name'];
inputFields: [...T['inputFields']];
};
type mapInterface<T extends IntrospectionInterfaceType> = {
kind: 'INTERFACE';
name: T['name'];
interfaces: T['interfaces'] extends readonly any[] ? T['interfaces'][number]['name'] : never;
possibleTypes: T['possibleTypes'][number]['name'];
fields: obj<mapNames<T['fields']>>;
};
type mapUnion<T extends IntrospectionUnionType> = {
kind: 'UNION';
name: T['name'];
fields: {};
possibleTypes: T['possibleTypes'][number]['name'];
};
type mapType<
Type,
Scalars extends ScalarsLike = DefaultScalars,
> = Type extends IntrospectionScalarType
? mapScalar<Type, Scalars>
: Type extends IntrospectionEnumType
? mapEnum<Type>
: Type extends IntrospectionObjectType
? mapObject<Type>
: Type extends IntrospectionInterfaceType
? mapInterface<Type>
: Type extends IntrospectionUnionType
? mapUnion<Type>
: Type extends IntrospectionInputObjectType
? mapInputObject<Type>
: never;
type mapIntrospectionTypes<
Query extends IntrospectionQuery,
Scalars extends ScalarsLike = DefaultScalars,
> = obj<{
[P in Query['__schema']['types'][number]['name']]: Query['__schema']['types'][number] extends infer Type
? Type extends { readonly name: P }
? mapType<Type, Scalars>
: never
: never;
}>;
type mapIntrospection<
Query extends IntrospectionQuery,
Scalars extends ScalarsLike = DefaultScalars,
> = {
query: Query['__schema']['queryType']['name'];
mutation: Query['__schema']['mutationType'] extends { name: string }
? Query['__schema']['mutationType']['name']
: never;
subscription: Query['__schema']['subscriptionType'] extends { name: string }
? Query['__schema']['subscriptionType']['name']
: never;
types: mapIntrospectionTypes<Query, Scalars>;
};
export type ScalarsLike = {
[name: string]: any;
};
export type IntrospectionLikeType = {
query: string;
mutation: string | never;
subscription: string | never;
types: { [name: string]: any };
};
export type { mapIntrospectionTypes, mapIntrospection };