Jérémie Astori
24 March 2017 — NE Scala NYC
@astorije on
Twitter,
GitHub,
…
New to Scala / GraphQL / giving talks
/breweries?city=Brooklyn
[{
"name": "Brooklyn Brewery",
"address": "79 North Eleventh Street",
"website": "http://www.brooklynbrewery.com/"
}]
{
"breweries": [{
"name": "Brooklyn Brewery",
"address": "79 North Eleventh Street",
"website": "http://www.brooklynbrewery.com/",
"beers": [{
"name": "Black Chocolate Stout",
"abv": 10,
"description": "This is the famous Brooklyn […]",
"style": {
"name": "American-Style Stout"
}
}]
}]
}
{
"breweries": [{
"name": "Brooklyn Brewery",
"address": "79 North Eleventh Street",
"website": "http://www.brooklynbrewery.com/",
"beers": [{
"name": "Black Chocolate Stout",
"abv": 10,
"description": "This is the famous Brooklyn […]",
"style": {
"name": "American-Style Stout"
}
}]
}]
}
{
breweries {
name
address
website
beers {
name
abv
description
style {
name
}
}
}
}
{
breweries {
name
address
website
}
}
GraphQL
by Facebook
by Oleg Ilyenko
@easyangel
type Query {
# Returns a beer
beer(id: Int!): Beer
}
type Query {
# Returns a beer
beer(id: Int!): Beer
}
{
beer(id: 360) {
name
abv
}
}
val Id = Argument("id", IntType)
val QueryType = ObjectType("Query", fields[Repository, Unit](
Field("beer", OptionType(BeerType),
description = Some("Returns a beer"),
arguments = Id :: Nil,
resolve = c => c.ctx.beer(c.arg(Id))
)
))
type Style {
id: Int!
name: String!
beers: [Beer!]!
category: Category!
}
case class Style(
id: Int,
category_id: Int,
name: String
)
val StyleType = deriveObjectType[Repository, Style](
ObjectTypeDescription("A style of beer, […]"),
AddFields(Field("beers", ListType(BeerType),
resolve = c => c.ctx.beersByStyle(c.value.id)
)),
ReplaceField("category_id", Field("category", CategoryType,
resolve = c => c.ctx.category(c.value.category_id).get
))
)