Created
August 31, 2015 20:59
-
-
Save dwhitney/dffafecace2baeefd0ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sangria.schema._ | |
import sangria.execution.Executor | |
import sangria.parser.QueryParser | |
import sangria.renderer.SchemaRenderer | |
import sangria.integration.PlayJsonSupport._ | |
import scala.util.{Success} | |
import scala.concurrent.ExecutionContext.Implicits.global | |
case class User(email: String, loginCount: Int = 0) | |
object UserRepo{ | |
import scala.collection.mutable.{Map => MMap} | |
val map = MMap[String,User]( | |
"[email protected]" -> User("[email protected]") | |
) | |
def byEmail(email: String) = map(email) | |
def loginPlusPlus(email: String) = { | |
val user = map(email) | |
map += (email -> user.copy(loginCount = user.loginCount + 1)) | |
map(email) | |
} | |
} | |
object SangriaMutation extends App{ | |
val UserObject = ObjectType("User", List[Field[Unit,User]]( | |
Field("email", StringType, resolve = _.value.email), | |
Field("loginCount", IntType, resolve = _.value.loginCount) | |
)) | |
val EMAIL = Argument("email", StringType) | |
val Query = ObjectType("Query", List[Field[Unit,Unit]]( | |
Field("byEmail", UserObject, arguments = EMAIL :: Nil, resolve = { ctx => UserRepo.byEmail(ctx arg EMAIL)}) | |
)) | |
val MutationTopLevel = ObjectType("Mutation", List[Field[Unit,Unit]]( | |
Field("loginPlusPlus", UserObject, arguments = EMAIL :: Nil, resolve = { ctx => UserRepo.loginPlusPlus(ctx arg EMAIL)}) | |
)) | |
val schema = Schema(query = Query, mutation = Some(MutationTopLevel), additionalTypes = List(UserObject)) | |
val Success(byEmailAst) = QueryParser.parse("""{ byEmail(email:"[email protected]"){email,loginCount}}""") | |
val Success(mutationAst) = QueryParser.parse("""mutation LoginPlusPlus { loginPlusPlus(email:"[email protected]"){loginCount}}""") | |
for{ | |
beforeMutationResult <- Executor.execute(schema = schema, queryAst = byEmailAst) | |
mutationResult <- Executor.execute(schema = schema, queryAst = mutationAst) | |
afterMutationResult <- Executor.execute(schema = schema, queryAst = byEmailAst) | |
} { | |
println(beforeMutationResult) | |
println(mutationResult) | |
println(afterMutationResult) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment