1

I'm currently using Apollo's Android GraphQL client to interact with our GraphQL server. I have several queries and mutations defined in my graphql file. A couple of them return same data structure in the response. However, the code that's autogenerated by Apollo results in different data classes.

For example I have the following mutations:

mutation completeLogin(...) {
    loginComplete(input: ...) {
        login {
            token {
                access_token
                refresh_token
            }
        }
    }
}

mutation createPassword(...) {
    createPassword(input: ...) {
        login {
            token {
                access_token
                refresh_token
            }
        }
    }
}

The autogenerated classes for these are as follows:

data class CompleteLoginMutation{

    //...

  data class Login(
    val token: Token
  )
  data class Token(
    val access_token: String,
    val refresh_token: String
  )
}

data class CreatePasswordMutation{

    //...

  data class Login(
    val token: Token
  )
  data class Token(
    val access_token: String,
    val refresh_token: String
  )
}

I was expecting that Login or Token classes in these autogenerated classes were mapped to the same class as they are actually the same on the backend side. This is a problem while implementing mappers that convert data entities to domain entities. I need to implement the same mapper for these classes separately, resulting in further duplication. Is there a way to map the same query/mutation responses to the same autogenerated classes in GraphQL or may Apollo client be capable of such convenience?

Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73
  • use fragments ? – xadm Dec 25 '20 at 14:38
  • fragments don't prevent generation of duplicate classes. – Ugurcan Yildirim Dec 25 '20 at 16:38
  • on login? I don't think it's supported for nested (why not just one level - token?) class ... or it should work - https://stackoverflow.com/a/56605813/6124657 ? – xadm Dec 25 '20 at 17:19
  • Doesn't matter. Each mutation data class has its own "Login" and "Token". I want them to be generated as common data classes. – Ugurcan Yildirim Dec 25 '20 at 17:27
  • server return type for this mutations? it's one type (Login?) ... querying nested data (Token) doesn't matter ... how did you try to implement fragments? – xadm Dec 25 '20 at 17:56
  • Yes it's same "Login" on server side. But on Android side, it's "CompleteLoginMutation.Login" and "CreatePasswordMutation.Login". This is the problem. – Ugurcan Yildirim Dec 25 '20 at 19:28
  • I mean **graphql type** (not java class) ... is it defined in API as `Login`, is it mutation return type? is it discoverable by introspection query? `createPassword(input: CreatePasswordMutationInput): LOGIN` ??? – xadm Dec 25 '20 at 19:36
  • Both mutations return this: type LoginCompleteResponse { login: LoginComplete! } – Ugurcan Yildirim Dec 25 '20 at 20:34
  • then you can use `fragment loginFragment on LoginCompleteResponse ...` – xadm Dec 25 '20 at 20:41
  • this doesnt resolve duplication issue. fragments only resolve duplication in graphql files. if I use fragments, the generated class becomes CompleteLoginMutation.Fragments.Login.Token. Still connected to CompleteLoginMutation. – Ugurcan Yildirim Dec 25 '20 at 21:06
  • why not simply LoginFragment? it should work with fragments ... if not, post an issue on github ... with full description ... again, how are you trying fragments? without subprops/subtypes I hope? – xadm Dec 25 '20 at 23:02

0 Answers0