-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy patherrors.ts
More file actions
2328 lines (2101 loc) · 90.3 KB
/
Copy patherrors.ts
File metadata and controls
2328 lines (2101 loc) · 90.3 KB
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 { Kind, type OperationTypeNode } from 'graphql';
import {
type EntityInterfaceFederationData,
type FieldData,
type InputValueData,
type ObjectDefinitionData,
} from '../schema-building/types/types';
import {
type InvalidEntityReturnTypeErrorParams,
type IncompatibleMergedTypesErrorParams,
type IncompatibleParentTypeMergeErrorParams,
type IncompatibleTypeWithProvidesErrorMessageParams,
type InvalidArgumentValueErrorParams,
type InvalidCustomDirectiveErrorParams,
type InvalidDirectiveLocationErrorParams,
type InvalidLinkDirectiveImportObjectErrorParams,
type InvalidNamedTypeErrorParams,
type InvalidRepeatedDirectiveErrorParams,
type InvalidSubValueFieldLinkDirectiveImportErrorParams,
type invalidVersionLinkDirectiveUrlErrorParams,
type MaxAgeNotPositiveIntegerErrorParams,
type NonExternalConditionalFieldErrorParams,
type OneOfRequiredFieldsErrorParams,
type SemanticNonNullLevelsIndexOutOfBoundsErrorParams,
type SemanticNonNullLevelsNonNullErrorParams,
} from './types/params';
import { type UnresolvableFieldData } from '../resolvability-graph/utils/utils';
import {
AND_UPPER,
ARGUMENT,
FIELD,
FIELD_PATH,
IN_UPPER,
INPUT_FIELD,
INTERFACE,
LEVELS,
LITERAL_NEW_LINE,
LITERAL_PERIOD,
NOT_UPPER,
OR_UPPER,
QUOTATION_JOIN,
SUBSCRIPTION_FIELD_CONDITION,
SUBSCRIPTION_FILTER,
SUBSCRIPTION_FILTER_CONDITION,
SUBSCRIPTION_FILTER_VALUE,
TYPENAME,
UNION,
VALUES,
} from '../utils/string-constants';
import { MAX_SUBSCRIPTION_FILTER_DEPTH, MAXIMUM_TYPE_NESTING } from '../utils/integer-constants';
import { getEntriesNotInHashSet, getOrThrowError, kindToNodeType } from '../utils/utils';
import {
type ImplementationErrors,
type InvalidEntityInterface,
type InvalidRequiredInputValueData,
} from '../utils/types';
import { isFieldData } from '../schema-building/utils';
import { printTypeNode } from '@graphql-tools/merge';
import {
type ArgumentName,
type DirectiveArgumentCoords,
type DirectiveLocation,
type DirectiveName,
type FieldName,
type NodeType,
type SubgraphName,
type TypeName,
} from '../types/types';
import { type InvalidRootTypeFieldEventsDirectiveData } from './types/types';
export const minimumSubgraphRequirementError = new Error('At least one subgraph is required for federation.');
export function multipleNamedTypeDefinitionError(
typeName: string,
firstTypeString: string,
secondTypeString: string,
): Error {
return new Error(
`The named type "${typeName}" is defined as both types "${firstTypeString}" and "${secondTypeString}".` +
`\nHowever, there must be only one type named "${typeName}".`,
);
}
export function incompatibleInputValueDefaultValueTypeError(
prefix: string,
coords: string,
typeString: string,
defaultValue: string,
): Error {
return new Error(
`The ${prefix} of type "${typeString}" defined on coords "${coords}" is` +
` incompatible with the default value of "${defaultValue}".`,
);
}
export function incompatibleMergedTypesError({
actualType,
coords,
expectedType,
isArgument,
}: IncompatibleMergedTypesErrorParams): Error {
return new Error(
`Incompatible types when merging two instances of ${isArgument ? 'field argument' : FIELD} "${coords}":\n` +
` Expected type "${expectedType}" but received "${actualType}".`,
);
}
export function incompatibleInputValueDefaultValuesError(
prefix: string,
path: string,
subgraphNames: string[],
expectedDefaultValue: string,
actualDefaultValue: string,
) {
return new Error(
`Expected the ${prefix} defined on path "${path}" to define the default value "${expectedDefaultValue}".\n"` +
`However, the default value "${actualDefaultValue}" is defined in the following subgraph` +
(subgraphNames.length > 1 ? 's' : '') +
`:\n "` +
subgraphNames.join(QUOTATION_JOIN) +
`"\n` +
'If an instance defines a default value, that default value must be consistently defined across all subgraphs.',
);
}
export function incompatibleSharedEnumError(parentName: string): Error {
return new Error(
`Enum "${parentName}" was used as both an input and output but was inconsistently defined across inclusive subgraphs. To update an Enum used as both an input and output, add any new Enum values with the @inaccessible directive in the origin subgraph. Next, add those new Enum values to all other subgraphs that define the Enum—this time without the @inaccessible directive. Finally, once all subgraphs have been updated, remove @inaccessible from the Enum values in the origin subgraph.`,
);
}
export function invalidSubgraphNamesError(names: string[], invalidNameErrorMessages: string[]): Error {
let message = 'Subgraphs to be federated must each have a unique, non-empty name.';
if (names.length > 0) {
message += '\n The following subgraph names are not unique:\n "' + names.join('", "') + `"`;
}
for (const invalidNameErrorMessage of invalidNameErrorMessages) {
message += `\n ${invalidNameErrorMessage}`;
}
return new Error(message);
}
export function duplicateDirectiveDefinitionError(directiveName: string) {
return new Error(`The directive "${directiveName}" must only be defined once.`);
}
export function duplicateEnumValueDefinitionError(enumTypeName: string, valueName: string): Error {
return new Error(`The Enum "${enumTypeName}" must only define the Enum value definition "${valueName}" once.`);
}
export function duplicateFieldDefinitionError(typeString: string, typeName: string, fieldName: string): Error {
return new Error(`The ${typeString} "${typeName}" must only define the field definition "${fieldName}" once.`);
}
export function duplicateInputFieldDefinitionError(inputObjectTypeName: string, fieldName: string): Error {
return new Error(
`The Input Object "${inputObjectTypeName}" must only define the Input field definition "${fieldName}" once.`,
);
}
export function duplicateImplementedInterfaceError(typeString: string, typeName: string, interfaceName: string): Error {
return new Error(`The ${typeString} "${typeName}" must only implement the Interface "${interfaceName}" once.`);
}
export function duplicateUnionMemberDefinitionError(unionTypeName: string, memberName: string): Error {
return new Error(`The Union "${unionTypeName}" must only define the Union member "${memberName}" once.`);
}
export function duplicateTypeDefinitionError(type: string, typeName: string): Error {
return new Error(`The ${type} "${typeName}" must only be defined once.`);
}
export function duplicateOperationTypeDefinitionError(
operationTypeName: OperationTypeNode,
newTypeName: string,
oldTypeName: string,
): Error {
return new Error(
`The operation type "${operationTypeName}" cannot be defined as "${newTypeName}"` +
` because it has already been defined as "${oldTypeName}".`,
);
}
export function noBaseDefinitionForExtensionError(typeString: string, typeName: string): Error {
return new Error(
`The ${typeString} "${typeName}" is an extension,` +
` but no base ${typeString} definition of "${typeName}" is defined in any subgraph.`,
);
}
export function noBaseScalarDefinitionError(typeName: string): Error {
return new Error(
`The Scalar extension "${typeName}" is invalid because no base Scalar definition` +
` of "${typeName} is defined in the subgraph.`,
);
}
export function noDefinedUnionMembersError(unionTypeName: string): Error {
return new Error(`The Union "${unionTypeName}" must define at least one Union member.`);
}
export function noDefinedEnumValuesError(enumTypeName: string): Error {
return new Error(`The Enum "${enumTypeName}" must define at least one Enum value.`);
}
export function operationDefinitionError(typeName: string, operationType: OperationTypeNode, actualType: Kind): Error {
return new Error(
`Expected the response type "${typeName}" for operation "${operationType}" to be type Object but received "${actualType}.`,
);
}
export function invalidFieldShareabilityError(objectData: ObjectDefinitionData, invalidFieldNames: Set<string>): Error {
const parentTypeName = objectData.name;
const errorMessages: string[] = [];
for (const [fieldName, fieldData] of objectData.fieldDataByName) {
if (!invalidFieldNames.has(fieldName)) {
continue;
}
const shareableSubgraphs: string[] = [];
const nonShareableSubgraphs: string[] = [];
for (const [subgraphName, isShareable] of fieldData.isShareableBySubgraphName) {
isShareable ? shareableSubgraphs.push(subgraphName) : nonShareableSubgraphs.push(subgraphName);
}
if (shareableSubgraphs.length < 1) {
errorMessages.push(
`\n The field "${fieldName}" is defined in the following subgraphs: "${[...fieldData.subgraphNames].join(
'", "',
)}".` + `\n However, it is not declared "@shareable" in any of them.`,
);
} else {
errorMessages.push(
`\n The field "${fieldName}" is defined and declared "@shareable" in the following subgraph` +
(shareableSubgraphs.length > 1 ? 's' : '') +
`: "` +
shareableSubgraphs.join(QUOTATION_JOIN) +
`".` +
`\n However, it is not declared "@shareable" in the following subgraph` +
(nonShareableSubgraphs.length > 1 ? 's' : '') +
`: "${nonShareableSubgraphs.join(QUOTATION_JOIN)}".`,
);
}
}
return new Error(
`The Object "${parentTypeName}" defines the same fields in multiple subgraphs without the "@shareable" directive:` +
`${errorMessages.join('\n')}`,
);
}
export function undefinedDirectiveError(directiveName: string, directiveCoords: string): Error {
return new Error(
`The directive "@${directiveName}" declared on coordinates "${directiveCoords}" is not defined in the schema.`,
);
}
export function undefinedTypeError(typeName: string): Error {
return new Error(` The type "${typeName}" was referenced in the schema, but it was never defined.`);
}
export function invalidRepeatedDirectiveErrorMessage(directiveName: string): string {
return `The definition for the directive "@${directiveName}" does not define it as repeatable, but it is declared more than once on these coordinates.`;
}
export function invalidDirectiveError(
directiveName: string,
directiveCoords: string,
ordinal: string,
errorMessages: Array<string>,
): Error {
return new Error(
`The ${ordinal} instance of the directive "@${directiveName}" declared on coordinates "${directiveCoords}" is invalid for the following reason` +
(errorMessages.length > 1 ? 's:\n' : ':\n') +
errorMessages.join('\n'),
);
}
export function invalidCustomDirectiveError({
directiveCoords,
directiveName,
errors,
ordinal,
}: InvalidCustomDirectiveErrorParams) {
return new Error(
`The ${ordinal} post-federation instance of the directive "@${directiveName}" declared on coordinates` +
` "${directiveCoords}" is invalid for the following reason` +
(errors.length > 1 ? 's:\n' : ':\n - ') +
errors.map((error) => error.message).join('\n - '),
);
}
export function invalidDirectiveLocationErrorMessage(directiveName: string, location: string): string {
return ` The definition for "@${directiveName}" does not define "${location}" as a valid location.`;
}
export function invalidDirectiveLocationError({
directiveCoords,
directiveName,
location,
}: InvalidDirectiveLocationErrorParams): Error {
return new Error(
`The directive "@${directiveName}" declared on "${directiveCoords}" is invalid because the` +
` directive definition for "@${directiveName}" does not define "${location}" as a valid location.`,
);
}
export function invalidRepeatedDirectiveError({
directiveCoords,
directiveName,
}: InvalidRepeatedDirectiveErrorParams): Error {
return new Error(
`The multiple instances of directive "@${directiveName}" declared on "${directiveCoords}" are invalid because` +
` the directive definition for "@${directiveName}" does not define it as repeatable.`,
);
}
export function undefinedRequiredArgumentsError(requiredArgumentNames: Array<ArgumentName>): Error {
return new Error(
` The following ` +
requiredArgumentNames.length +
` required argument` +
(requiredArgumentNames.length > 1 ? 's are' : ' is') +
` not defined: "` +
requiredArgumentNames.join(QUOTATION_JOIN) +
`"`,
);
}
export function undefinedRequiredArgumentsErrorMessage(
directiveName: string,
requiredArgumentNames: string[],
undefinedArgumentNames: string[],
): string {
let message =
` The definition for "@${directiveName}" defines the following ` +
requiredArgumentNames.length +
` required argument` +
(requiredArgumentNames.length > 1 ? 's: ' : ': ') +
`"` +
requiredArgumentNames.join('", "') +
`"` +
`.\n However,`;
if (undefinedArgumentNames.length < 1) {
return message + ` no arguments are defined on this instance.`;
}
return (
message +
` the following required argument` +
(undefinedArgumentNames.length > 1 ? `s are` : ` is`) +
` not defined on this instance: "` +
undefinedArgumentNames.join(QUOTATION_JOIN) +
`".`
);
}
export function unexpectedArgumentProvisionError(argumentNames: Array<string>): Error {
return new Error(
`The following unexpected argument` +
(argumentNames.length > 1 ? 's are' : ' is') +
` provided: "` +
argumentNames.join(QUOTATION_JOIN) +
`".`,
);
}
export function unexpectedDirectiveArgumentErrorMessage(directiveName: string, argumentNames: string[]): string {
return (
` The definition for "@${directiveName}" does not define the following argument` +
(argumentNames.length > 1 ? 's that are' : ' that is') +
` provided: "` +
argumentNames.join(QUOTATION_JOIN) +
`".`
);
}
export function duplicateArgumentDefinitionError(argumentNames: Array<ArgumentName>): Error {
return new Error(
`The following argument` +
(argumentNames.length > 1 ? 's are' : ' is') +
` defined more than once: "` +
argumentNames.join(QUOTATION_JOIN) +
`".`,
);
}
export function duplicateDirectiveArgumentDefinitionsErrorMessage(argumentNames: string[]): string {
return (
` The following argument` +
(argumentNames.length > 1 ? 's are' : ' is') +
` defined more than once: "` +
argumentNames.join(QUOTATION_JOIN) +
`"`
);
}
export function invalidArgumentValueError({
argumentName,
expectedTypeString,
value,
}: InvalidArgumentValueErrorParams): Error {
return new Error(
` The value "${value}" provided to argument "${argumentName}" is not a valid "${expectedTypeString}" type.`,
);
}
export function invalidArgumentValueErrorMessage(
value: string,
hostName: string,
argumentName: ArgumentName,
expectedTypeString: string,
): string {
return ` The value "${value}" provided to argument "${hostName}(${argumentName}: ...)" is not a valid "${expectedTypeString}" type.`;
}
export function maximumTypeNestingExceededError(path: string): Error {
return new Error(
` The type defined at path "${path}" has more than ${MAXIMUM_TYPE_NESTING} layers of nesting,` +
` or there is a cyclical error.`,
);
}
export function unexpectedKindFatalError(typeName: string) {
return new Error(`Fatal: Unexpected type for "${typeName}"`);
}
export function incompatibleParentKindFatalError(parentTypeName: string, expectedKind: Kind, actualKind: Kind): Error {
return new Error(
`Fatal: Expected "${parentTypeName}" to be type ${kindToNodeType(expectedKind)}` +
` but received "${kindToNodeType(actualKind)}".`,
);
}
export function unexpectedEdgeFatalError(typeName: string, edgeNames: Array<string>): Error {
return new Error(
`Fatal: The type "${typeName}" visited the following unexpected edge` +
(edgeNames.length > 1 ? 's' : '') +
`:\n "${edgeNames.join(QUOTATION_JOIN)}".`,
);
}
const interfaceObject = `"Interface Object" (an "Object" type that also defines the "@interfaceObject" directive)`;
export function incompatibleParentTypeMergeError({
existingData,
incomingNodeType,
incomingSubgraphName,
}: IncompatibleParentTypeMergeErrorParams): Error {
const existingSubgraphNames = [...existingData.subgraphNames];
const nodeType = incomingNodeType ? `"${incomingNodeType}"` : interfaceObject;
return new Error(
` "${existingData.name}" is defined using incompatible types across subgraphs.` +
` It is defined as type "${kindToNodeType(existingData.kind)}" in subgraph` +
(existingSubgraphNames.length > 1 ? 's' : '') +
` "${existingSubgraphNames.join(QUOTATION_JOIN)}" but type ${nodeType} in subgraph` +
` "${incomingSubgraphName}".`,
);
}
export function unexpectedTypeNodeKindFatalError(typePath: string): Error {
return new Error(
`Fatal: Expected all constituent types at path "${typePath}" to be one of the following: ` +
`"LIST_TYPE", "NAMED_TYPE", or "NON_NULL_TYPE".`,
);
}
export function invalidKeyFatalError<K>(key: K, mapName: string): Error {
return new Error(`Fatal: Expected key "${key}" to exist in the map "${mapName}".`);
}
export function unexpectedParentKindForChildError(
parentTypeName: string,
expectedTypeString: string,
actualTypeString: string,
childName: string,
childTypeString: string,
): Error {
return new Error(
` Expected "${parentTypeName}" to be type "${expectedTypeString}" but received "${actualTypeString}"` +
` when handling child "${childName}" of type "${childTypeString}".`,
);
}
export function subgraphValidationError(subgraphName: string, errors: Error[]): Error {
return new Error(
`The subgraph "${subgraphName}" could not be federated for the following reason` +
(errors.length > 1 ? 's' : '') +
`:\n` +
errors.map((error) => error.message).join('\n'),
);
}
export const noSubgraphNameError = new Error(
`Federation could not be attempted because at least one subgraph does not define a name.`,
);
export function duplicateSubgraphNamesError(subgraphNames: Array<SubgraphName>): Error {
return new Error(
`Federation could not be attempted because the following subgraph names are defined more` +
` than once: "${subgraphNames.join(QUOTATION_JOIN)}".`,
);
}
export function invalidOperationTypeDefinitionError(
existingOperationType: OperationTypeNode,
typeName: string,
newOperationType: OperationTypeNode,
): Error {
return new Error(
`The schema definition defines the "${existingOperationType}" operation as type "${typeName}".` +
` However, "${typeName}" was also used for the "${newOperationType}" operation.\n` +
` If explicitly defined, each operation type must be a unique and valid Object type.`,
);
}
export function invalidRootTypeDefinitionError(
operationType: OperationTypeNode,
typeName: string,
defaultTypeName: string,
): Error {
return new Error(
`The schema definition defines the "${operationType}" operation as type "${typeName}".` +
` However, the schema also defines another type named "${defaultTypeName}",` +
` which is the default (root) type name for the "${operationType}" operation.\n` +
`For federation, it is only possible to use the default root types names ("Mutation", "Query", "Subscription") as` +
` operation definitions. No other definitions with these default root type names are valid.`,
);
}
export function subgraphInvalidSyntaxError(error?: Error): Error {
let message = `The subgraph has syntax errors and could not be parsed.`;
if (error) {
message += `\n The reason provided was: ` + error.message;
}
return new Error(message);
}
export function invalidInterfaceImplementationError(
parentTypeName: TypeName,
parentNodeType: NodeType,
implementationErrorsByInterfaceTypeName: Map<TypeName, ImplementationErrors>,
): Error {
const messages: string[] = [];
for (const [interfaceName, implementationErrors] of implementationErrorsByInterfaceTypeName) {
let message =
` The implementation of Interface "${interfaceName}" by "${parentTypeName}"` + ` is invalid because:\n`;
const unimplementedFieldsLength = implementationErrors.unimplementedFields.length;
if (unimplementedFieldsLength) {
message +=
` The following field${unimplementedFieldsLength > 1 ? 's are' : ' is'} not implemented: "` +
implementationErrors.unimplementedFields.join('", "') +
'"\n';
}
for (const [fieldName, invalidFieldImplementation] of implementationErrors.invalidFieldImplementations) {
const unimplementedArgumentsSize = invalidFieldImplementation.unimplementedArguments.size;
const invalidArgumentsLength = invalidFieldImplementation.invalidImplementedArguments.length;
const invalidAdditionalArgumentsSize = invalidFieldImplementation.invalidAdditionalArguments.size;
message += ` The field "${fieldName}" is invalid because:\n`;
if (unimplementedArgumentsSize) {
message +=
` The following argument${unimplementedArgumentsSize > 1 ? 's are' : ' is'} not implemented: "` +
[...invalidFieldImplementation.unimplementedArguments].join('", "') +
'"\n';
}
if (invalidArgumentsLength) {
message += ` The following implemented argument${invalidArgumentsLength > 1 ? 's are' : ' is'} invalid:\n`;
for (const invalidArgument of invalidFieldImplementation.invalidImplementedArguments) {
message +=
` The argument "${invalidArgument.argumentName}" must define type "` +
invalidArgument.expectedType +
`" and not "${invalidArgument.actualType}"\n`;
}
}
if (invalidAdditionalArgumentsSize) {
message +=
` If a field from an Interface is implemented, any additional Arguments that were not defined` +
` on the original Interface field must be optional (nullable).\n`;
message +=
` The following additional argument` +
(invalidFieldImplementation.invalidAdditionalArguments.size > 1 ? `s are` : ` is`) +
` not defined as optional: "` +
[...invalidFieldImplementation.invalidAdditionalArguments].join(`", "`) +
`"\n`;
}
if (invalidFieldImplementation.implementedResponseType) {
message +=
` The implemented response type "${invalidFieldImplementation.implementedResponseType}" is not` +
` a valid subtype (equally or more restrictive) of the response type "` +
invalidFieldImplementation.originalResponseType +
`" for "${interfaceName}.${fieldName}".\n`;
}
if (invalidFieldImplementation.isInaccessible) {
message +=
` The field has been declared "@inaccessible"; however, the same field has not been declared "@inaccessible"` +
` on the Interface definition.\n Consequently, the Interface implementation cannot be satisfied.\n`;
}
}
messages.push(message);
}
return new Error(
`The ${parentNodeType} "${parentTypeName}" has the following Interface implementation errors:\n` +
messages.join('\n'),
);
}
export function invalidRequiredInputValueError(
typeString: string,
path: string,
errors: InvalidRequiredInputValueData[],
isArgument = true,
): Error {
const inputValueTypeString = isArgument ? ARGUMENT : INPUT_FIELD;
let message = `The ${typeString} "${path}" could not be federated because:\n`;
for (const error of errors) {
message +=
` The ${inputValueTypeString} "${error.inputValueName}" is required in the following subgraph` +
(error.requiredSubgraphs.length > 1 ? 's' : '') +
': "' +
error.requiredSubgraphs.join(`", "`) +
`"\n` +
` However, the ${inputValueTypeString} "${error.inputValueName}" is not defined in the following subgraph` +
(error.missingSubgraphs.length > 1 ? 's' : '') +
': "' +
error.missingSubgraphs.join(`", "`) +
`"\n` +
` If an ${inputValueTypeString} is required on a ${typeString} in any one subgraph, it must be at least defined` +
` as optional on all other definitions of that ${typeString} in all other subgraphs.\n`;
}
return new Error(message);
}
export function duplicateArgumentsError(fieldPath: string, duplicatedArguments: string[]): Error {
return new Error(
`The field "${fieldPath}" is invalid because:\n` +
` The following argument` +
(duplicatedArguments.length > 1 ? 's are' : ' is') +
` defined more than once: "` +
duplicatedArguments.join(QUOTATION_JOIN) +
`"\n`,
);
}
export function noQueryRootTypeError(isRouterSchema = true): Error {
return new Error(
`The ${isRouterSchema ? 'router' : 'client'} schema does not define at least one accessible query root` +
` type field after federation was completed, which is necessary for a federated graph to be valid.\n` +
` For example:\n` +
` type Query {\n` +
` dummy: String\n` +
` }`,
);
}
export const inaccessibleQueryRootTypeError = new Error(
`The root query type "Query" must be present in the client schema;` +
` consequently, it must not be declared "@inaccessible".`,
);
export function expectedEntityError(typeName: string): Error {
return new Error(`Expected object "${typeName}" to define a "key" directive, but it defines no directives.`);
}
export const inlineFragmentInFieldSetErrorMessage = ` Inline fragments are not currently supported within a field set argument.`;
export function abstractTypeInKeyFieldSetErrorMessage(
fieldSet: string,
fieldCoords: string,
abstractTypeName: string,
abstractTypeString: string,
): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because "${fieldCoords}" returns "${abstractTypeName}", which is type "${abstractTypeString}".\n` +
` Fields that return abstract types (Interfaces and Unions)` +
` cannot be included in the field set of "@key" directives.`
);
}
export function unknownTypeInFieldSetErrorMessage(
fieldSet: string,
fieldCoords: string,
responseTypeName: string,
): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because "${fieldCoords}" returns the unknown type "${responseTypeName}".`
);
}
export function invalidSelectionSetErrorMessage(
fieldSet: string,
fieldCoordinatesPath: Array<string>,
selectionSetTypeName: string,
fieldTypeString: string,
): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because of the selection set corresponding to the ` +
getSelectionSetLocationWithTypeString(fieldCoordinatesPath, selectionSetTypeName, fieldTypeString) +
` Composite types such as "${fieldTypeString}" types must define a selection set with at least one field selection.`
);
}
export function invalidSelectionSetDefinitionErrorMessage(
fieldSet: string,
fieldCoordinatesPath: Array<string>,
selectionSetTypeName: string,
fieldTypeString: string,
): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because of the selection set corresponding to the ` +
getSelectionSetLocationWithTypeString(fieldCoordinatesPath, selectionSetTypeName, fieldTypeString) +
` Non-composite types such as "${fieldTypeString}" cannot define a selection set.`
);
}
export function undefinedFieldInFieldSetErrorMessage(
fieldSet: string,
parentTypeName: string,
fieldName: string,
): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because of the selection set corresponding to the field coordinates "${parentTypeName}.${fieldName}".\n` +
` The type "${parentTypeName}" does not define a field named "${fieldName}".`
);
}
export function unparsableFieldSetErrorMessage(fieldSet: string, error?: Error): string {
let message = ` The following field set is invalid:\n "${fieldSet}"\n` + ` The field set could not be parsed.`;
if (error) {
message += `\n The reason provided was: ` + error.message;
}
return message;
}
export function unparsableFieldSetSelectionErrorMessage(fieldSet: string, fieldName: string): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because the selection set defined on "${fieldName}" could not be parsed.`
);
}
export function undefinedCompositeOutputTypeError(parentTypeName: string): Error {
return new Error(` Expected an object/interface or object/interface extension named "${parentTypeName}" to exist.`);
}
export function unexpectedArgumentErrorMessage(fieldSet: string, fieldPath: string, argumentName: string): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because "${fieldPath}" does not define an argument named "${argumentName}".`
);
}
export function argumentsInKeyFieldSetErrorMessage(fieldSet: string, fieldPath: string): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because "${fieldPath}" defines arguments.\n` +
` Fields that define arguments cannot be included in the field set of @key directives.`
);
}
export function invalidProvidesOrRequiresDirectivesError(directiveName: string, errorMessages: string[]): Error {
return new Error(
`The following "${directiveName}" directive` +
(errorMessages.length > 1 ? 's are' : ' is') +
` invalid:\n` +
errorMessages.join(`\n`),
);
}
export function duplicateFieldInFieldSetErrorMessage(fieldSet: string, fieldPath: string): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because "${fieldPath}" was included in the field set more than once.`
);
}
export function incompatibleTypeWithProvidesErrorMessage({
fieldCoords,
responseType,
subgraphName,
}: IncompatibleTypeWithProvidesErrorMessageParams): string {
return (
` A "@provides" directive is declared on field "${fieldCoords}" in subgraph "${subgraphName}".\n` +
` However, the response type "${responseType}" is not an Object nor Interface.`
);
}
function getSelectionSetLocation(
fieldCoordinatesPath: Array<string>,
selectionSetTypeName: string,
withReturnType: boolean = false,
): string {
/* fieldCoordinatesPath can have length 0 if it's a @requires directive,
* in which case the first part of the field set refers to the enclosing parent type.
* */
if (fieldCoordinatesPath.length < 1) {
return `enclosing type name "${selectionSetTypeName}".\n`;
}
return (
`field coordinates "${fieldCoordinatesPath[fieldCoordinatesPath.length - 1]}"` +
(withReturnType ? ` that returns "${selectionSetTypeName}"` : '') +
`.\n`
);
}
function getSelectionSetLocationWithTypeString(
fieldCoordinatesPath: Array<string>,
selectionSetTypeName: string,
typeString: string,
): string {
/* fieldCoordinatesPath can have length 0 if it's a @requires directive,
* in which case the first part of the field set refers to the enclosing parent type.
* */
if (fieldCoordinatesPath.length < 1) {
return `enclosing type name "${selectionSetTypeName}", which is type "${typeString}".\n`;
}
return (
`field coordinates "${fieldCoordinatesPath[fieldCoordinatesPath.length - 1]}"` +
` that returns "${selectionSetTypeName}", which is type "${typeString}".\n`
);
}
export function invalidInlineFragmentTypeErrorMessage(
fieldSet: string,
fieldCoordinatesPath: Array<string>,
typeConditionName: string,
selectionSetTypeName: string,
): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because an inline fragment with the type condition "${typeConditionName}" is defined on the` +
` selection set corresponding to the ` +
getSelectionSetLocation(fieldCoordinatesPath, selectionSetTypeName, true) +
` However, "${selectionSetTypeName}" is not an abstract (Interface or Union) type.\n` +
` Consequently, the only valid type condition at this selection set would be "${selectionSetTypeName}".`
);
}
export function inlineFragmentWithoutTypeConditionErrorMessage(fieldSet: string, fieldPath: string): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because "${fieldPath}" defines an inline fragment without a type condition.`
);
}
export function unknownInlineFragmentTypeConditionErrorMessage(
fieldSet: string,
fieldCoordinatesPath: Array<string>,
selectionSetTypeName: string,
typeConditionName: string,
): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because an inline fragment with the unknown type condition "${typeConditionName}" is defined on the` +
` selection set corresponding to the ` +
getSelectionSetLocation(fieldCoordinatesPath, selectionSetTypeName)
);
}
export function invalidInlineFragmentTypeConditionTypeErrorMessage(
fieldSet: string,
fieldCoordinatesPath: Array<string>,
selectionSetTypeName: string,
typeConditionName: string,
typeConditionTypeString: string,
): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because an inline fragment with the type condition "${typeConditionName}" is defined on the` +
` selection set corresponding to the ` +
getSelectionSetLocation(fieldCoordinatesPath, selectionSetTypeName) +
` However, "${typeConditionName}" is type "${typeConditionTypeString}" when types "Interface" or "Object" would` +
` be expected.`
);
}
export function invalidInlineFragmentTypeConditionErrorMessage(
fieldSet: string,
fieldCoordinatesPath: Array<string>,
typeConditionName: string,
parentTypeString: string,
selectionSetTypeName: string,
): string {
const message =
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because an inline fragment with the type condition "${typeConditionName}" is defined on the` +
` selection set corresponding to the ` +
getSelectionSetLocationWithTypeString(fieldCoordinatesPath, selectionSetTypeName, parentTypeString);
if (parentTypeString === INTERFACE) {
return message + ` However, "${typeConditionName}" does not implement "${selectionSetTypeName}"`;
}
return message + ` However, "${typeConditionName}" is not a member of "${selectionSetTypeName}".`;
}
export function invalidSelectionOnUnionErrorMessage(
fieldSet: string,
fieldCoordinatesPath: Array<string>,
selectionSetTypeName: string,
): string {
return (
` The following field set is invalid:\n "${fieldSet}"\n` +
` This is because of the selection set corresponding to the ` +
getSelectionSetLocationWithTypeString(fieldCoordinatesPath, selectionSetTypeName, UNION) +
` Union types such as "${selectionSetTypeName}" must define field selections (besides "__typename") on an` +
` inline fragment whose type condition corresponds to a constituent union member.`
);
}
export function duplicateOverriddenFieldErrorMessage(fieldPath: string, subgraphNames: string[]): string {
return (
` The field "${fieldPath}" declares an @override directive in the following subgraphs: "` +
subgraphNames.join(QUOTATION_JOIN) +
`".`
);
}
export function duplicateOverriddenFieldsError(errorMessages: string[]): Error {
return new Error(
`The "@override" directive must only be declared on one single instance of a field.` +
` However, an "@override" directive was declared on more than one instance of the following field` +
(errorMessages.length > 1 ? 's' : '') +
`: "` +
errorMessages.join(QUOTATION_JOIN) +
`".\n`,
);
}
export function noFieldDefinitionsError(typeString: string, typeName: string): Error {
return new Error(`The ${typeString} "${typeName}" is invalid because it does not define any fields.`);
}
export function noInputValueDefinitionsError(inputTypeName: string): Error {
return new Error(`The Input Object "${inputTypeName}" is invalid because it does not define any input values.`);
}
export function allChildDefinitionsAreInaccessibleError(
typeString: string,
typeName: string,
childType: string,
): Error {
return new Error(
`The ${typeString} "${typeName}" is invalid because all its ${childType} definitions are declared "@inaccessible".`,
);
}
export function equivalentSourceAndTargetOverrideErrorMessage(subgraphName: string, hostPath: string): string {
return `Cannot override field "${hostPath}" because the source and target subgraph names are both "${subgraphName}"`;
}
export function undefinedEntityInterfaceImplementationsError(
invalidEntityInterfacesByTypeName: Map<string, InvalidEntityInterface[]>,
entityInterfaceFederationDataByTypeName: Map<string, EntityInterfaceFederationData>,
): Error {
let message =
`Federation was unsuccessful because any one subgraph that defines a specific entity Interface` +
` must also define each and every entity Object that implements that entity Interface.\n` +
`Each entity Object must also explicitly define its implementation of the entity Interface.\n`;
for (const [typeName, undefinedImplementations] of invalidEntityInterfacesByTypeName) {
const entityInterfaceDatas = getOrThrowError(
entityInterfaceFederationDataByTypeName,
typeName,
'entityInterfaceFederationDataByTypeName',
);
const implementedConcreteTypeNames = entityInterfaceDatas.concreteTypeNames!;
message +=
` Across all subgraphs, the entity interface "${typeName}" is implemented by the following entit` +
(implementedConcreteTypeNames.size > 1 ? `ies` : `y`) +
`:\n "` +
Array.from(implementedConcreteTypeNames).join(QUOTATION_JOIN) +
`"\n` +
` However, the definition of at least one of these implementations is missing in a subgraph that` +
` defines the entity interface "${typeName}":\n`;
for (const { subgraphName, definedConcreteTypeNames } of undefinedImplementations) {
const disparities = getEntriesNotInHashSet(implementedConcreteTypeNames, definedConcreteTypeNames);
message +=
` Subgraph "${subgraphName}" does not define the following implementations: "` +
disparities.join(QUOTATION_JOIN) +
`"\n`;
}
}
return new Error(message);
}
export function orScopesLimitError(maxOrScopes: number, directiveCoords: string[]): Error {
return new Error(
`The maximum number of OR scopes that can be defined by @requiresScopes on a single field is ${maxOrScopes}.` +
` However, the following coordinates attempt to define more:\n "` +
directiveCoords.join(QUOTATION_JOIN) +
`"\nIf you require more, please contact support.`,
);
}
export function invalidEventDrivenGraphError(errorMessages: string[]): Error {
return new Error(
`An "Event Driven" graph—a subgraph that defines event driven directives—must not define any resolvers.\n` +
`Consequently, any "@key" definitions must also include the "resolvable: false" argument.\n` +
`Moreover, only fields that compose part of an entity's (composite) key and are` +
` declared "@external" are permitted.\n` +
errorMessages.join('\n'),
);
}