Skip to content

Instantly share code, notes, and snippets.

@xsistens
Created September 21, 2015 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xsistens/38774f3718033354596e to your computer and use it in GitHub Desktop.
Save xsistens/38774f3718033354596e to your computer and use it in GitHub Desktop.
sangria
package graphql.schema
import graphql.SchemaDefinition.{HidingSpot, Game}
object Data {
class GameRepo {
var hidingSpots = Map[String, HidingSpot]("1" -> HidingSpot("1", false, true), "2" -> HidingSpot("2", false, false), "3" -> HidingSpot("3", false, false))
var game = Game("1", Seq("1", "2", "3"), 3)
var games = Map[String, Game]("1" -> game)
def checkHidingSpot(gameId: String, hidingSpotId: String) = hidingSpots.get(hidingSpotId).foreach { hs =>
hidingSpots = hidingSpots + (hidingSpotId -> hs.copy(hasBeenChecked = true))
games.get(gameId).foreach { g =>
games = games + ("gameId" -> game.copy(turnsRemaining = g.turnsRemaining-1))
}
}
def getGame(id: String) = games.get(id)
def getHidingSpot(id: String) = hidingSpots.get(id)
}
}
package graphql
import graphql.schema.Data.GameRepo
import sangria.relay._
import sangria.schema._
object SchemaDefinition {
/**
* We get the node interface and field from the relay library.
*
* The first method is the way we resolve an ID to its object. The second is the
* way we resolve an object that implements node to its type.
*/
val NodeDefinition(nodeInterface, nodeField) = Node.definition((id: GlobalId, ctx: Context[GameRepo, Unit]) => {
id.typeName match {
case "Game" => ctx.ctx.getGame(id.id)
case "HidingSpot" => ctx.ctx.getHidingSpot(id.id)
case _ => None
}
}, Node.possibleNodeTypes[GameRepo, Node](HidingSpotType))
case class HidingSpot(id: String, hasBeenChecked: Boolean, hasTreasure: Boolean)
case class Game(id: String, var hidingSpots: Seq[String], var turnsRemaining: Int) {
}
val HidingSpotType: ObjectType[Unit, HidingSpot] =
ObjectType(
"HidingSpot",
"A place where you might find treasure",
fields[Unit, HidingSpot](
Node.globalIdField[Unit, HidingSpot]("HidingSpot"),
Field("hasBeenChecked", BooleanType,
Some("True this spot has already been checked for treasure"),
resolve = _.value.hasBeenChecked),
Field("hasTreasure", BooleanType,
Some("The number of turns a player has left to find the treasure"),
resolve = _.value.hasTreasure)
),
interfaces[Unit, HidingSpot](nodeInterface))
val ConnectionDefinition(_, hidingSpotConnection) = Connection.definition[GameRepo, Connection, HidingSpot]("HidingSpot", HidingSpotType)
case class CheckHidingSpotForTreasureMutationPayload(clientMutationId: String, hidingSpotId: String) extends Mutation
val GameType: ObjectType[GameRepo, Game] =
ObjectType(
"Game",
"A treasure search game",
fields[GameRepo, Game](
Node.globalIdField[GameRepo, Game]("Game"),
Field("hidingSpots", hidingSpotConnection,
Some("Places where treasure might be hidden"),
arguments = Connection.Args.All,
resolve = ctx => Connection.connectionFromSeq(ctx.value.hidingSpots map ctx.ctx.getHidingSpot, ConnectionArgs(ctx))),
Field("turnsRemaining", IntType,
Some("The number of turns a player has left to find the treasure"),
resolve = _.value.turnsRemaining)
),
interfaces[GameRepo, Game](nodeInterface))
val CheckHidingSpotForTreasureMutation = Mutation.fieldWithClientMutationId[GameRepo, Unit, CheckHidingSpotForTreasureMutationPayload](
fieldName = "checkHidingSpotForTreasure",
typeName = "CheckHidingSpotForTreasure",
inputFields = List(
InputField("id", IDType)
),
outputFields = fields(
Field("hidingSpot", HidingSpotType, resolve = ctx => ctx.ctx.getHidingSpot(ctx.value.hidingSpotId).get),
Field("game", GameType, resolve = ctx => ctx.ctx.getGame("1").get)),
mutateAndGetPayload = (input, ctx) => {
val mutationId = input(Mutation.ClientMutationIdFieldName).asInstanceOf[String]
val hidingSpotId = input("id").asInstanceOf[String]
ctx.ctx.checkHidingSpot("1", hidingSpotId)
CheckHidingSpotForTreasureMutationPayload(mutationId, hidingSpotId)
}
)
val Query = ObjectType[GameRepo, Unit](
"Query", List[Field[GameRepo, Unit]](
nodeField,
Field("game", GameType,
resolve = (ctx) => ctx.ctx.getGame("1").get)
)
)
val Mutations = ObjectType("Mutation", fields[GameRepo, Unit](
CheckHidingSpotForTreasureMutation
))
val Schema = new sangria.schema.Schema(query = Query, mutation = Some(Mutations))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment