Giter VIP home page Giter VIP logo

soda's Introduction

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

Simple Example

Target SDL

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

type Query {
    user(id: ID!): 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.id(of = _.id)
    t.prop("name", StringType, of = _.name)
  }
}

Query type

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

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) { 
      case Dfe(_, a, c) =>
        c.user(a.arg(id))
    }

    t.field("users", ListType(User.t)) {
      case Dfe(_, _, c) => c.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.

soda's People

Contributors

d-exclaimation avatar

Stargazers

 avatar

Watchers

 avatar

soda's Issues

Feature Request: Relay support

Description

Add support for conveniently building Relay spec compliance server.

Tasks

  • SodaRelayNode Traits and GraphQL Interfaces.
  • SodaRelayConnection Abstract class.
  • SodaRelayEdge Abstract class.
  • SodaPageInfo Abstract class.
  • SodaQuery's node field shorthand.
  • SodaRelay Abstract class combining all given abstract classes to automatically create a full Node, Connection, and Edges.

Depreciation and Other directives

Problem

Schema building is missing directives capabilities especially deprecated

type Query {
  oldField: String! @deprecated(reason: "Old")
  newField: String!
}

Solution

Add deprecation capabilities to t.field and t.prop

  def field[Out, Res](
    name: String,
    fieldType: OutputType[Out],
    desc: String = "",
    args: List[Argument[_]] = Nil,
    tags: List[FieldTag] = Nil,
    deprecated: Option[String] = None,
  )(resolve: Context[Ctx, Val] => Action[Ctx, Res])
    (implicit ev: ValidOutType[Res, Out]): Unit = {
    typedefs.addOne(
      Field(
        name = name,
        fieldType = fieldType,
        description = if (desc.isEmpty) None else Some(desc),
        arguments = args,
        tags = tags,
        deprecationReason  = deprecated,
        resolve = c => resolve(c)
      )
    )
  }

Not sure if prop should get deprecated capabilities to opt for suggesting using t.field for deprecated field.
So when a field is being deprecated, turn t.prop into t.field.

Documentation

Either markdowns and an actual documentation website for once.

Scalar and Arguments types

A simple utility functions to make arguments and scalars.

def arg(name: String, argType: InputType[_]): Argument[_] = ???

def $(name: String, argType: InputType[_]): Argument[_] = ???

def args(a: Argument[_]*): List[Argument[_]] = ???

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.