apollo-kotlin
apollo-kotlin copied to clipboard
How to generate Query Classes with direct values in Constructor not as an Optional Types ?
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 ?
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!) {
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 ?
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 .. Wow. This is what I expected. Thanks
@RageshAntony do you mind if I close this one?
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: @.***>