Greetings!
I realize this has been brought up before, but this is my one major pain point with this otherwise great software.
Could you allow mapping results to predefined POJOs? Right now, every Query has it's own generated model classes, which makes them relatively worthless for app-wide models and forces the use of utility mapping classes to map return results to POJOs.
While I'm sure there was some technical reason for this at the time, I don't see why Apollo
can't work in a similar fashion to retrofit/gson. I could map a __typename to a predefined POJO, and similar to gson, you could create a new instance of that object and assign values to the fields.
Not sure what then the benefit you will get? If you have already defined manually POJO that represents the http response for query execution then you can use for example Gson for deserialization?
The main feature of Apollo is type safe, nullability safe POJO code generation from provided GraphQL queries, plus query validation.
How do you set up Apollo so that it'll generate the code to build queries, but use gson to parse the results?
The benefit is code reusability, the ability to share common objects around an app and to not have to maintain mapping classes to map apollo response models to common app models.
The existing pojos don't necessarily represent actual http responses, they represent data objects used by the app.
Let me explain with some examples:
Common shared User object:
class Person {
int id;
String name;
}
class Books {
int id;
Person author;
}
class Movies {
int id;
User director;
}
// Gql
query getBooks {
id,
author
}
query getMovies {
id,
director
}
In Apollo, all of those "Person" objects are different objects. I get something like BooksQuery.Person, MoviesQuery.Person, and it's now my job to create and maintain a mapping of BooksQuery.Person to Person, etc.
In retrofit I can just use a Books class that contains a Person object and it'll put those values in there. If there's any discrepancy between the json results and the class structure, I can use directives to adjust.
Now, it'd be great if there were some way I could tell apollo, "__typename Person" maps to this Person class, and the generated models could create a Person class object and populate it in some sane way like gson/retrofit does.
This is not a question. It is a feature request.
How do you set up Apollo so that it'll generate the code to build queries, but use gson to parse the results?
You can't as parsing GraphQL response is not simple task when it comes to fragments, inline fragments, directives. You can't just have use Gson, Moshi etc. to map http response on DTO object.
the ability to share common objects around an app and to not have to maintain mapping classes to map apollo response models to common app models.
we understand and intentions but we think that concepts of clean architecture is more important here each layer must have own models, (separation of concerns and isolation).
In Apollo, all of those "Person" objects are different objects. I get something like BooksQuery.Person, MoviesQuery.Person
In order to share the same object in GraphQL it's common practice to use fragments
In retrofit I can just use a Books class that contains a Person object and it'll put those values in there.
Again GraphQL is not REST, you can't just map http response on POJO, it;s more complicated deserialization.
we understand and intentions but we think that concepts of clean architecture is more important here each layer must have own models, (separation of concerns and isolation).
And that is my answer.
That's unfortunate from my perspective, but it's your project, so I'll accept your answer.
Cheers.
Most helpful comment
The benefit is code reusability, the ability to share common objects around an app and to not have to maintain mapping classes to map apollo response models to common app models.
The existing pojos don't necessarily represent actual http responses, they represent data objects used by the app.
Let me explain with some examples:
Common shared User object:
In Apollo, all of those "Person" objects are different objects. I get something like BooksQuery.Person, MoviesQuery.Person, and it's now my job to create and maintain a mapping of BooksQuery.Person to Person, etc.
In retrofit I can just use a Books class that contains a Person object and it'll put those values in there. If there's any discrepancy between the json results and the class structure, I can use directives to adjust.
Now, it'd be great if there were some way I could tell apollo, "__typename Person" maps to this Person class, and the generated models could create a Person class object and populate it in some sane way like gson/retrofit does.