Home > Enterprise >  How to cast String to Input<String> - Kotlin
How to cast String to Input<String> - Kotlin

Time:03-25

I am using GraphQL Apollo client call and it generates files. So as a result I got this

val storeNumber: Input<String> = Input.absent()

Instead of regular string. So how can I cast parameter to Input<String> to avoid this error

enter image description here

CodePudding user response:

I don't use Apollo, but found the source code of Input. It depends what version of this library you're using. If you're using an older version, to wrap (not "cast"!) a String as an Input, use Input.Present:

storeNumber = Input.Present(storeNumber)

Note, the term "cast" means promising the compiler that your existing instance is also an instance of something else. That is very different from converting or wrapping an instance of something.

If you're using a newer version of the library, you shouldn't be using the Input class at all. It's been replaced with the Optional class, in which case you would use Optional.Present(storeNumber).

If you want to figure this kind of thing out on your own in the future, try Ctrl Clicking the function you're working with to jump to its source code. In turn you can Ctrl Click the types of the function parameters. That would take you to the source code of Input so you could see how to create an instance.

  • Related