Skip to content

Use invariants instead of throwing Errors #1457

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

Merged

Conversation

IvanGoncharov
Copy link
Member

No description provided.

@@ -116,9 +117,7 @@ export function buildASTSchema(
ast: DocumentNode,
options?: BuildSchemaOptions,
): GraphQLSchema {
if (!ast || ast.kind !== Kind.DOCUMENT) {
throw new Error('Must provide a document ast.');
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Make it similar to this:

invariant(
documentAST && documentAST.kind === Kind.DOCUMENT,
'Must provide valid Document AST',
);

@IvanGoncharov IvanGoncharov force-pushed the invariantsInsteadOfThrow branch from c05f897 to c5b7990 Compare August 8, 2018 14:36
@@ -342,7 +344,7 @@ export class ASTDefinitionBuilder {
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
return this._makeInputObjectDef(def);
default:
throw new Error(`Type kind "${def.kind}" not supported.`);
invariant(false, `Type kind "${def.kind}" not supported.`);
Copy link
Contributor

Choose a reason for hiding this comment

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

While the above case should probably be invariant, this one should almost definitely me throw new GraphQLError: you've got a type definition that somehow parsed but at validate time is invalid. In this instance it's very helpful to see where in your document that error actually is.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mjmahone You right 👍 especially tanking into account possible Experimental features.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mjmahone Fixed.

@IvanGoncharov IvanGoncharov force-pushed the invariantsInsteadOfThrow branch from c5b7990 to 22ec55b Compare August 8, 2018 15:05
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.

Approving, but we should also fix the throw Error to throw new GraphQLError. In general, if we have an AST node we should be throwing GraphQLError instead of Error or invariant, unless the issue is caused by tooling built on top of graphql-js making a mistake, rather than someone using a GraphQL document that won't work.

@IvanGoncharov IvanGoncharov merged commit bce300f into graphql:master Aug 8, 2018
@IvanGoncharov IvanGoncharov deleted the invariantsInsteadOfThrow branch August 10, 2018 14:49
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