-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathValidateRequiredArgumentsTransform.js
125 lines (114 loc) · 3.29 KB
/
ValidateRequiredArgumentsTransform.js
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';
const GraphQLIRValidator = require('../core/GraphQLIRValidator');
const {createUserError} = require('../core/RelayCompilerError');
const {getFieldDefinitionStrict} = require('../core/getFieldDefinition');
import type GraphQLCompilerContext from '../core/GraphQLCompilerContext';
import type {
Connection,
Directive,
Field,
Fragment,
Root,
SplitOperation,
} from '../core/GraphQLIR';
import type {Schema, TypeID, FieldArgument} from '../core/Schema';
type State = {|
+rootNode: Fragment | Root | SplitOperation,
+parentType: TypeID,
|};
/*
* Validate required arguments are provided after transforms filling in arguments
*/
function validateRequiredArguments(
context: GraphQLCompilerContext,
): GraphQLCompilerContext {
GraphQLIRValidator.validate(
context,
{
Directive: visitDirective,
ConnectionField: visitField,
InlineFragment: visitInlineFragment,
LinkedField: visitField,
ScalarField: visitField,
// FragmentSpread validation is done in ApplyFragmentArgumentTransform
},
node => ({rootNode: node, parentType: node.type}),
);
return context;
}
function visitDirective(node: Directive, {rootNode}: State): void {
const context: GraphQLCompilerContext = this.getContext();
const directiveDef = context.getSchema().getDirective(node.name);
if (directiveDef == null) {
return;
}
validateRequiredArgumentsOnNode(
context.getSchema(),
node,
directiveDef.args,
rootNode,
);
}
function visitInlineFragment(fragment, {rootNode}: State): void {
this.traverse(fragment, {
rootNode,
parentType: fragment.typeCondition,
});
}
function visitField(node: Field, {parentType, rootNode}: State): void {
const context: GraphQLCompilerContext = this.getContext();
const schema = context.getSchema();
const definition = getFieldDefinitionStrict(schema, parentType, node.name);
if (definition == null) {
const isLegacyFatInterface = node.directives.some(
directive => directive.name === 'fixme_fat_interface',
);
if (!isLegacyFatInterface) {
throw createUserError(
`Unknown field '${node.name}' on type ` +
`'${schema.getTypeString(parentType)}'.`,
[node.loc],
);
}
} else {
validateRequiredArgumentsOnNode(
schema,
node,
schema.getFieldConfig(definition).args,
rootNode,
);
}
this.traverse(node, {
rootNode,
parentType: node.type,
});
}
function validateRequiredArgumentsOnNode(
schema: Schema,
node: Connection | Directive | Field,
definitionArgs: $ReadOnlyArray<FieldArgument>,
rootNode: Fragment | Root | SplitOperation,
): void {
const nodeArgsSet = new Set(node.args.map(arg => arg.name));
for (const arg of definitionArgs) {
if (schema.isNonNull(arg.type) && !nodeArgsSet.has(arg.name)) {
throw createUserError(
`Required argument '${arg.name}: ${schema.getTypeString(arg.type)}' ` +
`is missing on '${node.name}' in '${rootNode.name}'.`,
[node.loc, rootNode.loc],
);
}
}
}
module.exports = {
transform: validateRequiredArguments,
};