Skip to content

Create a ktor-plugin for easy setup #1471

@jmfayard

Description

@jmfayard

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions