bare-graphql ⚛️

Bare GraphQL is a very small set of APIs to execute GraphQL in a completely unsafe manner.

Mainly made to be used from Kotlin scripts like update-graphql-schema.main.kts.

For other projects, Apollo Kotlin provides codegen and way more compile time guarantees.

Installation

@file:DependsOn("net.mbonnin.bare-graphql:bare-graphql:0.0.2")

Usage

Get the description of a Github Repository:

val query = """
    {
      repository(owner: "martinbonnin", name: "bare-graphql") {
        description
      }
    }
""".trimIndent()

val headers = mapOf("Authorization" to "bearer $token")

val description = graphql(
    url = "https://api.github.com/graphql",
    operation = query, 
    headers = headers
)["data"].asMap["repository"].asMap["description"]

println(description)
// "a **very small** set of APIs to execute GraphQL in a completely unsafe manner"

It also works with variables:

val query = """
    query(${'$'}owner: String!, ${'$'}name: String!){
      repository(owner: ${'$'}owner, name: ${'$'}name) {
        description
      }
    }
""".trimIndent()

val headers = mapOf("Authorization" to "bearer $token")

val variables = mapOf(
    "owner" to "martinbonnin",
    "name" to "bare-graphql",
)

val description = graphql(
    url = "https://api.github.com/graphql",
    operation = query,
    headers = headers
)["data"].asMap["repository"].asMap["description"]

println(description)
// "a **very small** set of APIs to execute GraphQL in a completely unsafe manner"

That’s it 🙂

GitHub

View Github