Skip to content

Commit

Permalink
Add 'isRequredArgument' & 'isRequredInputField' predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Aug 16, 2018
1 parent 7cfd686 commit 504ac62
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export {
isWrappingType,
isNullableType,
isNamedType,
isRequiredArgument,
isRequiredInputField,
isSpecifiedScalarType,
isIntrospectionType,
isSpecifiedDirective,
Expand Down
10 changes: 10 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,10 @@ export type GraphQLArgument = {
astNode?: ?InputValueDefinitionNode,
};

export function isRequiredArgument(arg: GraphQLArgument): boolean %checks {
return isNonNullType(arg.type) && arg.defaultValue === undefined;
}

export type GraphQLFieldMap<TSource, TContext> = ObjMap<
GraphQLField<TSource, TContext>,
>;
Expand Down Expand Up @@ -1273,4 +1277,10 @@ export type GraphQLInputField = {
astNode?: ?InputValueDefinitionNode,
};

export function isRequiredInputField(
field: GraphQLInputField,
): boolean %checks {
return isNonNullType(field.type) && field.defaultValue === undefined;
}

export type GraphQLInputFieldMap = ObjMap<GraphQLInputField>;
2 changes: 2 additions & 0 deletions src/type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export {
isWrappingType,
isNullableType,
isNamedType,
isRequiredArgument,
isRequiredInputField,
// Assertions
assertType,
assertScalarType,
Expand Down
14 changes: 3 additions & 11 deletions src/validation/rules/ProvidedRequiredArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ValidationContext } from '../ValidationContext';
import { GraphQLError } from '../../error/GraphQLError';
import inspect from '../../jsutils/inspect';
import keyMap from '../../jsutils/keyMap';
import { isNonNullType } from '../../type/definition';
import { isRequiredArgument } from '../../type/definition';
import type { ASTVisitor } from '../../language/visitor';

export function missingFieldArgMessage(
Expand Down Expand Up @@ -58,11 +58,7 @@ export function ProvidedRequiredArguments(
const argNodeMap = keyMap(argNodes, arg => arg.name.value);
for (const argDef of fieldDef.args) {
const argNode = argNodeMap[argDef.name];
if (
!argNode &&
isNonNullType(argDef.type) &&
argDef.defaultValue === undefined
) {
if (!argNode && isRequiredArgument(argDef)) {
context.reportError(
new GraphQLError(
missingFieldArgMessage(
Expand Down Expand Up @@ -90,11 +86,7 @@ export function ProvidedRequiredArguments(
const argNodeMap = keyMap(argNodes, arg => arg.name.value);
for (const argDef of directiveDef.args) {
const argNode = argNodeMap[argDef.name];
if (
!argNode &&
isNonNullType(argDef.type) &&
argDef.defaultValue === undefined
) {
if (!argNode && isRequiredArgument(argDef)) {
context.reportError(
new GraphQLError(
missingDirectiveArgMessage(
Expand Down
11 changes: 4 additions & 7 deletions src/validation/rules/ValuesOfCorrectType.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isInputObjectType,
isListType,
isNonNullType,
isRequiredInputField,
getNullableType,
getNamedType,
} from '../../type/definition';
Expand Down Expand Up @@ -97,16 +98,12 @@ export function ValuesOfCorrectType(context: ValidationContext): ASTVisitor {
const fieldNodeMap = keyMap(node.fields, field => field.name.value);
for (const fieldName of Object.keys(inputFields)) {
const fieldDef = inputFields[fieldName];
const fieldType = fieldDef.type;
const fieldNode = fieldNodeMap[fieldName];
if (
!fieldNode &&
isNonNullType(fieldType) &&
fieldDef.defaultValue === undefined
) {
if (!fieldNode && isRequiredInputField(fieldDef)) {
const typeStr = inspect(fieldDef.type);
context.reportError(
new GraphQLError(
requiredFieldMessage(type.name, fieldName, inspect(fieldType)),
requiredFieldMessage(type.name, fieldName, typeStr),
node,
),
);
Expand Down

0 comments on commit 504ac62

Please sign in to comment.