Skip to content

Possibility to add __typename to response projection of GraphQL types #245

@jxnu-liguobin

Description

@jxnu-liguobin

Is your feature request related to a problem? Please describe.
I think if model is a interface or them subclass always need __typename to deserialized

this is successfully

  def hero(episode: EpisodeDO): CharacterDO = {
    val heroQueryRequest = new HeroQueryRequest
    heroQueryRequest.setEpisode(episode)
    //must use typename, and add jackson annotation to support
    val characterResponseProjection = new CharacterResponseProjection().id().name().typename()
      .friends(new CharacterResponseProjection().id().typename()).appearsIn()
    val graphQLRequest = new GraphQLRequest(heroQueryRequest, characterResponseProjection)
    val retFuture = OkHttp.executeRequest(graphQLRequest, new TypeReference[CharacterDO] {})
    val ret = Await.result(retFuture, Duration.Inf)
    ret
  }

this is failed

  def droid(id: String): DroidDO = {
    val productByIdQueryRequest = new DroidQueryRequest
    productByIdQueryRequest.setId(id)
    val droidResponseProjection = new DroidResponseProjection().id().name()
    val graphQLRequest = new GraphQLRequest(productByIdQueryRequest, droidResponseProjection)
    val retFuture = OkHttp.executeRequest(graphQLRequest, new TypeReference[DroidDO] {})
    val ret = Await.result(retFuture, Duration.Inf)
    ret
  }

msg

com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class io.github.dreamylost.model.DroidDO]: missing type id property '__typename'
 at [Source: (String)"{"name":"R2-D2","id":"2001"}"; line: 1, column: 28]

config

customAnnotationsMapping := {
  val mapping = new util.HashMap[String, String]
  //must add this annotation
  //property is __typename and you must with __typename while invoke, like new CharacterResponseProjection().id().name().typename()
  //and in @JsonSubTypes.Type, name is __typename's value
  mapping.put("Character",
    """com.fasterxml.jackson.annotation.JsonTypeInfo(use=com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include=com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY,property = "__typename")
      |@com.fasterxml.jackson.annotation.JsonSubTypes(value = {
      |        @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = HumanDO.class, name = "Human"),
      |        @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = DroidDO.class, name = "Droid")
      |})
      |""".stripMargin)
  mapping
}

java class

package io.github.dreamylost.model;


@javax.annotation.Generated(
    value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
    date = "2020-07-21T09:54:54+0800"
)
@com.fasterxml.jackson.annotation.JsonTypeInfo(use=com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include=com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY,property = "__typename")
@com.fasterxml.jackson.annotation.JsonSubTypes(value = {
        @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = HumanDO.class, name = "Human"),
        @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = DroidDO.class, name = "Droid")
})

public interface CharacterDO {

    @javax.validation.constraints.NotNull
    String getId();

    @javax.validation.constraints.NotNull
    String getName();

    @com.fasterxml.jackson.annotation.JsonTypeInfo(use=com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include=com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY,property = "__typename")
@com.fasterxml.jackson.annotation.JsonSubTypes(value = {
        @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = HumanDO.class, name = "Human"),
        @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = DroidDO.class, name = "Droid")
})

    java.util.List<? extends CharacterDO> getFriends();

    @javax.validation.constraints.NotNull
    java.util.List<EpisodeDO> getAppearsIn();

    @Deprecated
    String getSecretBackstory();

}

because i can not use typename on DroidResponseProjection

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions