GraphQL
&
Ratpack@marioggar
1
Hey Hi
Mario Garcia
Twitter: @marioggar
Github: mariogarcia
Currently working for: kaleidos.net
Member of Madrid GUG
2 . 1
Why this talk ?
2 . 2
3 . 1
Today…
GraphQL theory
GraphQL + Groovy
GraphQL + Ratpack
Summary
3 . 2
GraphQL Theory
4 . 1
What is GraphQL ?
Is a query language
It uses a type system to de ned those queries
4 . 2
What is NOT ?
It’s not a web framework
It doesn’t have to do with HTTP necessarily
And de nitely…
GraphQL is not REST
4 . 3
5 . 1
To exec your 1st GraphQL query
De ne Data Types
De ne Query Types
De ne Schema
Execute queries against the de ned schema
5 . 2
De ne Data Types
type Contestant {
name: String
}
type Raffle {
id: String
title: String
contestants: [Contestant]
noWinners: Int
}
http://facebook.github.io/graphql/
5 . 3
De ne Queries
Query is just another type
It de nes all possible queries over de ned types
Queries can use other types or scalars
Queries may have arguments
type Query {
list: [Raffle]
getWinners(raffleId: String!): [Contestant]
}
5 . 4
De ne Schema
Which is the type for queries
Which is the type for mutations
type schema {
query: Query
mutation: Mutation
}
5 . 5
Execute a query
query: "give me (id, title) of all raf es
result: list of available raf es
{
list {
id
title
}
}
{
"data": {
"list": [
{
"id": "pfoqjfq09w8jf",
"title": "A Greach T-Shirt"
}
]
}
}
5 . 6
Validation
raf eId was mandatory
Invalid query
type Query {
...
winners(raffleId: String!): [Contestant]
...
}
{
winners {
name
}
}
5 . 7
5 . 8
GraphQL and JVM
https://github.com/graphql-java
5 . 9
5 . 10
First Groovy example
Execute a simple query
GQL ( )
DSL on top of
https://github.com/grooviter/gql
GraphQL-Java
5 . 11
GOTO code
5 . 12
Time to expose our schema
through HTTP
5 . 13
6 . 1
HTTP + GraphQL
6 . 2
Resources vs Queries (I)
6 . 3
Resources FrontEnd
Has to coordinate data coming from different resources
Several calls to different endpoints
6 . 4
Resources Backend
New controller/handler for every new resource
Implement different data views
Input formal validation
6 . 5
Resources vs Queries (II)
6 . 6
Faster interaction w Front End
Queries and Mutations can be batched
Queries are executed in parallel
No more controllers / handlers
GraphQL engine takes care of input formal validation
6 . 7
6 . 8
Ratpack + GraphQL
http://ratpack.io
6 . 9
Why Ratpack ?
Is lightweight
It is a lean and powerful foundation, not an all-
encompassing framework.
Very good performance
Netty + Ratpack’s execution model
6 . 10
Exposing GraphQL with Ratpack
gql-ratpack
Module of GQL project
It provides:
GraphQL endpoint
G hiQ l
https://grooviter.github.io/gql/docs/html5/index.html#_ratpack
6 . 11
Documentation: GraphiQL
Single Page Application (React)
Tool for prototyping queries
Connects to a existing GraphQL/HTTP engine
6 . 12
Documentation: Alternatives
https://nordicapis.com/10-graphql-consoles-in-action/
6 . 13
GOTO code
6 . 14
7 . 1
Ratpack's execution model
7 . 2
Ratpack's execution model (II)
7 . 3
Honor Ratpack's executors
CompletableFuture can be used as a service return type
Access executors via ratpack.exec.Execution
Execute CompletableFuture using the proper executor
Create your own abstractions
7 . 4
Abstractions
@Override
CompletableFuture<List<Map>> listCookies(DataFetchingEnvironment env) {
Selectors.ListCookiesParams params = Selectors.listCookiesParams(env)
return Futures.blocking { // <-- blocking operation
repository.list(params)
}
}
7 . 5
8 . 1
Error Handling
REST ⇒ HTTP codes
GraphQL ⇒ rich messages
8 . 2
Error vs Exception
graphql.GraphQLError
has meaninful error data to the client
graphql.GraphQLException
has more information for developers
8 . 3
Error
{
"data": null,
"errors": [
{
"message": "Validation error of type MissingFieldArgument: Missing
"locations": [
{
"line": 2,
"column": 3
}
],
"description": "Missing field argument raffleId",
"validationErrorType": "MissingFieldArgument",
"errorType": "ValidationError",
"path": null,
"extensions": null
}
8 . 4
Exception
{
"data": {
"winners": null
},
"errors": [
{
"message": "Exception while fetching data (/winners) : null",
"path": [
"winners"
],
"exception": {
"cause": {
"cause": null,
"stackTrace": [
{
"methodName": "newInstance0",
"fileName": "NativeConstructorAccessorImpl.java",
8 . 5
Error >> Exception
Throwing an exception is expensive
Returning an error is just a simple data structure
8 . 6
Error >> Exception (II)
You need to be aware of instrumentations
8 . 7
8 . 8
Instrumentations
Middleware executed before/after elds and data fetchers
Error handling, security, tracing… you name it!
Can be chained
Can modify the behavior of the execution ow
8 . 9
Instrumentations
8 . 10
A good example… security
8 . 11
9 . 1
Security
Did I mention that GraphQL is a query language ?
It knows nothing about security mechanisms
We have to make the engine aware of security…
Using the Context
9 . 2
Context
Can carry information from outside to the GraphQL
execution ow
Can be any type of data structure (Map, List…)
Available through all the execution ow (instrumentations
and data fetchers)
9 . 3
Two strategies
9 . 4
Using Instrumentation
More control
Harder to integrate with Ratpack’s execution model
9 . 5
Using REST + Instrumentation
You can use already implemented integrations (Pac4j)
Easier to integrate with Ratpack’s execution runtime
9 . 6
Using REST + Instrumentation…
gql-ratpack embraces this choice
gql.ratpack.pac4j.GraphQLHandler
9 . 7
GOTO code
9 . 8
What is left ?
10 . 1
10 . 2
A lot but…
10 . 3
Relay
GraphQL good practices
https://facebook.github.io/relay/
http://graphql.org/learn/best-practices/
10 . 4
Alternatives to Ratpack + GQL
gorm-graphql with/without Grails, Micronaut…
If you like GORM, and ts your plan, use it!
graphql-java
DIY
https://github.com/grails/gorm-graphql
h // h l j d h d i / /l / 10 . 5
11 . 1
Summary: GraphQL
Is not a silver bullet
Is an improvement between back/front
GraphQL It’s not limited to HTTP
As a query language, is still underestimated
11 . 2
Questions ?
11 . 3
Thanks!
11 . 4

GraphQL & Ratpack