1 of 12

GraphQL

Mike Peat, Unicorn InterGlobal, UK

2 of 12

How I got here

  • No, not the plane and train ride (and the accidental side-trip to Sweden)… how I got to where I am with GraphQL!
  • If you've read my article on the subject you already know, but for those who have not…
  • My marketing manager, Helen, wanted something interesting to put in our December newsletter for Christmas and she suggested I take a look at GraphQL, so I did (so: Why GraphQL? - Because Helen! 😀)

3 of 12

Where did GraphQL come from?

  • Facebook found that REST was too clumsy for their mobile app, so they decided to develop their own approach
  • In 2012, they started work on GraphQL and in 2015 released it
  • GraphQL is an alternative to REST (and older technologies such as SOAP) for building APIs
  • The core design imperative was mobile, where the end-user may be using the app on a phone with perhaps only a flakey 3G connection

4 of 12

What does REST being clumsy mean?

  • Over-fetching: more data returned than the app needs
  • Under-fetching: you can't get all the data the app needs in a single request
  • Multiple end-points: each "resource" has a different URL
  • What Facebook wanted was something more like an SQL interface, where all the data, and only the data, the app requested, no matter how complex and how many resources were involved, could be returned (or updated) from a single call

5 of 12

So how does it work?

  • Any given GraphQL API has only one URL and all requests go to that
  • Every request is an HTTP POST (well, mostly)
  • The details of the request go in the HTTP Body
  • There are three types of request:
    • Query (the default if no type is specified)
    • Mutation (the rest of CRUD: Create, Update, Delete)
    • Subscription

6 of 12

What does it look like?

Sample Query

Sample Response

{

user(id: 4) {

fullname

}

}

  • This kind of looks like JSON
  • Here I have added myself to the WebAppUser table

{

"user": {

"fullname": "Mike Peat"

}

}

  • This actually is JSON

7 of 12

Asking for a bit more

Sample Query

Sample Response

{

user(id: 4) {

loginName

fullname

password

rights

lastLogin

}

{

"user": {

"loginName": "Mike",

"fullname": "Mike Peat",

"password": "foo",

"rights": 1,

"lastLogin": "14/12/2022"

}

}

8 of 12

DEMO

9 of 12

How far have I got with GraphQL?

  • Well… not very far
  • If you've read my article you'll know what I've shown is about it
  • I took what I termed a "top-down" approach: just writing the code to deal with each situation
  • However what I think is required is a "bottom-up" approach

10 of 12

Bottom up

  • For this we need to be able to:
    • Create a GraphQL schema from the operations in our API
    • Correctly parse any valid (as defined by that schema) GraphQL request in terms of its resources, arguments, fields (and other spooky things like fragments and aliases!)
    • Use "resolvers" to fulfil the various aspects of those requests
  • All this is way above my pay grade
  • I need the services/help of a proper computer scientist! 😕

11 of 12

So why GraphQL?

  • The reason I'm interested in GraphQL is that SQL-like ability to do arbitrarily complex things in a single call
  • Almost the first thing customers, or users of our REST library, ask is to be able to get or post (or even modify) an Order and its OrderItems in a single call
  • I tell them: "That is not the way of REST, young grasshopper. You must first learn the way of the Order. Only then can you advance to the way of the OrderItems!"
  • But with GraphQL… you can! 😀

12 of 12

Thank you!

Are there any questions?