Apollo-android: Add support for default optional values in Kotlin models

Created on 10 Dec 2019  路  5Comments  路  Source: apollographql/apollo-android

Is your feature request related to a problem? Please describe.
In case where there exists a query/mutation which expects n amount of arguments, where a part of them are optional, generated kotlin code forces all of the arguments to be included during the query/mutation object building, instead of only the mandatory ones.

Describe the solution you'd like
For the sake of example, lets consider a mutation:

mutation updateUser(
    $name: String!,
    $lastName: String,
    $email: String,
)

We have one required argument name, rest is optional.

The generated Kotlin mutation will look like:

data class UpdateUserMutation(
   val name: String,
   val lastName: String,
   val email: String
) : Mutation<UpdateUserMutation.Data, UpdateUserMutation.Data, Operation.Variables> { }

In result, when building the mutation object, all arguments will be required, as opposed to the the generated java counterpart:

Allowed generated java mutation:

val mutation = UpdateUserMutation.builder().name(name).build()

Not allowed generated kotlin mutation:

val mutation = UpdateUserMutation(name) //compiler complains about missing arguments

Preferred solution
It would be great if the above was supported for models generated in kotlin. This maybe can be achieved with default values for optional fields in the query/mutation constructor:

data class UpdateUserMutation(
   val name: String,
   val lastName: String? = null,
   val email: String? = null
) : Mutation<UpdateUserMutation.Data, UpdateUserMutation.Data, Operation.Variables> { }
Bug

Most helpful comment

Just to clarify for your example

mutation updateUser(
    $name: String!,
    $lastName: String,
    $email: String,
)

this is what will be generated on Kotlin:

data class UpdateUserMutation(
  val name: String,
  val lastName: Input<String>,
  val email:  Input<String>
)

The issue that we are missing default value for these inputs, the correct output should be:

data class UpdateUserMutation(
  val name: String,
  val lastName: Input<String> = Input.absent(),
  val email:  Input<String> = Input.absent()
)

I don't think this is correct to Kotlin. This would force us to write:

email = Input.optional(value)

The correct code generation should be the normal optional value:

data class UpdateUserMutation(
   val name: String,
   val lastName: String? = null,
   val email:  String? = null
)

All 5 comments

Just to clarify for your example

mutation updateUser(
    $name: String!,
    $lastName: String,
    $email: String,
)

this is what will be generated on Kotlin:

data class UpdateUserMutation(
  val name: String,
  val lastName: Input<String>,
  val email:  Input<String>
)

The issue that we are missing default value for these inputs, the correct output should be:

data class UpdateUserMutation(
  val name: String,
  val lastName: Input<String> = Input.absent(),
  val email:  Input<String> = Input.absent()
)

Has this fix been deployed? I am using version 1.2.2 and still get the error on missing arguments.

No, this is not released yet.

Thanks, Is there a timeline on when this might be released?

Just to clarify for your example

mutation updateUser(
    $name: String!,
    $lastName: String,
    $email: String,
)

this is what will be generated on Kotlin:

data class UpdateUserMutation(
  val name: String,
  val lastName: Input<String>,
  val email:  Input<String>
)

The issue that we are missing default value for these inputs, the correct output should be:

data class UpdateUserMutation(
  val name: String,
  val lastName: Input<String> = Input.absent(),
  val email:  Input<String> = Input.absent()
)

I don't think this is correct to Kotlin. This would force us to write:

email = Input.optional(value)

The correct code generation should be the normal optional value:

data class UpdateUserMutation(
   val name: String,
   val lastName: String? = null,
   val email:  String? = null
)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

doums picture doums  路  3Comments

moritzmorgenroth picture moritzmorgenroth  路  4Comments

gmrandom picture gmrandom  路  4Comments

romainpiel picture romainpiel  路  4Comments

rnitame picture rnitame  路  3Comments