RPC, REST, and now GraphQL? Yes! Facebook’s latest standard is shaking up the API world as it promises to provide consumers with more power and flexibility than they’ve had with previous standards. Sounds great, but how should one get started surfacing up data from their relational databases with GraphQL? In this session, attendees will get a brief overview of GraphQL APIs and learn about some of the key advantages they can offer over REST APIs. Next, a technical deep dive will show how GraphQL works and how it can be tied into new or existing relational data stores.
Adoption has been rapid since first public spec released in 2015
Specification unlike REST.
Spec is Royalty free - Open Web Foundation Agreement (OWFa) v1.0
Regular spec updates. Current is June 2018 and no longer a ‘Draft RFC’. Contains the Schema Definition Language
GraphQL is used in other languages, but this talk is about Node.js
I’ve grabbed a few logos of companies using GraphQL from the graphql.org site and other public sources. Oracle is included – I know of at least one product group inside Oracle seriously using GraphQL internally.
Gets all data....: Performance benefits from requiring a single request
Stateless allows scaling via cloning of GraphQL server instances
To set the scene of where we’ve come from, let’s look at a REST example
A GET call is made to our endpoint for resource 2
<Click>
Typically most new APIs give a JSON response
If you’ll forgive my nursery tune joke and accept that AGE is a string, in this example we get ‘Old MacDonald’ as the payload
I promise no more bad jokes.
Let's look at GraphQL the 'graph query language'. Here we have a declarative query that is submitted, typically over HTTP
This query with the root field ‘farmer' asks for the farmer with id of 2. Specifically it asks for the id, the name and the age of that farmer to be returned.
<click>
The GraphQL specification doesn’t say JSON has to be returned but in-line with REST usage, it typically is.
Let's look at GraphQL the 'graph query language'. Here we have a declarative query that is submitted, typically over HTTP
This query with the root field ‘farmer' asks for the farmer with id of 2. Specifically it asks for the id, the name and the age of that farmer to be returned.
<click>
The GraphQL specification doesn’t say JSON has to be returned but in-line with REST usage, it typically is.
This is what the demo will do.
We’ll make some GraphQL queries from a browser using the fantastic GraphiQL (pronounced Graphical) interface. These queries talk to our GraphQL server running in Node.js. The npm eco system has some excellent packages to work with GraphQL.
Node.js uses Oracle’s open source node-oracledb module to query Oracle Database.
In this particular example I’m using Oracle’s newish Document storage APIs “Simple Oracle Document Acces" or SODA.
By default SODA documents are JSON, making it perfect for working with GraphQL (or REST for that matter). I’ll talk a bit more about SODA in a few slides.
Note I could easily have used Oracle DB 12.1.0.2’s native JSON functionality, or even manually constructed JSON strings by using standard SQL.