GraphQL in Go: 👍 or 👎?
Paweł Kosiec
Agenda
Quick introduction to GraphQL
What is GraphQL?
What are the benefits of using GraphQL?
How it works?
As API provider:
As API client:
How to describe the data?
Use Schema Definition Language to define types and its fields. Then, write resolvers.
type Person {
id: ID
name: String
age: Int
}
Example (server-side)
Type definition
type Person {
id: ID
name: String
age: Int
}
Query definition
type Query {
examplePerson: Person
}
Example (client-side)
{
examplePerson {
id
name
}
}
{
"examplePerson": {
"id": "55b1e3ca",
"name": "John Doe"
}
}
QUERY
RESULT
Example (client-side)
{
examplePerson {
name
age
}
}
{
"examplePerson": {
"name": "John Doe",
"age": 35,
}
}
QUERY
RESULT
Example (server-side)
type Artist {
id: ID!
name: String!
songs: [Song!]!
}
type Song {
id: ID!
title: String!
duration: Float!
}
type Query {
artists: [Artist!]!
}
Example (client-side)
{
artists {
name
songs {
title
}
}
}
{
"artists": [{
"name": "Krzysztof Krawczyk",
"songs": [{
"title": "Mój przyjacielu"
},
{
"title": "Parostatek"
}
]
},
{
"name": "Unknown Artist",
"songs": []
}]
}
QUERY
RESULT
Example (server-side)
type Artist {
id: ID!
name: String!
songs: [Song!]!
}
type Song {
id: ID!
title: String!
duration: Float!
}
type Query {
artist(id: ID!): Artist
}
Example (client-side)
{
artist(id: "404") {
name
songs {
title
}
}
}
{
"artist": null
}
QUERY
RESULT
GraphQL API guidelines
Operation types
mutation addArtist(name: "Dire Straits") {
id
}
Other GraphQL features
Who is using GraphQL?
GraphQL is
Our case, long story short
A little bit of context
Looking for best possible solution (Q1 2018)
Looking for best possible solution (Q1 2018)
Taking the most popular solution:
GraphQL Go
Schema of sample app
type Artist {
id: ID!
name: String!
songs: [Song!]!
}
type Song {
id: ID!
title: String!
duration: Float!
}
type Query {
artists: [Artist!]!
artist(id: ID!): Artist
}
GraphQL Go library: Demo
GraphQL Go - summary
We were all like:
Looking for best possible solution (Q1 2018)
Looking for best possible solution (Q1 2018)
Looking for best possible solution (Q1 2018)
Generating code from GraphQL schema
Schema of sample app
type Artist {
id: ID!
name: String!
songs: [Song!]!
}
type Song {
id: ID!
title: String!
duration: Float!
}
type Query {
artists: [Artist!]!
artist(id: ID!): Artist
}
GQLGen library: Demo
GQLGen - summary
Wrapping up
GraphQL in Go is *
��* - if you use proper library
Summary
Sources of sample apps
Thanks for attention.
Any questions?