Skip to content
Closed
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
22 changes: 19 additions & 3 deletions src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,25 @@ public boolean isPossibleType(Type abstractType, Type possibleType) {
return false;
} else {
InterfaceTypeDefinition iFace = (InterfaceTypeDefinition) abstractTypeDef;
List<ImplementingTypeDefinition> implementingTypeDefinitions = getAllImplementationsOf(iFace);
return implementingTypeDefinitions.stream()
.anyMatch(od -> od.getName().equals(targetObjectTypeDef.getName()));

for (TypeDefinition<?> t : types.values()) {
Copy link
Author

Choose a reason for hiding this comment

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

Probably worth investing in a cached reverse lookup at some point instead of looking through all the types everytime. Maybe on that new read only type registry.

Copy link
Member

Choose a reason for hiding this comment

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

How do you see this reverse lookup working - simply a map of name -> type say or somethings else?

ps the read only type registry PR is now merged

Copy link
Member

Choose a reason for hiding this comment

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

I think i know now - I suspect you want something like


    private final Map<InterfaceTypeDefinition, List<ImplementingTypeDefinition>> allImplementationsOf;
    private final Map<InterfaceTypeDefinition, List<ObjectTypeDefinition>> implementationsOf;


    @Override
    public List<ImplementingTypeDefinition> getAllImplementationsOf(InterfaceTypeDefinition targetInterface) {
        return allImplementationsOf.getOrDefault(targetInterface, ImmutableList.of());
    }

    @Override
    public List<ObjectTypeDefinition> getImplementationsOf(InterfaceTypeDefinition targetInterface) {
        return implementationsOf.getOrDefault(targetInterface, ImmutableList.of());
    }

if (t instanceof ImplementingTypeDefinition) {
if (t.getName().equals(targetObjectTypeDef.getName())) {
ImplementingTypeDefinition<?> itd = (ImplementingTypeDefinition<?>) t;

for (Type impl : itd.getImplements()) {
TypeName typeName = TypeInfo.getTypeName(impl);
TypeDefinition<?> matchingInterface = types.get(typeName.getName());

if (matchingInterface != null && matchingInterface.getName().equals(iFace.getName())) {
return true;
}
}
}
}
}

return false;
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/graphql/schema/idl/TypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ public static TypeInfo typeInfo(Type type) {
return new TypeInfo(type);
}

/**
* Gets the type name of a [Type], unwrapping any lists or non-null decorations
*
* @param type the Type
*
* @return the inner TypeName for this type
*/
public static TypeName getTypeName(Type type) {
Copy link
Author

Choose a reason for hiding this comment

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

What TypeInfo gave us, but without allocation of a TypeInfo object and without the tracking of decorations.

while (!(type instanceof TypeName)) {
if (type instanceof NonNullType) {
type = ((NonNullType) type).getType();
}
if (type instanceof ListType) {
type = ((ListType) type).getType();
}
}
return (TypeName) type;
}

Copy link
Member

Choose a reason for hiding this comment

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

This needs a unit test and some JavaDoc

private final Type rawType;
private final TypeName typeName;
private final Deque<Class<?>> decoration = new ArrayDeque<>();
Expand Down
17 changes: 17 additions & 0 deletions src/test/groovy/graphql/schema/idl/TypeInfoTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,21 @@ class TypeInfoTest extends Specification {
"[named!]!" | "[newName!]!"
"[[named!]!]" | "[[newName!]!]"
}

def "test getTypeName gets to the inner type"() {

expect:
Type actualType = TestUtil.parseType(actual)
def typeName = TypeInfo.getTypeName(actualType)
typeName.getName() == expected

where:
actual | expected
"named" | "named"
"named!" | "named"
"[named]" | "named"
"[named!]" | "named"
"[named!]!" | "named"
"[[named!]!]" | "named"
}
Copy link
Member

Choose a reason for hiding this comment

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

👍

}