forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeferStreamDirectiveLabelRule.ts
More file actions
60 lines (55 loc) · 1.82 KB
/
Copy pathDeferStreamDirectiveLabelRule.ts
File metadata and controls
60 lines (55 loc) · 1.82 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
import { GraphQLError } from '../../error/GraphQLError.js';
import type { DirectiveNode } from '../../language/ast.js';
import { Kind } from '../../language/kinds.js';
import type { ASTVisitor } from '../../language/visitor.js';
import {
GraphQLDeferDirective,
GraphQLStreamDirective,
} from '../../type/directives.js';
import type { ValidationContext } from '../ValidationContext.js';
/**
* Defer and stream directive labels are unique
*
* A GraphQL document is only valid if defer and stream directives' label argument is static and unique.
*/
export function DeferStreamDirectiveLabelRule(
context: ValidationContext,
): ASTVisitor {
const knownLabels = new Map<string, DirectiveNode>();
return {
Directive(node) {
if (
node.name.value === GraphQLDeferDirective.name ||
node.name.value === GraphQLStreamDirective.name
) {
const labelArgument = node.arguments?.find(
(arg) => arg.name.value === 'label',
);
const labelValue = labelArgument?.value;
if (!labelValue || labelValue.kind === Kind.NULL) {
return;
}
if (labelValue.kind !== Kind.STRING) {
context.reportError(
new GraphQLError(
`Argument "@${node.name.value}(label:)" must be a static string.`,
{ nodes: node },
),
);
return;
}
const knownLabel = knownLabels.get(labelValue.value);
if (knownLabel != null) {
context.reportError(
new GraphQLError(
'Value for arguments "defer(label:)" and "stream(label:)" must be unique across all Defer/Stream directive usages.',
{ nodes: [knownLabel, node] },
),
);
} else {
knownLabels.set(labelValue.value, node);
}
}
},
};
}