Is this library able to take a `myschema.graphql` ...
# graphql-kotlin
j
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
Copy code
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
Copy code
data class User(
    val id: String,
    val name: String, 
    val gender: Gender
)
Gender.kt
Copy code
enum class Gender {
    MALE, FEMALE, OTHER, UNKNOWN;
}
b
Don't think so, but #apollo-kotlin can. #graphql-kotlin uses code-first approach to generate schema
m
#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
Yeah same as
apollo-kotlin
we habe codegen for queries
j
Thanks @Big Chungus @mbonnin @Dariusz Kuc for the quick replies!
d
I believe
netflix-dgs
has some schema codegen capability but unsure how good/bad it is
j
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
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
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
I think it does. But think what would happen if your frontend doesn't need to display the gender and does this query:
Copy code
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
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
@Justin it sounds like you are looking for client and not a server?
clients generate your code based on your graphql queries
j
That is correct, the server already exists and is out of my hands
d
and was mentioned above you can use either
apollo-android
or
graphql-kotlin
to do it
m
A client that always fetches all the fields on all objects automatically
d
*there are some other JVM clients (like amex nodes) but unsure if they generate the models
m
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
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
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
I have not used it but https://github.com/ermadmi78/kobby sounds similar to the approach I think you have in mind?