Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'isRequredArgument' & 'isRequredInputField' predicates #1465

Merged
merged 1 commit into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add 'isRequredArgument' & 'isRequredInputField' predicates
  • Loading branch information
IvanGoncharov committed Aug 16, 2018
commit 504ac62d0612cd1a6dd18c8f53f578aac4dcb9e6
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
) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They make code more readable.

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