1 of 40

GraphQL in Go: 👍 or 👎?

Paweł Kosiec

2 of 40

Agenda

  • Introduction to GraphQL
  • Our case, long story short
  • Choosing right solution
  • Wrapping up�

3 of 40

Quick introduction to GraphQL

4 of 40

What is GraphQL?

  • Query language for API
  • Complete server-side runtime
  • Interprets a string by a server and returns required data in a specified format
  • Ideal for API used by UIs
  • Can be built on top of REST API
  • Multiple implementations (Node.js, Java, Go…)

5 of 40

What are the benefits of using GraphQL?

  • Strongly typed schema
  • No overfetching and underfetching
  • Better database queries efficiency
  • Support for real-time data
  • Great open source community
  • Autogenerated API documentation

6 of 40

How it works?

As API provider:

  • Describe your data

As API client:

  • Ask for what you need
  • You get exactly that, nothing more

7 of 40

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

}

8 of 40

Example (server-side)

Type definition

type Person {

id: ID

name: String

age: Int

}

Query definition

type Query {

examplePerson: Person

}

9 of 40

Example (client-side)

{

examplePerson {

id

name

}

}

{

"examplePerson": {

"id": "55b1e3ca",

"name": "John Doe"

}

}

QUERY

RESULT

10 of 40

Example (client-side)

{

examplePerson {

name

age

}

}

{

"examplePerson": {

"name": "John Doe",

"age": 35,

}

}

QUERY

RESULT

11 of 40

Example (server-side)

type Artist {

id: ID!

name: String!

songs: [Song!]!

}

type Song {

id: ID!

title: String!

duration: Float!

}

type Query {

artists: [Artist!]!

}

12 of 40

Example (client-side)

{

artists {

name

songs {

title

}

}

}

{

"artists": [{

"name": "Krzysztof Krawczyk",

"songs": [{

"title": "Mój przyjacielu"

},

{

"title": "Parostatek"

}

]

},

{

"name": "Unknown Artist",

"songs": []

}]

}

QUERY

RESULT

13 of 40

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

}

14 of 40

Example (client-side)

{

artist(id: "404") {

name

songs {

title

}

}

}

{

"artist": null

}

QUERY

RESULT

15 of 40

GraphQL API guidelines

  • One single HTTP endpoint
  • JSON response
  • No versioning
  • Nullability
  • Server-side caching

16 of 40

Operation types

  • Query - fetching data
  • Mutation - creating/updating/deleting data

mutation addArtist(name: "Dire Straits") {

id

}

  • Subscription - server-side data pushing

17 of 40

Other GraphQL features

  • Input types - complex objects as arguments
  • Fragments - reusable units of queries
  • Directives - for constructing dynamic queries
  • and more...

18 of 40

Who is using GraphQL?

19 of 40

GraphQL is

20 of 40

Our case, long story short

21 of 40

A little bit of context

  1. We had GraphQL API server written in Node.js (MVP)
  2. It was an API for our UIs
  3. It needed refactoring & advanced caching
  4. We’ve seen benefits of using Go
  5. We had a requirement to use Go for beta stage

22 of 40

Looking for best possible solution (Q1 2018)

  • graphql-go/graphql (maintained, all features we need in place, most popular)
  • neelance/graphql-go (abandoned by author which admitted that this is not a Go-idiomatic library; community fork not actively maintained)
  • vektah/gqlgen (one-man project, still immature, many required features missing)

23 of 40

Looking for best possible solution (Q1 2018)

  • graphql-go/graphql (maintained, all features we need in place, most popular)
  • neelance/graphql-go (abandoned by author which admitted that this is not a Go-idiomatic library; community fork not actively maintained)
  • vektah/gqlgen (one-man project, still immature, many required features missing)

24 of 40

Taking the most popular solution:

GraphQL Go

25 of 40

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

}

26 of 40

GraphQL Go library: Demo

27 of 40

GraphQL Go - summary

  • No schema definition by SDL - Runtime types
  • A lot of boilerplate
  • No typed resolvers
  • You cannot easily have type definition in one place
  • Inconvenient unit testing

28 of 40

We were all like:

29 of 40

Looking for best possible solution (Q1 2018)

  • graphql-go/graphql (maintained, all features we need in place, most popular)
  • neelance/graphql-go (abandoned by author which admitted that this is not a Go-idiomatic library; community fork not actively maintained)
  • vektah/gqlgen (one-man project, still immature, many required features missing)

30 of 40

Looking for best possible solution (Q1 2018)

  • graphql-go/graphql (maintained, all features we need in place, most popular)
  • neelance/graphql-go (abandoned by author which admitted that this is not a Go-idiomatic library; community fork not actively maintained)
  • vektah/gqlgen (rapid development, subscriptions, custom scalars & all other features in place)

31 of 40

Looking for best possible solution (Q1 2018)

  • graphql-go/graphql (maintained, all features we need in place, most popular)
  • neelance/graphql-go (abandoned by author which admitted that this is not a Go-idiomatic library; community fork not actively maintained)
  • vektah/gqlgen (rapid development, subscriptions, custom scalars & all other features in place)

32 of 40

Generating code from GraphQL schema

33 of 40

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

}

34 of 40

GQLGen library: Demo

35 of 40

GQLGen - summary

  • No boilerplate needed; schema definition by SDL
  • Generated structs speed up work a lot
  • Typed resolvers
  • Easy unit testing (resolver is just a method)
  • Graphcool Playground console - much more powerful than GraphiQL
  • Actively maintained

36 of 40

Wrapping up

37 of 40

GraphQL in Go is *

��* - if you use proper library

38 of 40

Summary

  • GraphQL is really convenient solution for some use cases
  • Most popular solution isn’t always the best
  • Don’t try to write GraphQL objects by yourself
  • Generating code from GraphQL schema saves a lot of time
  • GraphQL in Go is as fun as in Node.js with Apollo

39 of 40

Sources of sample apps

40 of 40

Thanks for attention.

Any questions?