apollo-kotlin icon indicating copy to clipboard operation
apollo-kotlin copied to clipboard

How to generate Query Classes with direct values in Constructor not as an Optional Types ?

Open RageshAntony opened this issue 3 years ago • 4 comments

Question

Enter your question here.

Apollo KMM is generating Query Classes as Optional Types ..

public data class LoginUserQuery(
  public val phone_no: Optional<String?> = Optional.Absent,
  public val password: Optional<String?> = Optional.Absent,
.....

This makes me to call the constructor like LoginUserQuery(phone_no = Optional.presentIfNotNull("999999"),password = Optional.presentIfNotNull("ddd"))

I need something like

    LoginUserQuery(phone_no = "999999",password = "ddd")

Is there any configuration to generate like above ?

RageshAntony avatar Aug 10 '22 05:08 RageshAntony

Hi!

If you declare your variables as non nullable (with !) they won't be generated as optional. Something like:

query LoginUserQuery($phone_no: String!, $password: String!) {

BoD avatar Aug 10 '22 07:08 BoD

Hi!

If you declare your variables as non nullable (with !) they won't be generated as optional. Something like:

query LoginUserQuery($phone_no: String!, $password: String!) {

Yeah. Working Now. Thanks .

But is there any way to do the same for nullable , like generating classes with default value constructs ..

public data class LoginUserQuery( public val phone_no: String? = null, public val password: String, )

like this ?

RageshAntony avatar Aug 10 '22 08:08 RageshAntony

You can define a default value on query arguments, like this:

query LoginUserQuery($phone_no: String = null, $password: String!) {

this will generate something like:

public data class LoginUserQuery(
  public val phone_no: Optional<String?> = Optional.Absent,
  public val password: String,
)

Note, the reason Optional is generated by default is because a backend may distinguish:

  • no value
  • a present value which is null

If that's preferable, your can configure Apollo to generate a nullable type instead, by either adding @optional(if: false) on a specific argument, or globally with generateOptionalOperationVariables.set(false) - more info about this here. But note that if you use a default value as above, it will still be generated with Optional.

BoD avatar Aug 10 '22 09:08 BoD

@BoD .. Wow. This is what I expected. Thanks

RageshAntony avatar Aug 10 '22 09:08 RageshAntony

@RageshAntony do you mind if I close this one?

martinbonnin avatar Sep 11 '22 20:09 martinbonnin

No problem. Please proceed 😊

On Mon, 12 Sep, 2022, 1:32 AM Martin Bonnin, @.***> wrote:

@RageshAntony https://github.com/RageshAntony do you mind if I close this one?

— Reply to this email directly, view it on GitHub https://github.com/apollographql/apollo-kotlin/issues/4327#issuecomment-1243032903, or unsubscribe https://github.com/notifications/unsubscribe-auth/AITNQ5YZWJIDQN6V3NG5SYDV5Y3GPANCNFSM56DEPKLQ . You are receiving this because you were mentioned.Message ID: @.***>

RageshAntony avatar Sep 12 '22 05:09 RageshAntony