Skip to content
New issue

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

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

Already on GitHub? # to your account

Add 'isRequredArgument' & 'isRequredInputField' predicates #1465

Merged
merged 1 commit into from
Aug 17, 2018

Conversation

IvanGoncharov
Copy link
Member

No description provided.

!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.

Copy link
Contributor

@mjmahone mjmahone left a comment

Choose a reason for hiding this comment

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

@IvanGoncharov is this something you're planning on re-using, or is it just to make this one location more readable?

I don't think it drastically improves readability, and there's some danger that isRequiredArgument would be implemented for a VariableDefinition instead of inside the Schema's Argument definition.

If you're going to use this elsewhere, it makes sense, otherwise we should leave things as-is.

@Marak
Copy link

Marak commented Aug 16, 2018

required, not requred?

@IvanGoncharov
Copy link
Member Author

IvanGoncharov commented Aug 16, 2018

@Marak Thanks a lot 👍 Fixed.

is this something you're planning on re-using, or is it just to make this one location more readable?

@mjmahone It's actually a very very painful topic. Initially I didn't want to overburden you with technical details but here is a full story.

So before graphql/graphql-spec#418 required === NonNull but after this change, you also need to check defaultValue. Lee made necessary changes in all major places including validation rules in #1274. But many other places including all external projects still assume that required === NonNull.

That makes me pay extra attention to all isNonNullType/isNullableType checks and at some point, we need to check all such places and fix all remaining bugs.
For example, here are a few places that are easy to guess:

if (isNonNullType(newArgDef.type)) {
breakingChanges.push({
type: BreakingChangeType.NON_NULL_ARG_ADDED,
description:
`A non-null arg ${newArgDef.name} on ` +
`${newType.name}.${fieldName} was added`,
});
} else {

Adding non-null argument with a default value is not a breaking change.

if (!ifaceArg && isNonNullType(objectArg.type)) {
context.reportError(
`Object field argument ${object.name}.${fieldName}(${argName}:) ` +
`is of required type ${inspect(objectArg.type)} but is not also ` +
`provided by the Interface field ${iface.name}.${fieldName}.`,
[
getFieldArgTypeNode(object, fieldName, argName),
getFieldNode(iface, fieldName),
],
);

Same problem here since according to the spec:
http://facebook.github.io/graphql/June2018/#sec-Objects

The object field may include additional arguments not defined in the interface field, but any additional argument must not be required, e.g. must not be of a non‐nullable type.

And probably many more places since
So these functions are not strictly necessary but they solve a lot of confusion and motivate people who don't necessarily know about graphql/graphql-spec#418 change to use correct check in their code.

@IvanGoncharov
Copy link
Member Author

@mjmahone I just remembered that Lee made undefined a valid value for enums in b013e73 and that mean field.defaultValue === undefined is invalid check and instead it should use hasOwnProperty simular to this place:

value: value.hasOwnProperty('value') ? value.value : valueName,

But I can't use hasOwnProperty since a lot of place incorrectly assign undefined to defaultValue. For example:

defaultValue: valueFromAST(value.defaultValue, type),

I think it's even bigger reason to use dedicated function for this check.

Copy link
Contributor

@mjmahone mjmahone left a comment

Choose a reason for hiding this comment

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

@IvanGoncharov that makes sense, thank you for the backstory: it makes the motivation clear and agreement that this is desirable easy to reach.

@IvanGoncharov IvanGoncharov merged commit 36dc149 into graphql:master Aug 17, 2018
@IvanGoncharov IvanGoncharov deleted the requiredPredicates branch August 17, 2018 19:02
ganny26 added a commit to ganny26/graphql-js that referenced this pull request Aug 27, 2018
 Add 'isRequredArgument' & 'isRequredInputField' predicates (graphql#1465)
IvanGoncharov added a commit to IvanGoncharov/graphql-js that referenced this pull request Aug 29, 2018
IvanGoncharov added a commit that referenced this pull request Aug 29, 2018
vladar added a commit to webonyx/graphql-php that referenced this pull request Jun 19, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants