d-exclaimation / soda

GraphQL Schema tooling for Scala, built on Sangria

Version Matrix

Soda

A GraphQL Schema Tooling to make schema composing in Scala more convenient, built on Sangria.

Setup

Latest Published Version: 0.5.0

"io.github.d-exclaimation" %% "soda" % latestVersion

Usage/Examples

Interoperability with Sangria

Soda is built on Sangria, you can use Soda with existing Sangria schema. Even if you don't want to use the Schema tooling, you can still take advantage some features of Soda

Read more:

Quick Start

Example Schema

Target SDL

type User {
    id: String!
    name: String!
}

type Query {
    user(id: String!): User
    users: [User!]!
}

User

import io.github.dexclaimation.soda.derive.obj
import io.github.dexclaimation.soda.schema.SodaObject
import sangria.schema.StringType

case class User(id: String, name: String)

// Using macro (More abstraction & limitations, faster to write)
object User {
  final val t = obj[Repo, User]()
}

// Using regular traits (Clear, easier to debug, slower to write)
object User extends SodaObject[Repo, User]("User") {
  def definition: Def = { t =>
    t.prop("id", StringType, of = _.id)
    t.prop("name", StringType, of = _.name)
  }
}

Query type

import io.github.dexclaimation.soda.schema._
import sangria.schema.{IDType, OptionType, ListType}

class Repo {
  private val Users = Map("1" -> User("1", "Bob"))

  def user(id: String): Option[User] =
    Users get id

  def products: List[Product] = Users.values.toList
}

object UserQuery extends SodaQuery[Repo, Unit] {
  val id = $("id", IDType)

  def definition: Def = { t =>
    t.field("user", OptionType(User.t), args = id :: Nil) { c =>
      c.ctx.user(c.arg(id))
    }

    t.field("users", ListType(User.t)) {
      _.ctx.users
    }
  }
}

Get the final schema

import io.github.dexclaimation.soda.core.SchemaDefinition.makeSchema

val schema = makeSchema(UserQuery.t)

Feedback

If you have any feedback, please reach out to me through the issues tab or Twitter @d_exclaimation

Acknowledgements

This package is inspired by Nexus.

Icons made by flaticon