https://kotlinlang.org logo
Title
j

Justin

03/11/2022, 5:54 PM
Is this library able to take a
myschema.graphql
schema file as input and output a collection of kotlin files containing kotlin data classes for that schema? For example, this GraphQL schema:
myschema.graphql
type User {
    """
    The unique identifier for the user
    """
    id: String!
    """
    User first and last name
    """
    name: String!
    """
    Gender identity of user
    """
    gender: Gender
}

enum Gender {
	MALE
	FEMALE
	OTHER
	UNKNOWN
}
...would generate:
User.kt
data class User(
    val id: String,
    val name: String, 
    val gender: Gender
)
Gender.kt
enum class Gender {
    MALE, FEMALE, OTHER, UNKNOWN;
}
b

Big Chungus

03/11/2022, 5:55 PM
Don't think so, but #apollo-kotlin can. #graphql-kotlin uses code-first approach to generate schema
m

mbonnin

03/11/2022, 5:58 PM
#apollo-kotlin does but it's mostly generating models for queries, not for schema types
(because queries select a subselection of fields)
That being said, #graphql-kotlin also has codegen but IIRC it's also query-based
d

Dariusz Kuc

03/11/2022, 6:00 PM
Yeah same as
apollo-kotlin
we habe codegen for queries
j

Justin

03/11/2022, 6:00 PM
Thanks @Big Chungus @mbonnin @Dariusz Kuc for the quick replies!
d

Dariusz Kuc

03/11/2022, 6:00 PM
I believe
netflix-dgs
has some schema codegen capability but unsure how good/bad it is
j

Justin

03/11/2022, 6:01 PM
I wonder if the lack of library support for this sort of thing is an indication that my use case is somehow misguided?
It seemed like an obvious way to work with existing graphql schema types in kotlin code safely, but I don't see evidence that anyone else is taking this approach
m

mbonnin

03/11/2022, 6:03 PM
Basically, if you're doing this, all your data classes fields need to be nullable because depending the query, the field might or might not be returned
j

Justin

03/11/2022, 6:04 PM
hmmm
let me start with the problem i'm trying to solve here
• i have a postgres database behind a graphql service • the graphql service is written in rust and is not changeable • one of the types defined in the graphql schema is the data model that informs my client frontend application (i.e. for mobile/web/etc.) • i want to be able to query the graphql service for that model from those frontend apps and use it to drive the UI • in order to do that safely, i need to be able to deserialize the response from graphql into a kotlin type
Does that make sense?
m

mbonnin

03/11/2022, 6:11 PM
I think it does. But think what would happen if your frontend doesn't need to display the gender and does this query:
query GetUser {
  user {
    id
    name
    # do not query gender
    # gender
  }
}
How would you map the result to that query to your above Kotlin class?
j

Justin

03/11/2022, 6:14 PM
so the short answer to that question is that when the application pulls data from graphql, it always queries the entire
user
object (doesn't matter what is on screen at that moment). (fwiw the answer is different in terms of how the application publishes changes to the
user
)
d

Dariusz Kuc

03/11/2022, 6:18 PM
@Justin it sounds like you are looking for client and not a server?
clients generate your code based on your graphql queries
j

Justin

03/11/2022, 6:19 PM
That is correct, the server already exists and is out of my hands
d

Dariusz Kuc

03/11/2022, 6:19 PM
and was mentioned above you can use either
apollo-android
or
graphql-kotlin
to do it
m

mbonnin

03/11/2022, 6:19 PM
A client that always fetches all the fields on all objects automatically
d

Dariusz Kuc

03/11/2022, 6:20 PM
*there are some other JVM clients (like amex nodes) but unsure if they generate the models
m

mbonnin

03/11/2022, 6:20 PM
That's not a very common use case because one of the strength of GraphQL is avoiding overfetching like this. I guess this is why there's not much support out there
j

Justin

03/11/2022, 6:24 PM
I see. Based on the responses here, I'm guessing that most apps have some sort of layer between their graphql client and the models that drive their apps.
m

mbonnin

03/11/2022, 6:26 PM
It's very common to map the generated GraphQL models to the UI models. But the main point here is that the UI models (or GraphQL models) dont need all the fields from the backend. If you're writing a Github client for an example you don't want to query all the fields on
Repository
or you will waste network bandwith and backend CPU time.
But maybe your use case is different. Just if your graph grows, it's nice to have the possibility to only query a sub-selection of fields
j

Joe

03/11/2022, 8:00 PM
I have not used it but https://github.com/ermadmi78/kobby sounds similar to the approach I think you have in mind?