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
95 changes: 57 additions & 38 deletions src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import graphql.language.InputObjectTypeExtensionDefinition;
import graphql.language.InterfaceTypeDefinition;
import graphql.language.InterfaceTypeExtensionDefinition;
import graphql.language.ListType;
import graphql.language.NonNullType;
import graphql.language.ObjectTypeDefinition;
import graphql.language.ObjectTypeExtensionDefinition;
import graphql.language.OperationTypeDefinition;
Expand Down Expand Up @@ -40,7 +42,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

import static graphql.Assert.assertNotNull;
import static graphql.schema.idl.SchemaExtensionsChecker.defineOperationDefs;
Expand Down Expand Up @@ -796,6 +797,10 @@ public boolean isPossibleType(Type abstractType, Type possibleType) {
}
TypeDefinition possibleTypeDef = assertNotNull(getTypeOrNull(possibleType));
TypeDefinition abstractTypeDef = assertNotNull(getTypeOrNull(abstractType));
return isPossibleTypeDef(abstractTypeDef, possibleTypeDef);
}

private boolean isPossibleTypeDef(TypeDefinition<?> abstractTypeDef, TypeDefinition<?> possibleTypeDef) {
if (abstractTypeDef instanceof UnionTypeDefinition) {
return isUnionMember((UnionTypeDefinition) abstractTypeDef, possibleTypeDef);
}
Expand All @@ -807,12 +812,25 @@ public boolean isPossibleType(Type abstractType, Type possibleType) {
private boolean implementsInterface(
ImplementingTypeDefinition<?> implementingType,
InterfaceTypeDefinition targetInterface) {
return Stream.concat(
Stream.of(implementingType),
getImplementingTypeExtensions(implementingType).stream())
.flatMap(type -> type.getImplements().stream())
.map(TypeInfo::typeName)
.anyMatch(targetInterface.getName()::equals);
String targetName = targetInterface.getName();
if (containsTypeNamed(implementingType.getImplements(), targetName)) {
return true;
}
for (ImplementingTypeDefinition<?> extension : getImplementingTypeExtensions(implementingType)) {
if (containsTypeNamed(extension.getImplements(), targetName)) {
return true;
}
}
return false;
}

private static boolean containsTypeNamed(List<Type> types, String name) {
for (Type type : types) {
if (TypeInfo.getTypeName(type).getName().equals(name)) {
return true;
}
}
return false;
}

private List<? extends ImplementingTypeDefinition<?>> getImplementingTypeExtensions(
Expand All @@ -824,12 +842,16 @@ private List<? extends ImplementingTypeDefinition<?>> getImplementingTypeExtensi
}

private boolean isUnionMember(UnionTypeDefinition unionType, TypeDefinition<?> possibleType) {
return Stream.concat(
unionType.getMemberTypes().stream(),
unionTypeExtensions.getOrDefault(unionType.getName(), List.of()).stream()
.flatMap(extension -> extension.getMemberTypes().stream()))
.map(TypeInfo::typeName)
.anyMatch(possibleType.getName()::equals);
String possibleName = possibleType.getName();
if (containsTypeNamed(unionType.getMemberTypes(), possibleName)) {
return true;
}
for (UnionTypeExtensionDefinition extension : unionTypeExtensions.getOrDefault(unionType.getName(), List.of())) {
if (containsTypeNamed(extension.getMemberTypes(), possibleName)) {
return true;
}
}
return false;
}

/**
Expand All @@ -840,50 +862,47 @@ private boolean isUnionMember(UnionTypeDefinition unionType, TypeDefinition<?> p
*
* @return true if maybeSubType is covariant or equal to superType
*/
@SuppressWarnings("SimplifiableIfStatement")
public boolean isSubTypeOf(Type maybeSubType, Type superType) {
TypeInfo maybeSubTypeInfo = TypeInfo.typeInfo(maybeSubType);
TypeInfo superTypeInfo = TypeInfo.typeInfo(superType);
// Equivalent type is a valid subtype
if (maybeSubTypeInfo.equals(superTypeInfo)) {
return true;
}


// If superType is non-null, maybeSubType must also be non-null.
if (superTypeInfo.isNonNull()) {
if (maybeSubTypeInfo.isNonNull()) {
return isSubTypeOf(maybeSubTypeInfo.unwrapOneType(), superTypeInfo.unwrapOneType());
if (superType instanceof NonNullType) {
if (maybeSubType instanceof NonNullType) {
return isSubTypeOf(((NonNullType) maybeSubType).getType(), ((NonNullType) superType).getType());
}
return false;
}
if (maybeSubTypeInfo.isNonNull()) {
if (maybeSubType instanceof NonNullType) {
// If superType is nullable, maybeSubType may be non-null or nullable.
return isSubTypeOf(maybeSubTypeInfo.unwrapOneType(), superType);
return isSubTypeOf(((NonNullType) maybeSubType).getType(), superType);
}

// If superType type is a list, maybeSubType type must also be a list.
if (superTypeInfo.isList()) {
if (maybeSubTypeInfo.isList()) {
return isSubTypeOf(maybeSubTypeInfo.unwrapOneType(), superTypeInfo.unwrapOneType());
if (superType instanceof ListType) {
if (maybeSubType instanceof ListType) {
return isSubTypeOf(((ListType) maybeSubType).getType(), ((ListType) superType).getType());
}
return false;
}
if (maybeSubTypeInfo.isList()) {
if (maybeSubType instanceof ListType) {
// If superType is not a list, maybeSubType must also be not a list.
return false;
}

// If superType type is an abstract type, maybeSubType type may be a currently
// possible object type.
if (isInterfaceOrUnion(superType) &&
isObjectTypeOrInterface(maybeSubType) &&
isPossibleType(superType, maybeSubType)) {
// Equivalent type is a valid subtype
if (typeName(maybeSubType).equals(typeName(superType))) {
return true;
}

// Otherwise, the child type is not a valid subtype of the parent type.
return false;
// If superType type is an abstract type, maybeSubType type may be a currently
// possible object type.
TypeDefinition<?> superTypeDef = getTypeOrNull(superType);
if (!(superTypeDef instanceof UnionTypeDefinition || superTypeDef instanceof InterfaceTypeDefinition)) {
return false;
}
TypeDefinition<?> maybeSubTypeDef = getTypeOrNull(maybeSubType);
if (!(maybeSubTypeDef instanceof ObjectTypeDefinition || maybeSubTypeDef instanceof InterfaceTypeDefinition)) {
return false;
}
return isPossibleTypeDef(superTypeDef, maybeSubTypeDef);
}

}
25 changes: 20 additions & 5 deletions src/main/java/graphql/schema/idl/TypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.Objects;

import static graphql.Assert.assertNotNull;
Expand Down Expand Up @@ -174,17 +175,31 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TypeInfo typeInfo = (TypeInfo) o;
return isNonNull() == typeInfo.isNonNull() &&
isList() == typeInfo.isList() &&
Objects.equals(typeName.getName(), typeInfo.typeName.getName());
return Objects.equals(typeName.getName(), typeInfo.typeName.getName()) &&
sameDecoration(decoration, typeInfo.decoration);
}

private static boolean sameDecoration(Deque<Class<?>> a, Deque<Class<?>> b) {
if (a.size() != b.size()) {
return false;
}
Iterator<Class<?>> itA = a.iterator();
Iterator<Class<?>> itB = b.iterator();
while (itA.hasNext()) {
if (itA.next() != itB.next()) {
return false;
}
}
return true;
}

@Override
public int hashCode() {
int result = 1;
result = 31 * result + Objects.hashCode(typeName.getName());
result = 31 * result + Boolean.hashCode(isNonNull());
result = 31 * result + Boolean.hashCode(isList());
for (Class<?> wrapper : decoration) {
result = 31 * result + wrapper.hashCode();
}
return result;
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/groovy/graphql/schema/idl/SchemaTypeCheckerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import graphql.schema.TypeResolver
import graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError
import graphql.schema.idl.errors.DirectiveIllegalLocationError
import graphql.schema.idl.errors.DirectiveUndeclaredError
import graphql.schema.idl.errors.InterfaceFieldRedefinitionError
import graphql.schema.idl.errors.MissingTypeError
import graphql.schema.idl.errors.NonUniqueNameError
import graphql.schema.idl.errors.QueryOperationMissingError
Expand Down Expand Up @@ -595,6 +596,34 @@ class SchemaTypeCheckerTest extends Specification {
result.get(0).getMessage().contains("has tried to redefine field 'fieldA' defined via interface 'InterfaceType'")
}

def "test object when redefining interface field with different inner list wrapping"() {

def spec = """
interface InterfaceType {
nullability : [String!]
depth : [String]
}

type BaseType implements InterfaceType {
nullability : [String]
depth : [[String]]
}

schema {
query : BaseType
}
"""

def result = check(spec)

expect:

result.size() == 2
result.every { it instanceof InterfaceFieldRedefinitionError }
result.any { it.getMessage().contains("has tried to redefine field 'nullability' defined via interface 'InterfaceType'") }
result.any { it.getMessage().contains("has tried to redefine field 'depth' defined via interface 'InterfaceType'") }
}

def "test type extension when redefining interface field"() {

def spec = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,14 @@ class TypeDefinitionRegistryTest extends Specification {
registry.isSubTypeOf(listType(nonNullType(listType(type("Canine")))), listType(nonNullType(listType(type("Mammal")))))
!registry.isSubTypeOf(listType(nonNullType(listType(type("Turtle")))), listType(nonNullType(listType(type("Mammal")))))

// inner wrappers must match, not just the outermost one
registry.isSubTypeOf(listType(nonNullType("Mammal")), listType(nonNullType("Mammal")))
registry.isSubTypeOf(listType(nonNullType("Mammal")), listType(type("Mammal")))
!registry.isSubTypeOf(listType(type("Mammal")), listType(nonNullType("Mammal")))
!registry.isSubTypeOf(listType(listType(type("Mammal"))), listType(type("Mammal")))
!registry.isSubTypeOf(listType(type("Mammal")), listType(listType(type("Mammal"))))
!registry.isSubTypeOf(nonNullType(listType(type("Mammal"))), nonNullType(listType(nonNullType("Mammal"))))


where:
typeOfReg | _
Expand Down
5 changes: 5 additions & 0 deletions src/test/groovy/graphql/schema/idl/TypeInfoTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ class TypeInfoTest extends Specification {

assertNotEqualsAndHashCode(new NonNullType(new ListType(new TypeName("A"))), new ListType(new TypeName("A")))
assertNotEqualsAndHashCode(new NonNullType(new ListType(new TypeName("A"))), new NonNullType(new ListType(new TypeName("B"))))

assertEqualsAndHashCode(new ListType(new NonNullType(new TypeName("A"))), new ListType(new NonNullType(new TypeName("A"))))
assertNotEqualsAndHashCode(new ListType(new TypeName("A")), new ListType(new NonNullType(new TypeName("A"))))
assertNotEqualsAndHashCode(new ListType(new ListType(new TypeName("A"))), new ListType(new TypeName("A")))
assertNotEqualsAndHashCode(new NonNullType(new ListType(new TypeName("A"))), new NonNullType(new ListType(new NonNullType(new TypeName("A")))))
}


Expand Down
Loading