Code Monkey home page Code Monkey logo

scalajs-graphql's Introduction

scalajs-graphql

Build Status

scalajs-graphql provides GraphQL clients and utilities, in a type-safe manner, for Scala.js.

Disclaimer: scalajs-graphql has not been released and is under heavy development at the moment. You could still publish locally to use it, but keep in mind that something may not work as expected. We are working to release the very first version soon.

Features

  • GraphQL clients
  • UI framework integrations
  • Scala code generation (generating case classes, traits, etc.)
    • Schema
    • Operations (queries, mutations, subscriptions)
    • Fragments
    • Input types
  • Server-side rendering
  • Testing utilities

Installation

As scalajs-graphql is not released yet, you have to build locally to use it.

$ git clone https://github.com/ngthanhtrung/scalajs-graphql.git
$ cd scalajs-graphql
$ sbt publishLocal

Add this to your project/plugins.sbt file:

addSbtPlugin("com.ngthanhtrung" % "sbt-graphql-codegen" % "0.1.0-SNAPSHOT")

You can then configure where scalajs-graphql puts generated GraphQL classes in your project settings in build.sbt:

graphqlCodegenPackage := Some("com.example")

Usage

Say you have this schema definition in src/main/resources/schema.graphql:

type Name {
  firstName: String!
  lastName: String
}

enum Gender {
  MALE
  FEMALE
  UNKNOWN
}

type Person {
  name: Name!
  gender: Gender!
  age: Int
}

type Query {
  peopleByGender(gender: Gender!): [Person]
}

schema {
  query: Query
}

You can write your own query in src/main/resources/people-by-gender-query.graphql like this:

query PeopleByGender($gender: Gender!) {
  peopleByGender(gender: $gender) {
    name {
      firstName
      lastName
    }
    gender
    age
  }
}

scalajs-graphql will automatically generate your query structure in Scala:

package com.example

// Some parts are omitted for brevity

sealed abstract class Gender

object Gender {
  case object MALE extends Gender
  case object FEMALE extends Gender
  case object UNKNOWN extends Gender
}

// Note that "Query" suffix is added to your query name
object PeopleByGenderQuery {

  final case class Variables(gender: Gender)

  type Props = ApolloQueryProps[Data]

  final case class Data(
    peopleByGender: Option[List[Data.PeopleByGender]]
  )(raw: js.Any)

  object Data {

    final case class PeopleByGender(
      name: PeopleByGender.Name,
      gender: Gender,
      age: Option[Int]
    )(raw: js.Any)

    object PeopleByGender {

      final case class Name(
        firstName: String,
        lastName: Option[String]
      )(raw: js.Any)
    }
  }
}

Now you are able to write your React component in a type-safe way:

// Define your React component
val component = ScalaComponent
  .builder[PeopleByGenderQuery.Props]("PeopleByGender")
  .render_P { props =>
    props.data.personByGender.fold(
      <.div("Error occurred or data is not available yet.")
    ) { people =>
      people.toVdomArray { person =>
        <.div(
          <.div(s"First name: ${person.name.firstName}"),
          person.name.lastName.whenDefined { lastName =>
            <.div(s"Last name: $lastName")
          },
          <.div(s"Gender: ${person.gender}"),
          person.age.whenDefined { age =>
            <.div(s"Age: $age")
          }
        )
      }
    }
  }
  .build

// Declare data query for your component
val graphqlComponent = ReactApollo.graphql(PeopleByGenderQuery).apply(component)

// Create an Apollo client to talk to a GraphQL server
val apolloClient = new ApolloClient(
  link = new ApolloHttpLink(
    // Change it to your server address
    uri = "http://localhost:6789/graphql"
  ),
  cache = new ApolloInMemoryCache()
)

// Put your component inside a GraphQL environment
val apolloProvider = ApolloProvider(apolloClient)(
  graphqlComponent(PeopleByGenderQuery.Variables(Gender.FEMALE))
)

// Render everything, you would need scala-js-dom for this
apolloProvider.renderIntoDOM(dom.document.getElementById("root"))

License

MIT licensed. See LICENSE.

scalajs-graphql's People

Contributors

ngbinh avatar superjenkins avatar trungfinity avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.