Skip to content

Commit 36dc149

Browse files
Add 'isRequredArgument' & 'isRequredInputField' predicates (#1465)
1 parent 7cfd686 commit 36dc149

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export {
9898
isWrappingType,
9999
isNullableType,
100100
isNamedType,
101+
isRequiredArgument,
102+
isRequiredInputField,
101103
isSpecifiedScalarType,
102104
isIntrospectionType,
103105
isSpecifiedDirective,

src/type/definition.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,10 @@ export type GraphQLArgument = {
858858
astNode?: ?InputValueDefinitionNode,
859859
};
860860
861+
export function isRequiredArgument(arg: GraphQLArgument): boolean %checks {
862+
return isNonNullType(arg.type) && arg.defaultValue === undefined;
863+
}
864+
861865
export type GraphQLFieldMap<TSource, TContext> = ObjMap<
862866
GraphQLField<TSource, TContext>,
863867
>;
@@ -1273,4 +1277,10 @@ export type GraphQLInputField = {
12731277
astNode?: ?InputValueDefinitionNode,
12741278
};
12751279

1280+
export function isRequiredInputField(
1281+
field: GraphQLInputField,
1282+
): boolean %checks {
1283+
return isNonNullType(field.type) && field.defaultValue === undefined;
1284+
}
1285+
12761286
export type GraphQLInputFieldMap = ObjMap<GraphQLInputField>;

src/type/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export {
3535
isWrappingType,
3636
isNullableType,
3737
isNamedType,
38+
isRequiredArgument,
39+
isRequiredInputField,
3840
// Assertions
3941
assertType,
4042
assertScalarType,

src/validation/rules/ProvidedRequiredArguments.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { ValidationContext } from '../ValidationContext';
1111
import { GraphQLError } from '../../error/GraphQLError';
1212
import inspect from '../../jsutils/inspect';
1313
import keyMap from '../../jsutils/keyMap';
14-
import { isNonNullType } from '../../type/definition';
14+
import { isRequiredArgument } from '../../type/definition';
1515
import type { ASTVisitor } from '../../language/visitor';
1616

1717
export function missingFieldArgMessage(
@@ -58,11 +58,7 @@ export function ProvidedRequiredArguments(
5858
const argNodeMap = keyMap(argNodes, arg => arg.name.value);
5959
for (const argDef of fieldDef.args) {
6060
const argNode = argNodeMap[argDef.name];
61-
if (
62-
!argNode &&
63-
isNonNullType(argDef.type) &&
64-
argDef.defaultValue === undefined
65-
) {
61+
if (!argNode && isRequiredArgument(argDef)) {
6662
context.reportError(
6763
new GraphQLError(
6864
missingFieldArgMessage(
@@ -90,11 +86,7 @@ export function ProvidedRequiredArguments(
9086
const argNodeMap = keyMap(argNodes, arg => arg.name.value);
9187
for (const argDef of directiveDef.args) {
9288
const argNode = argNodeMap[argDef.name];
93-
if (
94-
!argNode &&
95-
isNonNullType(argDef.type) &&
96-
argDef.defaultValue === undefined
97-
) {
89+
if (!argNode && isRequiredArgument(argDef)) {
9890
context.reportError(
9991
new GraphQLError(
10092
missingDirectiveArgMessage(

src/validation/rules/ValuesOfCorrectType.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
isInputObjectType,
1919
isListType,
2020
isNonNullType,
21+
isRequiredInputField,
2122
getNullableType,
2223
getNamedType,
2324
} from '../../type/definition';
@@ -97,16 +98,12 @@ export function ValuesOfCorrectType(context: ValidationContext): ASTVisitor {
9798
const fieldNodeMap = keyMap(node.fields, field => field.name.value);
9899
for (const fieldName of Object.keys(inputFields)) {
99100
const fieldDef = inputFields[fieldName];
100-
const fieldType = fieldDef.type;
101101
const fieldNode = fieldNodeMap[fieldName];
102-
if (
103-
!fieldNode &&
104-
isNonNullType(fieldType) &&
105-
fieldDef.defaultValue === undefined
106-
) {
102+
if (!fieldNode && isRequiredInputField(fieldDef)) {
103+
const typeStr = inspect(fieldDef.type);
107104
context.reportError(
108105
new GraphQLError(
109-
requiredFieldMessage(type.name, fieldName, inspect(fieldType)),
106+
requiredFieldMessage(type.name, fieldName, typeStr),
110107
node,
111108
),
112109
);

0 commit comments

Comments
 (0)