Repository navigation
Description
NOTE: My original example was tragically bad, so I've completely replaced it. As a result, the comments discussing my original example will not make sense. I apologize for that. The gist of the issue is still the same though.
An example of the problem is when resolving a type for a GraphQL interface. The only information available to the TypeResolver is the resulting Java object itself, from which the resolver is supposed to deduce the corresponding GraphQL type. This forces the implementor to manually maintain a map of GraphQLObjectTypes (as the schema object isn't (easily) reachable) and, more importantly, if the resulting object is an instance of a generic class, this decision is impossible without further information.
Suppose the following:
interface Repository { ... }
class InMemoryRepository<T> implements Repository { ... }
InMemoryRepository<Order> might map to e.g. InMemoryRepository_Order GraphQL type (that has a containedItem field of type Order), while InMemoryRepository<Product> might map to InMemoryRepository_Product GraphQL type (that has a containedItem field of type Product), both implementing Repository:
type InMemoryRepository_Order implements Repository {
containedItem: Order
}
type InMemoryRepository_Product implements Repository {
containedItem: Product
}
When TypeResolver receives an instance of InMemoryRepository, there's no way to decide the GraphQL type: as it may be InMemoryRepository_Order or InMemoryRepository_Product.
If TypeResolver were to receive more information, it may be able to deduce the type. E.g. if it had access to the Field object, it could get the Java type of the field e.g. InMemoryRepository<Order> and it may deduce the corresponding type should be InMemoryRepository_Order. (It's possible to store the mapped Java type into Field's GraphQL type, as demonstrated by graphql-java-annotations here). With some advanced type analysis, even subtypes can be dealt with, e.g. if the runtime type of the resulting object wasn't InMemoryRepository but some subtype of it. I use this type of advanced analysis in my project (link below).
For example, in graphql-java-annotations and schemagen-graphql this exact strategy could be used for a much richer type resolution logic (a Type could be used instead of just a Class and combined with the runtime type of the resulting object).
In general, this change is especially of value to the maintainers of libraries that rely on GraphQL-Java (and we fortunately have a few of those now, mine included), because when developing a library as opposed the final project, the Java types can not be known ahead of time, so strategies like inspecting the runtime type of containedItem in the example above e.g. repo.getContainedItem().getClass() are impossible.
The change I propose to allow such advanced type resolution logic is providing TypeResolver most of the data available to DataFetchingEnvironment (this object is called TypeResolutionEnvironment in my PR) instead of just the resulting object. You can see this in use in my project: https://github.com/leangen/graphql-spqr/blob/master/src/main/java/io/leangen/graphql/generator/HintedTypeResolver.java#L44
It can be argued that this is an edge case, but that would imply usage of generics with this library in general is an edge case. I firmly believe that the fact we're dealing with a Java implementation of the GraphQL spec means it needs to deeply support Java idioms.
Activity
okorz001 commentedon Apr 23, 2016
instanceof?
kaqqao commentedon Apr 23, 2016
Ha, with the way I worded the example, you're right! I now realize just how terrible job I've done trying to explain my problem. In my attempts to simplify the explanation of the situation I'm facing, I completely butchered the example. Sorry for that. I've made at attempt at fixing it. It's still somewhat vague, but I really hope it does a better job than the previous version.
okorz001 commentedon Apr 27, 2016
Is the id not in the returned User object?
kaqqao commentedon Apr 27, 2016
Well, think of Relay global ID required by the Node spec. An ID is in the User return object, but it's no longer prefixed. The prefix was used only to satisfy Relay Node query, and to know what object type is being queried for. Perhaps the received ID was created using GraphQL-Java's own
Relay#toGlobalIdmethod.So the result would contain ID "1234" but not e.g. "objectType:1234".
(Edited the title and the description to contain this info as well)
[-]TypeResolver often needs more input[/-][+]TypeResolver often needs more input to resolve Relay Node type[/+]ntkoopman commentedon May 16, 2016
Why would it have a different type if the actual class is the same?
Anyway, instead of using a User object directly, you could wrap it inside a TypedUser object that contained the the User object and the correct type.
kaqqao commentedon May 16, 2016
Actual
Classmight be the same, but not theType, thus GraphQL types might differ. This can potentially be worked around if you know all your Java types ahead of time, but that is not always the case either (e.g. if you're working on a library and not project code).As for the
TypedUserclass, that's exactly what I tried doing, but theTypedUserclass has to be generated dynamically, at runtime, as the lib I'm working on has to work with types unknown ahead of time. This is both very limiting (as it requires default constructor to always exist, can't deal with collections reasonably etc) and needlessly complicated and expensive. That's why I'm proposing to simply forward most of the info fromDataFetchingEnvironmentto theTypeResolver.domkm commentedon Oct 31, 2016
I am also running into this issue. @kaqqao's suggestion that the
DataFetchingEnvironmentbe made available toTypeResolverwould solve it perfectly. This is also the approach that Apollo'sgraphql-toolsuses for their type resolver signature.dminkovsky commentedon Nov 4, 2016
@domkm yes we're trying to figure out an approach for this, for the next breaking release.
[-]TypeResolver often needs more input to resolve Relay Node type[/-][+]TypeResolver often needs more input to resolve (Relay Node) type[/+]kaqqao commentedon Apr 9, 2017
I've just tried removing the dependency on our forked version of graphql-java in out project and realized it's more painful than expected as
TypeResolverdoesn't even give access to the schema. And sinceTypeResolvers are instantiated before the schema is even built, it means there's no way to even provide the schema through constructor... which either means mutability/synchronization issues with adding the schema later or manually maintaining a schema-like registry of GraphQL types. And both are rather awful :(I really hope the next release will bring some resolution.
bbakerman commentedon Apr 27, 2017
is this related to #205 ?
kaqqao commentedon Apr 27, 2017
I elaborated in #205. In short, yes, I believe solving this one may solve or at least alleviate #205 as well.
Adding extra data to type resolver
Adding extra data to type resolver
Merge remote-tracking branch 'upstream/master'