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

Better Predicates #1137

Merged
merged 1 commit into from
Dec 12, 2017
Merged

Better Predicates #1137

merged 1 commit into from
Dec 12, 2017

Conversation

leebyron
Copy link
Contributor

@leebyron leebyron commented Dec 12, 2017

Introduces new predicate and assertion functions for each kind of type mirroring the existing ones for the higher order types. It also includes predicates for directives and schema. This should remove the need to use instanceof in any usage of GraphQL.js

This consolidates the checking of instances which generalizes the cross-realm/multiple-module check so that the clearer error message is provided in nearly all scenarios, reducing confusion.

This also replaces nearly all instanceof with new predicate functions internally.

Fixes #1134
Closes #1016

Introduces new predicate and assertion functions for each kind of type mirroring the existing ones for the higher order types. It also includes predicates for directives and schema. This should remove the need to use `instanceof` in any usage of GraphQL.js

This consolidates the checking of instances which generalizes the cross-realm/multiple-module check so that the clearer error message is provided in nearly all scenarios, reducing confusion.

This also replaces nearly all `instanceof` with new predicate functions internally.

Fixes #1134
@leebyron leebyron merged commit 2adbe22 into master Dec 12, 2017
@leebyron leebyron deleted the better-predicates branch December 12, 2017 04:10
throw new Error(
`Cannot use ${constructor.name} "${value}" from another module or realm.

Ensure that there is only one instance of "graphql" in the node_modules
Copy link
Member

Choose a reason for hiding this comment

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

@leebyron Not sure if anybody still using alternative package managers(e.g. bower) but maybe it worth to prefix this string with "If you are using NPM package" to reduce possible confusion.

export function assertNonNullType(type: mixed): GraphQLNonNull<any> {
invariant(
isNonNullType(type),
`Expected ${String(type)} to be a GraphQL Non-Null type.`,
Copy link
Member

Choose a reason for hiding this comment

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

@leebyron Maybe it worth to say something like to be wrapped in?
Same goes for List.

return type;
}

declare function isNonNullType(type: mixed): boolean %checks(type instanceof
Copy link
Member

Choose a reason for hiding this comment

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

@leebyron Since antagonist function called isNullableType maybe it worth to rename this one to isNonNullableType?

export function assertWrappingType(type: mixed): GraphQLWrappingType {
invariant(
isWrappingType(type),
`Expected ${String(type)} to be a GraphQL wrapping type.`,
Copy link
Member

@IvanGoncharov IvanGoncharov Dec 12, 2017

Choose a reason for hiding this comment

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

@leebyron Since this error messages is intended for developers who just use public API they can be unfamiliar with what wrapping type means. Maybe it worth to replace it with something like to be a type wrapped by either NonNull or List wrapper?
Similarly, other error messages will benefit from replacing composite terms like leaf type, abstract type, composite type, etc.

type instanceof GraphQLScalarType || type instanceof GraphQLEnumType,
'Must provide Input Type, cannot use: ' + String(type),
);
if (isScalarType(type) || isEnumType(type)) {
Copy link
Member

Choose a reason for hiding this comment

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

@leebyron Can it be replaced with isLeafType?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants