-
Notifications
You must be signed in to change notification settings - Fork 2k
/
definition.ts
1623 lines (1436 loc) · 46.7 KB
/
definition.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { devAssert } from '../jsutils/devAssert';
import { didYouMean } from '../jsutils/didYouMean';
import { identityFunc } from '../jsutils/identityFunc';
import { inspect } from '../jsutils/inspect';
import { instanceOf } from '../jsutils/instanceOf';
import { keyMap } from '../jsutils/keyMap';
import { keyValMap } from '../jsutils/keyValMap';
import { mapValue } from '../jsutils/mapValue';
import type { Maybe } from '../jsutils/Maybe';
import type { ObjMap } from '../jsutils/ObjMap';
import type { Path } from '../jsutils/Path';
import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
import { suggestionList } from '../jsutils/suggestionList';
import { toObjMap } from '../jsutils/toObjMap';
import { GraphQLError } from '../error/GraphQLError';
import type {
EnumTypeDefinitionNode,
EnumTypeExtensionNode,
EnumValueDefinitionNode,
FieldDefinitionNode,
FieldNode,
FragmentDefinitionNode,
InputObjectTypeDefinitionNode,
InputObjectTypeExtensionNode,
InputValueDefinitionNode,
InterfaceTypeDefinitionNode,
InterfaceTypeExtensionNode,
ObjectTypeDefinitionNode,
ObjectTypeExtensionNode,
OperationDefinitionNode,
ScalarTypeDefinitionNode,
ScalarTypeExtensionNode,
UnionTypeDefinitionNode,
UnionTypeExtensionNode,
ValueNode,
} from '../language/ast';
import { Kind } from '../language/kinds';
import { print } from '../language/printer';
import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped';
import { assertEnumValueName, assertName } from './assertName';
import type { GraphQLSchema } from './schema';
// Predicates & Assertions
/**
* These are all of the possible kinds of types.
*/
export type GraphQLType = GraphQLNamedType | GraphQLWrappingType;
export function isType(type: unknown): type is GraphQLType {
return (
isScalarType(type) ||
isObjectType(type) ||
isInterfaceType(type) ||
isUnionType(type) ||
isEnumType(type) ||
isInputObjectType(type) ||
isListType(type) ||
isNonNullType(type)
);
}
export function assertType(type: unknown): GraphQLType {
if (!isType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL type.`);
}
return type;
}
/**
* There are predicates for each kind of GraphQL type.
*/
export function isScalarType(type: unknown): type is GraphQLScalarType {
return instanceOf(type, GraphQLScalarType);
}
export function assertScalarType(type: unknown): GraphQLScalarType {
if (!isScalarType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL Scalar type.`);
}
return type;
}
export function isObjectType(type: unknown): type is GraphQLObjectType {
return instanceOf(type, GraphQLObjectType);
}
export function assertObjectType(type: unknown): GraphQLObjectType {
if (!isObjectType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL Object type.`);
}
return type;
}
export function isInterfaceType(type: unknown): type is GraphQLInterfaceType {
return instanceOf(type, GraphQLInterfaceType);
}
export function assertInterfaceType(type: unknown): GraphQLInterfaceType {
if (!isInterfaceType(type)) {
throw new Error(
`Expected ${inspect(type)} to be a GraphQL Interface type.`,
);
}
return type;
}
export function isUnionType(type: unknown): type is GraphQLUnionType {
return instanceOf(type, GraphQLUnionType);
}
export function assertUnionType(type: unknown): GraphQLUnionType {
if (!isUnionType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL Union type.`);
}
return type;
}
export function isEnumType(type: unknown): type is GraphQLEnumType {
return instanceOf(type, GraphQLEnumType);
}
export function assertEnumType(type: unknown): GraphQLEnumType {
if (!isEnumType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL Enum type.`);
}
return type;
}
export function isInputObjectType(
type: unknown,
): type is GraphQLInputObjectType {
return instanceOf(type, GraphQLInputObjectType);
}
export function assertInputObjectType(type: unknown): GraphQLInputObjectType {
if (!isInputObjectType(type)) {
throw new Error(
`Expected ${inspect(type)} to be a GraphQL Input Object type.`,
);
}
return type;
}
export function isListType(
type: GraphQLInputType,
): type is GraphQLList<GraphQLInputType>;
export function isListType(
type: GraphQLOutputType,
): type is GraphQLList<GraphQLOutputType>;
export function isListType(type: unknown): type is GraphQLList<GraphQLType>;
export function isListType(type: unknown): type is GraphQLList<GraphQLType> {
return instanceOf(type, GraphQLList);
}
export function assertListType(type: unknown): GraphQLList<GraphQLType> {
if (!isListType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL List type.`);
}
return type;
}
export function isNonNullType(
type: GraphQLInputType,
): type is GraphQLNonNull<GraphQLNullableInputType>;
export function isNonNullType(
type: GraphQLOutputType,
): type is GraphQLNonNull<GraphQLNullableOutputType>;
export function isNonNullType(
type: unknown,
): type is GraphQLNonNull<GraphQLNullableType>;
export function isNonNullType(
type: unknown,
): type is GraphQLNonNull<GraphQLNullableType> {
return instanceOf(type, GraphQLNonNull);
}
export function assertNonNullType(
type: unknown,
): GraphQLNonNull<GraphQLNullableType> {
if (!isNonNullType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL Non-Null type.`);
}
return type;
}
/**
* These types may be used as input types for arguments and directives.
*/
export type GraphQLNullableInputType =
| GraphQLNamedInputType
| GraphQLList<GraphQLInputType>;
export type GraphQLInputType =
| GraphQLNullableInputType
| GraphQLNonNull<GraphQLNullableInputType>;
export function isInputType(type: unknown): type is GraphQLInputType {
return (
isScalarType(type) ||
isEnumType(type) ||
isInputObjectType(type) ||
(isWrappingType(type) && isInputType(type.ofType))
);
}
export function assertInputType(type: unknown): GraphQLInputType {
if (!isInputType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL input type.`);
}
return type;
}
/**
* These types may be used as output types as the result of fields.
*/
export type GraphQLNullableOutputType =
| GraphQLNamedOutputType
| GraphQLList<GraphQLOutputType>;
export type GraphQLOutputType =
| GraphQLNullableOutputType
| GraphQLNonNull<GraphQLNullableOutputType>;
export function isOutputType(type: unknown): type is GraphQLOutputType {
return (
isScalarType(type) ||
isObjectType(type) ||
isInterfaceType(type) ||
isUnionType(type) ||
isEnumType(type) ||
(isWrappingType(type) && isOutputType(type.ofType))
);
}
export function assertOutputType(type: unknown): GraphQLOutputType {
if (!isOutputType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL output type.`);
}
return type;
}
/**
* These types may describe types which may be leaf values.
*/
export type GraphQLLeafType = GraphQLScalarType | GraphQLEnumType;
export function isLeafType(type: unknown): type is GraphQLLeafType {
return isScalarType(type) || isEnumType(type);
}
export function assertLeafType(type: unknown): GraphQLLeafType {
if (!isLeafType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL leaf type.`);
}
return type;
}
/**
* These types may describe the parent context of a selection set.
*/
export type GraphQLCompositeType =
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType;
export function isCompositeType(type: unknown): type is GraphQLCompositeType {
return isObjectType(type) || isInterfaceType(type) || isUnionType(type);
}
export function assertCompositeType(type: unknown): GraphQLCompositeType {
if (!isCompositeType(type)) {
throw new Error(
`Expected ${inspect(type)} to be a GraphQL composite type.`,
);
}
return type;
}
/**
* These types may describe the parent context of a selection set.
*/
export type GraphQLAbstractType = GraphQLInterfaceType | GraphQLUnionType;
export function isAbstractType(type: unknown): type is GraphQLAbstractType {
return isInterfaceType(type) || isUnionType(type);
}
export function assertAbstractType(type: unknown): GraphQLAbstractType {
if (!isAbstractType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL abstract type.`);
}
return type;
}
/**
* List Type Wrapper
*
* A list is a wrapping type which points to another type.
* Lists are often created within the context of defining the fields of
* an object type.
*
* Example:
*
* ```ts
* const PersonType = new GraphQLObjectType({
* name: 'Person',
* fields: () => ({
* parents: { type: new GraphQLList(PersonType) },
* children: { type: new GraphQLList(PersonType) },
* })
* })
* ```
*/
export class GraphQLList<T extends GraphQLType> {
readonly ofType: T;
constructor(ofType: T) {
this.ofType = ofType;
}
get [Symbol.toStringTag]() {
return 'GraphQLList';
}
toString(): string {
return '[' + String(this.ofType) + ']';
}
toJSON(): string {
return this.toString();
}
}
/**
* Non-Null Type Wrapper
*
* A non-null is a wrapping type which points to another type.
* Non-null types enforce that their values are never null and can ensure
* an error is raised if this ever occurs during a request. It is useful for
* fields which you can make a strong guarantee on non-nullability, for example
* usually the id field of a database row will never be null.
*
* Example:
*
* ```ts
* const RowType = new GraphQLObjectType({
* name: 'Row',
* fields: () => ({
* id: { type: new GraphQLNonNull(GraphQLString) },
* })
* })
* ```
* Note: the enforcement of non-nullability occurs within the executor.
*/
export class GraphQLNonNull<T extends GraphQLNullableType> {
readonly ofType: T;
constructor(ofType: T) {
this.ofType = ofType;
}
get [Symbol.toStringTag]() {
return 'GraphQLNonNull';
}
toString(): string {
return String(this.ofType) + '!';
}
toJSON(): string {
return this.toString();
}
}
/**
* These types wrap and modify other types
*/
export type GraphQLWrappingType =
| GraphQLList<GraphQLType>
| GraphQLNonNull<GraphQLNullableType>;
export function isWrappingType(type: unknown): type is GraphQLWrappingType {
return isListType(type) || isNonNullType(type);
}
export function assertWrappingType(type: unknown): GraphQLWrappingType {
if (!isWrappingType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL wrapping type.`);
}
return type;
}
/**
* These types can all accept null as a value.
*/
export type GraphQLNullableType = GraphQLNamedType | GraphQLList<GraphQLType>;
export function isNullableType(type: unknown): type is GraphQLNullableType {
return isType(type) && !isNonNullType(type);
}
export function assertNullableType(type: unknown): GraphQLNullableType {
if (!isNullableType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL nullable type.`);
}
return type;
}
export function getNullableType(type: undefined | null): void;
export function getNullableType<T extends GraphQLNullableType>(
type: T | GraphQLNonNull<T>,
): T;
export function getNullableType(
type: Maybe<GraphQLType>,
): GraphQLNullableType | undefined;
export function getNullableType(
type: Maybe<GraphQLType>,
): GraphQLNullableType | undefined {
if (type) {
return isNonNullType(type) ? type.ofType : type;
}
}
/**
* These named types do not include modifiers like List or NonNull.
*/
export type GraphQLNamedType = GraphQLNamedInputType | GraphQLNamedOutputType;
export type GraphQLNamedInputType =
| GraphQLScalarType
| GraphQLEnumType
| GraphQLInputObjectType;
export type GraphQLNamedOutputType =
| GraphQLScalarType
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType;
export function isNamedType(type: unknown): type is GraphQLNamedType {
return (
isScalarType(type) ||
isObjectType(type) ||
isInterfaceType(type) ||
isUnionType(type) ||
isEnumType(type) ||
isInputObjectType(type)
);
}
export function assertNamedType(type: unknown): GraphQLNamedType {
if (!isNamedType(type)) {
throw new Error(`Expected ${inspect(type)} to be a GraphQL named type.`);
}
return type;
}
export function getNamedType(type: undefined | null): void;
export function getNamedType(type: GraphQLInputType): GraphQLNamedInputType;
export function getNamedType(type: GraphQLOutputType): GraphQLNamedOutputType;
export function getNamedType(type: GraphQLType): GraphQLNamedType;
export function getNamedType(
type: Maybe<GraphQLType>,
): GraphQLNamedType | undefined;
export function getNamedType(
type: Maybe<GraphQLType>,
): GraphQLNamedType | undefined {
if (type) {
let unwrappedType = type;
while (isWrappingType(unwrappedType)) {
unwrappedType = unwrappedType.ofType;
}
return unwrappedType;
}
}
/**
* Used while defining GraphQL types to allow for circular references in
* otherwise immutable type definitions.
*/
export type ThunkReadonlyArray<T> = (() => ReadonlyArray<T>) | ReadonlyArray<T>;
export type ThunkObjMap<T> = (() => ObjMap<T>) | ObjMap<T>;
export function resolveReadonlyArrayThunk<T>(
thunk: ThunkReadonlyArray<T>,
): ReadonlyArray<T> {
return typeof thunk === 'function' ? thunk() : thunk;
}
export function resolveObjMapThunk<T>(thunk: ThunkObjMap<T>): ObjMap<T> {
return typeof thunk === 'function' ? thunk() : thunk;
}
/**
* Custom extensions
*
* @remarks
* Use a unique identifier name for your extension, for example the name of
* your library or project. Do not use a shortened identifier as this increases
* the risk of conflicts. We recommend you add at most one extension field,
* an object which can contain all the values you need.
*/
export interface GraphQLScalarTypeExtensions {
[attributeName: string]: unknown;
}
/**
* Scalar Type Definition
*
* The leaf values of any request and input values to arguments are
* Scalars (or Enums) and are defined with a name and a series of functions
* used to parse input from ast or variables and to ensure validity.
*
* If a type's serialize function returns `null` or does not return a value
* (i.e. it returns `undefined`) then an error will be raised and a `null`
* value will be returned in the response. It is always better to validate
*
* Example:
*
* ```ts
* const OddType = new GraphQLScalarType({
* name: 'Odd',
* serialize(value) {
* if (!Number.isFinite(value)) {
* throw new Error(
* `Scalar "Odd" cannot represent "${value}" since it is not a finite number.`,
* );
* }
*
* if (value % 2 === 0) {
* throw new Error(`Scalar "Odd" cannot represent "${value}" since it is even.`);
* }
* return value;
* }
* });
* ```
*/
export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal> {
name: string;
description: Maybe<string>;
specifiedByURL: Maybe<string>;
serialize: GraphQLScalarSerializer<TExternal>;
parseValue: GraphQLScalarValueParser<TInternal>;
parseLiteral: GraphQLScalarLiteralParser<TInternal>;
extensions: Readonly<GraphQLScalarTypeExtensions>;
astNode: Maybe<ScalarTypeDefinitionNode>;
extensionASTNodes: ReadonlyArray<ScalarTypeExtensionNode>;
constructor(config: Readonly<GraphQLScalarTypeConfig<TInternal, TExternal>>) {
const parseValue =
config.parseValue ??
(identityFunc as GraphQLScalarValueParser<TInternal>);
this.name = assertName(config.name);
this.description = config.description;
this.specifiedByURL = config.specifiedByURL;
this.serialize =
config.serialize ?? (identityFunc as GraphQLScalarSerializer<TExternal>);
this.parseValue = parseValue;
this.parseLiteral =
config.parseLiteral ??
((node, variables) => parseValue(valueFromASTUntyped(node, variables)));
this.extensions = toObjMap(config.extensions);
this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes ?? [];
if (config.parseLiteral) {
devAssert(
typeof config.parseValue === 'function' &&
typeof config.parseLiteral === 'function',
`${this.name} must provide both "parseValue" and "parseLiteral" functions.`,
);
}
}
get [Symbol.toStringTag]() {
return 'GraphQLScalarType';
}
toConfig(): GraphQLScalarTypeNormalizedConfig<TInternal, TExternal> {
return {
name: this.name,
description: this.description,
specifiedByURL: this.specifiedByURL,
serialize: this.serialize,
parseValue: this.parseValue,
parseLiteral: this.parseLiteral,
extensions: this.extensions,
astNode: this.astNode,
extensionASTNodes: this.extensionASTNodes,
};
}
toString(): string {
return this.name;
}
toJSON(): string {
return this.toString();
}
}
export type GraphQLScalarSerializer<TExternal> = (
outputValue: unknown,
) => TExternal;
export type GraphQLScalarValueParser<TInternal> = (
inputValue: unknown,
) => TInternal;
export type GraphQLScalarLiteralParser<TInternal> = (
valueNode: ValueNode,
variables?: Maybe<ObjMap<unknown>>,
) => TInternal;
export interface GraphQLScalarTypeConfig<TInternal, TExternal> {
name: string;
description?: Maybe<string>;
specifiedByURL?: Maybe<string>;
/** Serializes an internal value to include in a response. */
serialize?: GraphQLScalarSerializer<TExternal> | undefined;
/** Parses an externally provided value to use as an input. */
parseValue?: GraphQLScalarValueParser<TInternal> | undefined;
/** Parses an externally provided literal value to use as an input. */
parseLiteral?: GraphQLScalarLiteralParser<TInternal> | undefined;
extensions?: Maybe<Readonly<GraphQLScalarTypeExtensions>>;
astNode?: Maybe<ScalarTypeDefinitionNode>;
extensionASTNodes?: Maybe<ReadonlyArray<ScalarTypeExtensionNode>>;
}
interface GraphQLScalarTypeNormalizedConfig<TInternal, TExternal>
extends GraphQLScalarTypeConfig<TInternal, TExternal> {
serialize: GraphQLScalarSerializer<TExternal>;
parseValue: GraphQLScalarValueParser<TInternal>;
parseLiteral: GraphQLScalarLiteralParser<TInternal>;
extensions: Readonly<GraphQLScalarTypeExtensions>;
extensionASTNodes: ReadonlyArray<ScalarTypeExtensionNode>;
}
/**
* Custom extensions
*
* @remarks
* Use a unique identifier name for your extension, for example the name of
* your library or project. Do not use a shortened identifier as this increases
* the risk of conflicts. We recommend you add at most one extension field,
* an object which can contain all the values you need.
*
* We've provided these template arguments because this is an open type and
* you may find them useful.
*/
export interface GraphQLObjectTypeExtensions<_TSource = any, _TContext = any> {
[attributeName: string]: unknown;
}
/**
* Object Type Definition
*
* Almost all of the GraphQL types you define will be object types. Object types
* have a name, but most importantly describe their fields.
*
* Example:
*
* ```ts
* const AddressType = new GraphQLObjectType({
* name: 'Address',
* fields: {
* street: { type: GraphQLString },
* number: { type: GraphQLInt },
* formatted: {
* type: GraphQLString,
* resolve(obj) {
* return obj.number + ' ' + obj.street
* }
* }
* }
* });
* ```
*
* When two types need to refer to each other, or a type needs to refer to
* itself in a field, you can use a function expression (aka a closure or a
* thunk) to supply the fields lazily.
*
* Example:
*
* ```ts
* const PersonType = new GraphQLObjectType({
* name: 'Person',
* fields: () => ({
* name: { type: GraphQLString },
* bestFriend: { type: PersonType },
* })
* });
* ```
*/
export class GraphQLObjectType<TSource = any, TContext = any> {
name: string;
description: Maybe<string>;
isTypeOf: Maybe<GraphQLIsTypeOfFn<TSource, TContext>>;
extensions: Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>;
astNode: Maybe<ObjectTypeDefinitionNode>;
extensionASTNodes: ReadonlyArray<ObjectTypeExtensionNode>;
private _fields: ThunkObjMap<GraphQLField<TSource, TContext>>;
private _interfaces: ThunkReadonlyArray<GraphQLInterfaceType>;
constructor(config: Readonly<GraphQLObjectTypeConfig<TSource, TContext>>) {
this.name = assertName(config.name);
this.description = config.description;
this.isTypeOf = config.isTypeOf;
this.extensions = toObjMap(config.extensions);
this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes ?? [];
this._fields = () => defineFieldMap(config);
this._interfaces = () => defineInterfaces(config);
}
get [Symbol.toStringTag]() {
return 'GraphQLObjectType';
}
getFields(): GraphQLFieldMap<TSource, TContext> {
if (typeof this._fields === 'function') {
this._fields = this._fields();
}
return this._fields;
}
getInterfaces(): ReadonlyArray<GraphQLInterfaceType> {
if (typeof this._interfaces === 'function') {
this._interfaces = this._interfaces();
}
return this._interfaces;
}
toConfig(): GraphQLObjectTypeNormalizedConfig<TSource, TContext> {
return {
name: this.name,
description: this.description,
interfaces: this.getInterfaces(),
fields: fieldsToFieldsConfig(this.getFields()),
isTypeOf: this.isTypeOf,
extensions: this.extensions,
astNode: this.astNode,
extensionASTNodes: this.extensionASTNodes,
};
}
toString(): string {
return this.name;
}
toJSON(): string {
return this.toString();
}
}
function defineInterfaces(
config: Readonly<
GraphQLObjectTypeConfig<any, any> | GraphQLInterfaceTypeConfig<any, any>
>,
): ReadonlyArray<GraphQLInterfaceType> {
return resolveReadonlyArrayThunk(config.interfaces ?? []);
}
function defineFieldMap<TSource, TContext>(
config: Readonly<
| GraphQLObjectTypeConfig<TSource, TContext>
| GraphQLInterfaceTypeConfig<TSource, TContext>
>,
): GraphQLFieldMap<TSource, TContext> {
const fieldMap = resolveObjMapThunk(config.fields);
return mapValue(fieldMap, (fieldConfig, fieldName) => {
const argsConfig = fieldConfig.args ?? {};
return {
name: assertName(fieldName),
description: fieldConfig.description,
type: fieldConfig.type,
args: defineArguments(argsConfig),
resolve: fieldConfig.resolve,
subscribe: fieldConfig.subscribe,
deprecationReason: fieldConfig.deprecationReason,
extensions: toObjMap(fieldConfig.extensions),
astNode: fieldConfig.astNode,
};
});
}
export function defineArguments(
config: GraphQLFieldConfigArgumentMap,
): ReadonlyArray<GraphQLArgument> {
return Object.entries(config).map(([argName, argConfig]) => ({
name: assertName(argName),
description: argConfig.description,
type: argConfig.type,
defaultValue: argConfig.defaultValue,
deprecationReason: argConfig.deprecationReason,
extensions: toObjMap(argConfig.extensions),
astNode: argConfig.astNode,
}));
}
function fieldsToFieldsConfig<TSource, TContext>(
fields: GraphQLFieldMap<TSource, TContext>,
): GraphQLFieldConfigMap<TSource, TContext> {
return mapValue(fields, (field) => ({
description: field.description,
type: field.type,
args: argsToArgsConfig(field.args),
resolve: field.resolve,
subscribe: field.subscribe,
deprecationReason: field.deprecationReason,
extensions: field.extensions,
astNode: field.astNode,
}));
}
/**
* @internal
*/
export function argsToArgsConfig(
args: ReadonlyArray<GraphQLArgument>,
): GraphQLFieldConfigArgumentMap {
return keyValMap(
args,
(arg) => arg.name,
(arg) => ({
description: arg.description,
type: arg.type,
defaultValue: arg.defaultValue,
deprecationReason: arg.deprecationReason,
extensions: arg.extensions,
astNode: arg.astNode,
}),
);
}
export interface GraphQLObjectTypeConfig<TSource, TContext> {
name: string;
description?: Maybe<string>;
interfaces?: ThunkReadonlyArray<GraphQLInterfaceType> | undefined;
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>;
isTypeOf?: Maybe<GraphQLIsTypeOfFn<TSource, TContext>>;
extensions?: Maybe<Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>>;
astNode?: Maybe<ObjectTypeDefinitionNode>;
extensionASTNodes?: Maybe<ReadonlyArray<ObjectTypeExtensionNode>>;
}
interface GraphQLObjectTypeNormalizedConfig<TSource, TContext>
extends GraphQLObjectTypeConfig<any, any> {
interfaces: ReadonlyArray<GraphQLInterfaceType>;
fields: GraphQLFieldConfigMap<any, any>;
extensions: Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>;
extensionASTNodes: ReadonlyArray<ObjectTypeExtensionNode>;
}
export type GraphQLTypeResolver<TSource, TContext> = (
value: TSource,
context: TContext,
info: GraphQLResolveInfo,
abstractType: GraphQLAbstractType,
) => PromiseOrValue<string | undefined>;
export type GraphQLIsTypeOfFn<TSource, TContext> = (
source: TSource,
context: TContext,
info: GraphQLResolveInfo,
) => PromiseOrValue<boolean>;
export type GraphQLFieldResolver<
TSource,
TContext,
TArgs = any,
TResult = unknown,
> = (
source: TSource,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo,
) => TResult;
export interface GraphQLResolveInfo {
readonly fieldName: string;
readonly fieldNodes: ReadonlyArray<FieldNode>;
readonly returnType: GraphQLOutputType;
readonly parentType: GraphQLObjectType;
readonly path: Path;
readonly schema: GraphQLSchema;
readonly fragments: ObjMap<FragmentDefinitionNode>;
readonly rootValue: unknown;
readonly operation: OperationDefinitionNode;
readonly variableValues: { [variable: string]: unknown };
}
/**
* Custom extensions
*
* @remarks
* Use a unique identifier name for your extension, for example the name of
* your library or project. Do not use a shortened identifier as this increases
* the risk of conflicts. We recommend you add at most one extension field,
* an object which can contain all the values you need.
*
* We've provided these template arguments because this is an open type and
* you may find them useful.
*/
export interface GraphQLFieldExtensions<_TSource, _TContext, _TArgs = any> {
[attributeName: string]: unknown;
}
export interface GraphQLFieldConfig<TSource, TContext, TArgs = any> {
description?: Maybe<string>;
type: GraphQLOutputType;
args?: GraphQLFieldConfigArgumentMap | undefined;
resolve?: GraphQLFieldResolver<TSource, TContext, TArgs> | undefined;
subscribe?: GraphQLFieldResolver<TSource, TContext, TArgs> | undefined;
deprecationReason?: Maybe<string>;
extensions?: Maybe<
Readonly<GraphQLFieldExtensions<TSource, TContext, TArgs>>
>;
astNode?: Maybe<FieldDefinitionNode>;
}
export type GraphQLFieldConfigArgumentMap = ObjMap<GraphQLArgumentConfig>;
/**
* Custom extensions
*
* @remarks
* Use a unique identifier name for your extension, for example the name of
* your library or project. Do not use a shortened identifier as this increases
* the risk of conflicts. We recommend you add at most one extension field,
* an object which can contain all the values you need.
*/
export interface GraphQLArgumentExtensions {
[attributeName: string]: unknown;
}
export interface GraphQLArgumentConfig {
description?: Maybe<string>;
type: GraphQLInputType;
defaultValue?: unknown;
deprecationReason?: Maybe<string>;
extensions?: Maybe<Readonly<GraphQLArgumentExtensions>>;
astNode?: Maybe<InputValueDefinitionNode>;
}
export type GraphQLFieldConfigMap<TSource, TContext> = ObjMap<
GraphQLFieldConfig<TSource, TContext>
>;
export interface GraphQLField<TSource, TContext, TArgs = any> {
name: string;
description: Maybe<string>;
type: GraphQLOutputType;
args: ReadonlyArray<GraphQLArgument>;
resolve?: GraphQLFieldResolver<TSource, TContext, TArgs> | undefined;
subscribe?: GraphQLFieldResolver<TSource, TContext, TArgs> | undefined;
deprecationReason: Maybe<string>;
extensions: Readonly<GraphQLFieldExtensions<TSource, TContext, TArgs>>;
astNode: Maybe<FieldDefinitionNode>;
}
export interface GraphQLArgument {
name: string;
description: Maybe<string>;
type: GraphQLInputType;
defaultValue: unknown;
deprecationReason: Maybe<string>;
extensions: Readonly<GraphQLArgumentExtensions>;
astNode: Maybe<InputValueDefinitionNode>;
}
export function isRequiredArgument(arg: GraphQLArgument): boolean {
return isNonNullType(arg.type) && arg.defaultValue === undefined;
}
export type GraphQLFieldMap<TSource, TContext> = ObjMap<
GraphQLField<TSource, TContext>
>;
/**
* Custom extensions
*
* @remarks
* Use a unique identifier name for your extension, for example the name of
* your library or project. Do not use a shortened identifier as this increases
* the risk of conflicts. We recommend you add at most one extension field,
* an object which can contain all the values you need.
*/
export interface GraphQLInterfaceTypeExtensions {