ExpediaGroup / graphql-kotlin

Libraries for running GraphQL in Kotlin
https://opensource.expediagroup.com/graphql-kotlin/
Apache License 2.0
1.73k stars 344 forks source link

Create a ktor-plugin for easy setup #1471

Closed jmfayard closed 1 year ago

jmfayard commented 2 years ago

Is your feature request related to a problem? Please describe.

We use graphql-kotlin with ktor at https://tignum.com/ and we really like it.

The initial setup OTOH is not so nice. It made my head explode when I first set-it up, and then it made the head of my colleagues explode when I explained it to them.

The ktor way to install new feature is :

Describe the solution you'd like

I had a shot at what such a plugin could look like.

Have a look at https://github.com/ExpediaGroup/graphql-kotlin/compare/master...jmfayard:ktor-plugin?expand=1

The simplest setup for graphql-kotlin/examples/server/ktor-server would be:

// build.gradle.kts
implementation("com.expediagroup", "graphql-kotlin-ktor-server", latestVersion)

and your main file:

fun Application.graphQLModule() {
    install(Routing)
    install(ContentNegotiation) {
        jackson()
    }
    install(GraphQLKotlin) {
        queries = listOf(
            HelloQueryService(),
            BookQueryService(),
            CourseQueryService(),
            UniversityQueryService(),
        )
        mutations = listOf(
            LoginMutationService()
        )
        schemaGeneratorConfig = SchemaGeneratorConfig(
            supportedPackages = listOf(
                "com.expediagroup.graphql.examples.server.ktor"
            ),
        )
     }   
}        

For a more advanced setup you could use those optional parameters

fun Application.graphQLModule() {
    // ...
    install(GraphQLKotlin) {
       // Required settings: 
       // see above

       // OPTIONAL SETTINGS
       generateContextMap { request: ApplicationRequest ->
            mapOf(
                User::class to generateUser(request),
                MyHeaders::class to generateMyHeaders(request)
            )
        }
        configureGraphQL {
            valueUnboxer(IDValueUnboxer())
        }
        endpoints {
            graphql = "graphql"
            sdl = "sdl"
            playground = "playground"

            enableSdl = true
            enablePlayground = true
        }
    }    
}

Describe alternatives you've considered

The alternative is to currently copy-paste the boilerplate from graphql-kotlin/examples/server/ktor-server and try to figure what GraphQlContextFactory, KtorGraphQLRequestParser, KtroGraphQLSchema, KtorGraphQlSchema, KtorServer and the likes are doing.

Additional context

Ktor documentation:

jmfayard commented 2 years ago

Hello any thoughts on this? Even if you don't have the bandwith to implelment it, would be nice to know if such a plugin would go in the right direction

dariuszkuc commented 2 years ago

@jmfayard I think it's a great idea but its hihgly unlikely to be implemented anytime soon* (I am no longer with Expedia so may be wrong)

*unless someone from community contributes a PR. I think your example in the issue is a good start - maybe you could open a PR?

@samuelAndalon @tapaderster any thoughts on your side?

samuelAndalon commented 2 years ago

@jmfayard the idea sound really cool, as currently we only support out of the box auto-configuration for spring server, if we implement a ktor plugin would be super beneficial for the ktor community. Feel free to continue working on it/open a PR we will for sure take a look at it.