Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/java/graphql/schema/idl/SchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import graphql.schema.PropertyDataFetcher;
import graphql.schema.TypeResolver;
import graphql.schema.TypeResolverProxy;
import graphql.schema.idl.errors.NotAnInputTypeError;
import graphql.schema.idl.errors.NotAnOutputTypeError;
import graphql.schema.idl.errors.SchemaProblem;

import java.util.Collections;
Expand Down Expand Up @@ -212,8 +214,11 @@ private <T extends GraphQLOutputType> T buildOutputType(BuildContext buildCtx, T
outputType = buildUnionType(buildCtx, (UnionTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof EnumTypeDefinition) {
outputType = buildEnumType((EnumTypeDefinition) typeDefinition);
} else {
} else if (typeDefinition instanceof ScalarTypeDefinition){
outputType = buildScalar(buildCtx, (ScalarTypeDefinition) typeDefinition);
} else {
// typeDefinition is not a valid output type
throw new NotAnOutputTypeError(typeDefinition);
}

buildCtx.put(outputType);
Expand Down Expand Up @@ -242,8 +247,11 @@ private GraphQLInputType buildInputType(BuildContext buildCtx, Type rawType) {
inputType = buildInputObjectType(buildCtx, (InputObjectTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof EnumTypeDefinition) {
inputType = buildEnumType((EnumTypeDefinition) typeDefinition);
} else {
} else if (typeDefinition instanceof ScalarTypeDefinition){
inputType = buildScalar(buildCtx, (ScalarTypeDefinition) typeDefinition);
} else {
// typeDefinition is not a valid InputType
throw new NotAnInputTypeError(typeDefinition);
}

buildCtx.put(inputType);
Expand Down Expand Up @@ -461,4 +469,4 @@ private String buildDescription(Node node) {
}
return sb.toString();
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/graphql/schema/idl/errors/NotAnInputTypeError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package graphql.schema.idl.errors;

import graphql.language.TypeDefinition;

import static java.lang.String.format;

public class NotAnInputTypeError extends BaseError {

public NotAnInputTypeError(TypeDefinition typeDefinition) {
super(typeDefinition, format("expected InputType, but found %s type", typeDefinition.getName()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Have a look at the other errors. They try to include a linecol() of the offending definition

@bbakerman bbakerman May 15, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

eg

public class MissingInterfaceTypeError extends BaseError {

    public MissingInterfaceTypeError(String typeOfType, TypeDefinition typeDefinition, TypeName typeName) {
        super(typeDefinition, format("The %s type '%s' is not present when resolving type '%s' %s",
                typeOfType, typeName.getName(), typeDefinition.getName(), lineCol(typeDefinition)));
    }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@bbakerman I merged it already ... my bad.

}
}
12 changes: 12 additions & 0 deletions src/main/java/graphql/schema/idl/errors/NotAnOutputTypeError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package graphql.schema.idl.errors;

import graphql.language.TypeDefinition;

import static java.lang.String.format;

public class NotAnOutputTypeError extends BaseError {

public NotAnOutputTypeError(TypeDefinition typeDefinition) {
super(typeDefinition, format("expected OutputType, but found %s type", typeDefinition.getName()));
}
}
56 changes: 56 additions & 0 deletions src/test/groovy/graphql/schema/idl/SchemaGeneratorTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package graphql.schema.idl

import graphql.TypeResolutionEnvironment
import graphql.schema.*
import graphql.schema.idl.errors.NotAnInputTypeError
import graphql.schema.idl.errors.NotAnOutputTypeError
import spock.lang.Specification

import java.util.function.UnaryOperator
Expand Down Expand Up @@ -595,7 +597,61 @@ class SchemaGeneratorTest extends Specification {

type.interfaces.size() == 1
type.interfaces[0].name == "Character"
}

def "Type used as inputType should throw appropriate error #425"() {
when:
def spec = """
schema {
query: Query
}

type Query {
findCharacter(character: CharacterInput!): Boolean
}

# CharacterInput must be an input, but is a type
type CharacterInput {
firstName: String
lastName: String
family: Boolean
}
"""
def wiring = RuntimeWiring.newRuntimeWiring()
.build()

generateSchema(spec, wiring)

then:
def err = thrown(NotAnInputTypeError.class)
err.message == "expected InputType, but found CharacterInput type"
}

def "InputType used as type should throw appropriate error #425"() {
when:
def spec = """
schema {
query: Query
}

type Query {
findCharacter: CharacterInput
}

# CharacterInput must be an input, but is a type
input CharacterInput {
firstName: String
lastName: String
family: Boolean
}
"""
def wiring = RuntimeWiring.newRuntimeWiring()
.build()

generateSchema(spec, wiring)

then:
def err = thrown(NotAnOutputTypeError.class)
err.message == "expected OutputType, but found CharacterInput type"
}
}