Skip to content

Commit

Permalink
Craft: use createUserError for unknown types (facebook#2686)
Browse files Browse the repository at this point in the history
Summary:
This improves the compiler error message when an unknown type is used.
Pull Request resolved: facebook#2686

Reviewed By: jstejada

Differential Revision: D14454331

Pulled By: kassens

fbshipit-source-id: 6d2d2d00ae4b547288281bce88c8c163867f1f00
  • Loading branch information
kassens authored and facebook-github-bot committed Mar 14, 2019
1 parent e69c9b7 commit 825605a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
12 changes: 0 additions & 12 deletions packages/relay-compiler/core/GraphQLSchemaUtils.js
Expand Up @@ -228,24 +228,12 @@ function assertTypeWithFields(
return type;
}

/**
* Helper for calling `typeFromAST()` with a clear warning when the type does
* not exist. This enables the pattern `assertXXXType(getTypeFromAST(...))`,
* emitting distinct errors for unknown types vs types of the wrong category.
*/
function getTypeFromAST(schema: GraphQLSchema, ast: TypeNode): GraphQLType {
const type = typeFromAST(schema, ast);
invariant(isType(type), 'GraphQLSchemaUtils: Unknown type `%s`.', print(ast));
return (type: any);
}

module.exports = {
assertTypeWithFields,
canHaveSelections,
getNullableType,
getRawType,
getSingularType,
getTypeFromAST,
hasID,
implementsInterface,
isAbstractType,
Expand Down
35 changes: 26 additions & 9 deletions packages/relay-compiler/core/RelayParser.js
Expand Up @@ -17,7 +17,6 @@ const partitionArray = require('../util/partitionArray');
const {DEFAULT_HANDLE_KEY} = require('../util/DefaultHandleKey');
const {
getNullableType,
getTypeFromAST,
isExecutableDefinitionAST,
} = require('./GraphQLSchemaUtils');
const {
Expand All @@ -41,9 +40,12 @@ const {
GraphQLNonNull,
GraphQLScalarType,
isLeafType,
isType,
typeFromAST,
isTypeSubTypeOf,
parse: parseGraphQL,
parseType,
print,
Source,
} = require('graphql');

Expand All @@ -68,28 +70,30 @@ import type {GetFieldDefinitionFn} from './getFieldDefinition';
import type {
ASTNode,
ArgumentNode,
BooleanValueNode,
DefinitionNode,
DirectiveNode,
EnumValueNode,
FieldNode,
FloatValueNode,
FragmentDefinitionNode,
FragmentSpreadNode,
DefinitionNode,
GraphQLArgument,
GraphQLInputType,
GraphQLOutputType,
GraphQLSchema,
GraphQLType,
InlineFragmentNode,
IntValueNode,
ListValueNode,
Location as ASTLocation,
ObjectValueNode,
OperationDefinitionNode,
SelectionSetNode,
StringValueNode,
TypeNode,
ValueNode,
VariableNode,
IntValueNode,
FloatValueNode,
StringValueNode,
BooleanValueNode,
EnumValueNode,
ListValueNode,
ObjectValueNode,
} from 'graphql';

type ASTDefinitionNode = FragmentDefinitionNode | OperationDefinitionNode;
Expand Down Expand Up @@ -159,6 +163,19 @@ function transform(
});
}

/**
* Helper for calling `typeFromAST()` with a clear warning when the type does
* not exist. This enables the pattern `assertXXXType(getTypeFromAST(...))`,
* emitting distinct errors for unknown types vs types of the wrong category.
*/
function getTypeFromAST(schema: GraphQLSchema, ast: TypeNode): GraphQLType {
const type = typeFromAST(schema, ast);
if (!isType(type)) {
throw createUserError(`Unknown type: '${print(ast)}'.`, null, [ast]);
}
return (type: $FlowFixMe);
}

/**
* @private
*/
Expand Down
Expand Up @@ -5679,6 +5679,26 @@ query TestQuery($id: ID!) {
]
`;
exports[`RelayParser matches expected output: undefined-type.invalid.graphql 1`] = `
~~~~~~~~~~ INPUT ~~~~~~~~~~
query TestQuery($id: UnknownType) {
node(id: "someid") {
id
}
}
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
ERROR:
Error: RelayParser: Encountered 1 error(s):
- Unknown type: 'UnknownType'.
GraphQL request (1:22)
1: query TestQuery($id: UnknownType) {
^
2: node(id: "someid") {
`;
exports[`RelayParser should error when parsing fragment that references undeclared variables are used with differing types 1`] = `
"RelayParser: Encountered 1 error(s):
- Variable '$id' was used in locations expecting the conflicting types 'ID' and 'Int'. Source: document \`TestFragment\` file: \`GraphQL request\`
Expand Down
@@ -0,0 +1,5 @@
query TestQuery($id: UnknownType) {
node(id: "someid") {
id
}
}

0 comments on commit 825605a

Please sign in to comment.