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

Create a ktor-plugin for easy setup

Open jmfayard opened this issue 3 years ago • 3 comments

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 :

  • add a dependency to a plugin
  • install(ThePlugin)
  • with a declarative configuration lambda
  • abstract away implementation details

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:

  • https://ktor.io/docs/cors.html#configure (example on how to configure the CORS plugin)
  • https://ktor.io/docs/custom-plugins.html
  • https://ktor.io/docs/custom-plugins-base-api.html

jmfayard avatar Jun 16 '22 14:06 jmfayard

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

jmfayard avatar Jul 04 '22 13:07 jmfayard

@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?

dariuszkuc avatar Jul 04 '22 18:07 dariuszkuc

@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.

samuelAndalon avatar Jul 05 '22 02:07 samuelAndalon