Apollo Kotlin Client vs Netflix DGS

Hello,

I am playing with the apollo client and netflix dgs client. One of the things I am looking in Netflix DGS is that. It can accept a schema file which includes types, queries , inputs in one single file and will generate the types and mutation and query builder classes.

This is something I did not find in the Apollo Kotlin Client. I wanted to know if that is true that it does not support or I am missing something.

Hi :wave:

It can accept a schema file which includes types, queries , inputs in one single file

In that case it’s not just a schema file but a generic GraphQL file. GraphQL has 2 sides:

  • schema (type definitions)
  • execution (operations and fragment definitions)

Apollo Kotlin decided to require them to be separate files because it’s generally easier to reason about. This also saves recompiling everything if you just changed one query. Is there a strong reason you require everything in one file?

Hi, thanks. The only reason its nicer to be in one single file is that I use a cli and it just spits out in one single file. I don’t want to fiddle with it manually.

example
./gradlew downloadApolloSchema
and
rover graph introspect

Oooohh right sorry I see what you’re seeing now. Both ./gradlew downloadApolloSchema and rover graph introspect will give you schema files. Looks like there’s a difference between DGS and Apollo Kotlin generate their models:

  • DGS generates models based on the Schema
  • Apollo Kotlin generates modeles based on your queries (and therefore needs query files)

DGS is simpler but will give you less type safety. For an example, say you have a User type:

type User {
  id: ID
  firstName: String
  lastName: String
  email: String
  address: String
}

And you’re querying only email:

query GetUserEmail($id: ID!) {
  user(id: $id) {
    email
  }
}

DGS will create a model that has firstName, lastName, etc… So you can do this:

// This will compile just fine but will error at runtime
response.data.user.firstName

This is not possible with Apollo Kotlin because the model will only contain email:

class QueryUser(val email: String)

// This will not compile
response.data.user.firstName

All in all it all depends how you prefer to tradeoff simplicity for type safety.

I see. I am a bit lost though. If it’s generating using the operations. Then why does in the getting started states to download the schema? Introduction to Apollo Kotlin - Apollo GraphQL Docs

If it’s generating using the operations. Then why does in the getting started states to download the schema?

It’s using the schema to validate the operations at build time. This way you are guaranteed that if you code compiles, the property in your UI code will exist and be of the correct type.

I see I understood quite wrong how things work in Apollo side. So do I understand correct that Neflix DGS is more like Model first approach and Apollo on the other hand its code first approach ?

The code first vs schema first is usually more of a backend thing, whether you write your schema in GraphQL (and use it in Kotlin) or write it in Kotlin (and generate the GraphQL from it). But I guess you’re right that it can be extended to client.

With Apollo Kotlin you would write your queries in GraphQL and generate Kotlin models (GraphQL first) while with DGS you would write your queries in Kotlin (using the generated query builders from the schema).

I hope that helps?

1 Like

that helps. thank you. btw I am planning to use apollo client lib from a backend service calling a graphql API. my usecase is acceptable or will i bump into some issues?

1 Like

I am planning to use apollo client lib from a backend service calling a graphql API

That should work. A lot of the users are using Apollo Kotlin on Android but there’s actually no Android specific code in the runtime. You can use it everywhere there is a JVM!

1 Like