Skip to content

TypeResolver often needs more input to resolve (Relay Node) type #122

Closed

Description

@kaqqao

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

  1. okorz001 commented on Apr 23, 2016

    @okorz001
    Contributor

    instanceof?

  2. kaqqao commented on Apr 23, 2016

    @kaqqao
    ContributorAuthor

    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.

  3. okorz001 commented on Apr 27, 2016

    @okorz001
    Contributor

    Is the id not in the returned User object?

  4. kaqqao commented on Apr 27, 2016

    @kaqqao
    ContributorAuthor

    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#toGlobalId method.
    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)

  5. changed the title [-]TypeResolver often needs more input[/-] [+]TypeResolver often needs more input to resolve Relay Node type[/+] on May 3, 2016
  6. ntkoopman commented on May 16, 2016

    @ntkoopman

    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.

  7. kaqqao commented on May 16, 2016

    @kaqqao
    ContributorAuthor

    Actual Class might be the same, but not the Type, 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 TypedUser class, that's exactly what I tried doing, but the TypedUser class 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 from DataFetchingEnvironment to the TypeResolver.

  8. domkm commented on Oct 31, 2016

    @domkm

    I am also running into this issue. @kaqqao's suggestion that the DataFetchingEnvironment be made available to TypeResolver would solve it perfectly. This is also the approach that Apollo's graphql-tools uses for their type resolver signature.

  9. dminkovsky commented on Nov 4, 2016

    @dminkovsky
    Contributor

    @domkm yes we're trying to figure out an approach for this, for the next breaking release.

  10. changed the title [-]TypeResolver often needs more input to resolve Relay Node type[/-] [+]TypeResolver often needs more input to resolve (Relay Node) type[/+] on Nov 24, 2016
  11. kaqqao commented on Apr 9, 2017

    @kaqqao
    ContributorAuthor

    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 TypeResolver doesn't even give access to the schema. And since TypeResolvers 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.

  12. bbakerman commented on Apr 27, 2017

    @bbakerman
    Member

    is this related to #205 ?

  13. kaqqao commented on Apr 27, 2017

    @kaqqao
    ContributorAuthor

    I elaborated in #205. In short, yes, I believe solving this one may solve or at least alleviate #205 as well.

  14. added a commit that references this issue on May 29, 2017
    e3f8487
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions