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
20 changes: 2 additions & 18 deletions src/main/java/graphql/schema/idl/SchemaTypeExtensionsChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import graphql.GraphQLError;
import graphql.Internal;
import graphql.language.Argument;
import graphql.language.AstPrinter;
import graphql.language.Directive;
import graphql.language.EnumTypeDefinition;
import graphql.language.EnumValueDefinition;
Expand All @@ -14,7 +13,6 @@
import graphql.language.InterfaceTypeDefinition;
import graphql.language.ObjectTypeDefinition;
import graphql.language.ScalarTypeDefinition;
import graphql.language.Type;
import graphql.language.TypeDefinition;
import graphql.language.TypeName;
import graphql.language.UnionTypeDefinition;
Expand Down Expand Up @@ -328,10 +326,7 @@ private void checkForFieldRedefinition(List<GraphQLError> errors, TypeDefinition
fieldDefinitions.forEach(fld -> {
FieldDefinition reference = referenceMap.get(fld.getName());
if (referenceMap.containsKey(fld.getName())) {
// ok they have the same field but is it the same type
if (!isSameType(fld.getType(), reference.getType())) {
errors.add(new TypeExtensionFieldRedefinitionError(typeDefinition, fld));
}
errors.add(new TypeExtensionFieldRedefinitionError(typeDefinition, fld));
}
});
}
Expand All @@ -342,10 +337,7 @@ private void checkForInputValueRedefinition(List<GraphQLError> errors, InputObje
inputValueDefinitions.forEach(fld -> {
InputValueDefinition reference = referenceMap.get(fld.getName());
if (referenceMap.containsKey(fld.getName())) {
// ok they have the same field but is it the same type
if (!isSameType(fld.getType(), reference.getType())) {
errors.add(new TypeExtensionFieldRedefinitionError(typeDefinition, fld));
}
errors.add(new TypeExtensionFieldRedefinitionError(typeDefinition, fld));
}
});
}
Expand All @@ -369,12 +361,4 @@ private <T> void forEachBut(T butThisOne, List<T> list, Consumer<T> consumer) {
consumer.accept(t);
}
}


private boolean isSameType(Type type1, Type type2) {
String s1 = AstPrinter.printAst(type1);
String s2 = AstPrinter.printAst(type2);
return s1.equals(s2);
}

}
3 changes: 0 additions & 3 deletions src/test/groovy/graphql/schema/idl/SchemaGeneratorTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,9 @@ class SchemaGeneratorTest extends Specification {
extraField1 : String
}
extend type BaseType implements Interface2 {
extraField1 : String
extraField2 : Int
}
extend type BaseType implements Interface3 {
extraField1 : String
extraField3 : ID
}
extend type BaseType {
Expand Down Expand Up @@ -604,7 +602,6 @@ class SchemaGeneratorTest extends Specification {
name: String!
}
extend type Human implements Character {
name: String!
friends: [Character]
}
extend type Human {
Expand Down
89 changes: 81 additions & 8 deletions src/test/groovy/graphql/schema/idl/SchemaTypeCheckerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ class SchemaTypeCheckerTest extends Specification {
result.get(0).getMessage().contains("tried to redefine field 'fieldA'")
}

def "test ext type can redefine fields in their base type of the same type"() {

def "test ext type cannot redefine fields in their base type of the same type"() {
given:
def spec = """

type BaseType {
Expand All @@ -418,11 +418,12 @@ class SchemaTypeCheckerTest extends Specification {
}
"""

when:
def result = check(spec)

expect:
then:

result.isEmpty()
errorContaining(result, "BaseType' extension type [@n:n] tried to redefine field 'fieldA' [@n:n]")
}

def "test ext type redefines fields in their peer types"() {
Expand Down Expand Up @@ -454,7 +455,7 @@ class SchemaTypeCheckerTest extends Specification {
}

def "test ext type redefines fields in their peer types of the same type is ok"() {

given:
def spec = """

type BaseType {
Expand All @@ -474,11 +475,11 @@ class SchemaTypeCheckerTest extends Specification {
}
"""

when:
def result = check(spec)

expect:

result.isEmpty()
then:
errorContaining(result, "BaseType' extension type [@n:n] tried to redefine field 'fieldB' [@n:n]")
}

def "test ext type is missing the base type"() {
Expand Down Expand Up @@ -1665,4 +1666,76 @@ class SchemaTypeCheckerTest extends Specification {

}

def "field in base interface type redefined in extension type should cause an error"() {
given:
def sdl = """
type Query { hello: String }

interface Human {
id: ID!
name: String!
}
extend interface Human {
name: String!
friends: [String]
}
"""

when:
def result = check(sdl)

then:
errorContaining(result, "'Human' extension type [@n:n] tried to redefine field 'name' [@n:n]")

}

def "field in interface extension type redefined in another extension type should cause an error"() {
given:
def sdl = """
type Query { hello: String }

interface Human {
id: ID!
}

extend interface Human {
name: String!
}

extend interface Human {
name: String!
friends: [String]
}
"""

when:
def result = check(sdl)

then:
errorContaining(result, "'Human' extension type [@n:n] tried to redefine field 'name' [@n:n]")

}

def "field in base input type redefined in extension type should cause an error"() {
given:
def sdl = """
type Query { hello: String }

input Human {
id: ID!
name: String!
}
extend input Human {
name: String!
friends: [String]
}
"""

when:
def result = check(sdl)

then:
errorContaining(result, "'Human' extension type [@n:n] tried to redefine field 'name' [@n:n]")

}
}