Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document Kotlin Support #1853

Open
NfNitLoop opened this issue Jun 9, 2023 · 5 comments
Open

Document Kotlin Support #1853

NfNitLoop opened this issue Jun 9, 2023 · 5 comments

Comments

@NfNitLoop
Copy link

NfNitLoop commented Jun 9, 2023

Hello! Thanks for this project! We're enjoying using it.

We'd like to use Kotlin to define our GraphQL models and implementation. But I don't see documentation about what Kotlin features are supported or not supported by SmallRye.

I think I've figured out the list below by trial an error, but it would be nice to have them documented somewhere as explicitly supported/unsupported. Can someone confirm these? 😊

  • SmallRye uses JSON-B for deserialization, so if you use any data classes as inputs, they'll need a @JsonbCretor annotation on their constructor. (example below)
  • SmallRye supports Kotlin's nullability annotations (code) and will
    • properly propagate them through schema generation
    • validate non-null inputs and provide proper error messages. (i.e.: not just kotlin null checks failing)
  • Unlike what I read elsewhere, I don't seem to need to annotate each constructor parameter with @JsonbProperty("names"), that seems to be handled automatically. (Is this handled by SmallRye? Or its JSON-B library? Is that a feature I can rely on?)
  • SmallRye doesn't support Kotlin default parameters when deserializing objects.
// Sample kotlin code showing required annotations.
data class Foo @JsonbCreator constructor(
    val name: String,
    val bar: Bar?,
)

data class Bar @JsonbCreator constructor(
    val name: String
)

@GraphQLApi
class MyService {
    @Query("foo")
    @Description("GQL Docs here")
    fun getFoo() = Foo(
            name = "My foo",
            bar = Bar(
                name = "Hello from Bar!"
            ),
    )

    @Mutation("foo")
    // Nice: I get to use Foo as an Input type too. ❤️
    fun setFoo(foo: Foo): String {
        val msg = "Got foo: $foo"
        println(msg)
        return msg
    }
}
@t1
Copy link
Collaborator

t1 commented Jun 10, 2023

@NfNitLoop: thanks for your input. Would you like to turn this into a PR for a new page in the docs folder?

BTW: you don't have to add @JsonProperty to parameters, probably because we use the parameter names that can be compiled into the bytecode; javac does this with the -parameters flag, maybe Kotlin does this out of the box.

@NfNitLoop
Copy link
Author

NfNitLoop commented Jun 12, 2023

I'm not quite ready to turn it into a PR w/o knowing answers to some of the questions I asked:

  • Is the automatic handling of data class constructor field names a feature SmallRye intends to support?

I guess the main underlying question is, in general: What level of Kotlin compatibility does the project aim to provide? I can't answer that as someone who is not a main contributor. But it would be nice to have that stated somewhere so folks know what to expect. 😊

For context: my team is trying to decide whether to go all-in on Kotlin data models. And it's a bit more difficult to make that recommendation when the project we'll be using them with doesn't even mention Kotlin in its docs. (Yes, in theory, it shouldn't matter, because Kotlin runs on the JVM, but w.r.t. serialization it apparently does.)

@phillip-kruger
Copy link
Member

Hi @NfNitLoop - none of the main contributors know Kotlin. SmallRye GraphQL has no intention w.r.t Kotlin, we do what we can with supporting Kotlin. If currently data class constructor field names is NOT supported, then you are most welcome to open a PR that adds support for it. We will definitely consider it. As long as a PR does not add a bunch of new dependencies or rewrite a bunch of things, there should be no reason not to accept a feature. This project can do with a contributor that has knowledge and passion for Kotlin...

@jmartisk
Copy link
Member

Yeah. I like Kotlin but I am not very skilled with it. Any contributions to make us support it better are welcome. I can also imagine a Kotlin-based test suite, that would be nice.

@danielspeixoto
Copy link

Hi @NfNitLoop, having the same issue around here. I am still deciding if we are going to use data classes or not. What you ended up doing?
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants