Skip to content

Commit 7e02a96

Browse files
dh94andimarek
authored andcommitted
425 improve wrong type exceptions (graphql-java#429)
better exceptions for incorrect types in IDL
1 parent 5eae29a commit 7e02a96

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

src/main/java/graphql/schema/idl/SchemaGenerator.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import graphql.schema.PropertyDataFetcher;
4242
import graphql.schema.TypeResolver;
4343
import graphql.schema.TypeResolverProxy;
44+
import graphql.schema.idl.errors.NotAnInputTypeError;
45+
import graphql.schema.idl.errors.NotAnOutputTypeError;
4446
import graphql.schema.idl.errors.SchemaProblem;
4547

4648
import java.util.Collections;
@@ -212,8 +214,11 @@ private <T extends GraphQLOutputType> T buildOutputType(BuildContext buildCtx, T
212214
outputType = buildUnionType(buildCtx, (UnionTypeDefinition) typeDefinition);
213215
} else if (typeDefinition instanceof EnumTypeDefinition) {
214216
outputType = buildEnumType((EnumTypeDefinition) typeDefinition);
215-
} else {
217+
} else if (typeDefinition instanceof ScalarTypeDefinition){
216218
outputType = buildScalar(buildCtx, (ScalarTypeDefinition) typeDefinition);
219+
} else {
220+
// typeDefinition is not a valid output type
221+
throw new NotAnOutputTypeError(typeDefinition);
217222
}
218223

219224
buildCtx.put(outputType);
@@ -242,8 +247,11 @@ private GraphQLInputType buildInputType(BuildContext buildCtx, Type rawType) {
242247
inputType = buildInputObjectType(buildCtx, (InputObjectTypeDefinition) typeDefinition);
243248
} else if (typeDefinition instanceof EnumTypeDefinition) {
244249
inputType = buildEnumType((EnumTypeDefinition) typeDefinition);
245-
} else {
250+
} else if (typeDefinition instanceof ScalarTypeDefinition){
246251
inputType = buildScalar(buildCtx, (ScalarTypeDefinition) typeDefinition);
252+
} else {
253+
// typeDefinition is not a valid InputType
254+
throw new NotAnInputTypeError(typeDefinition);
247255
}
248256

249257
buildCtx.put(inputType);
@@ -461,4 +469,4 @@ private String buildDescription(Node node) {
461469
}
462470
return sb.toString();
463471
}
464-
}
472+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package graphql.schema.idl.errors;
2+
3+
import graphql.language.TypeDefinition;
4+
5+
import static java.lang.String.format;
6+
7+
public class NotAnInputTypeError extends BaseError {
8+
9+
public NotAnInputTypeError(TypeDefinition typeDefinition) {
10+
super(typeDefinition, format("expected InputType, but found %s type", typeDefinition.getName()));
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package graphql.schema.idl.errors;
2+
3+
import graphql.language.TypeDefinition;
4+
5+
import static java.lang.String.format;
6+
7+
public class NotAnOutputTypeError extends BaseError {
8+
9+
public NotAnOutputTypeError(TypeDefinition typeDefinition) {
10+
super(typeDefinition, format("expected OutputType, but found %s type", typeDefinition.getName()));
11+
}
12+
}

src/test/groovy/graphql/schema/idl/SchemaGeneratorTest.groovy

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package graphql.schema.idl
22

33
import graphql.TypeResolutionEnvironment
44
import graphql.schema.*
5+
import graphql.schema.idl.errors.NotAnInputTypeError
6+
import graphql.schema.idl.errors.NotAnOutputTypeError
57
import spock.lang.Specification
68

79
import java.util.function.UnaryOperator
@@ -595,7 +597,61 @@ class SchemaGeneratorTest extends Specification {
595597

596598
type.interfaces.size() == 1
597599
type.interfaces[0].name == "Character"
600+
}
601+
602+
def "Type used as inputType should throw appropriate error #425"() {
603+
when:
604+
def spec = """
605+
schema {
606+
query: Query
607+
}
608+
609+
type Query {
610+
findCharacter(character: CharacterInput!): Boolean
611+
}
612+
613+
# CharacterInput must be an input, but is a type
614+
type CharacterInput {
615+
firstName: String
616+
lastName: String
617+
family: Boolean
618+
}
619+
"""
620+
def wiring = RuntimeWiring.newRuntimeWiring()
621+
.build()
622+
623+
generateSchema(spec, wiring)
624+
625+
then:
626+
def err = thrown(NotAnInputTypeError.class)
627+
err.message == "expected InputType, but found CharacterInput type"
628+
}
629+
630+
def "InputType used as type should throw appropriate error #425"() {
631+
when:
632+
def spec = """
633+
schema {
634+
query: Query
635+
}
636+
637+
type Query {
638+
findCharacter: CharacterInput
639+
}
640+
641+
# CharacterInput must be an input, but is a type
642+
input CharacterInput {
643+
firstName: String
644+
lastName: String
645+
family: Boolean
646+
}
647+
"""
648+
def wiring = RuntimeWiring.newRuntimeWiring()
649+
.build()
598650

651+
generateSchema(spec, wiring)
599652

653+
then:
654+
def err = thrown(NotAnOutputTypeError.class)
655+
err.message == "expected OutputType, but found CharacterInput type"
600656
}
601657
}

0 commit comments

Comments
 (0)