GraphQL Notes

REST - Different end points for different resources

GET /books/1 will return all the attributes implemented by server/developer

{
  "id": 1,
  "title": "REST",
  "author": "Chandan",
  "ISBN": "89898948934"
}

GraphQL - One endpoint, where query parameters decides, which resource/data the server needs to respond with.

curl \
  -X POST \
  -H "Content-Type: application/json" \
  --data '{ "query": "{ todos { edges { node { id completed text } } } }" }' \
  http://localhost:3000/

Tutorial: Designing a GraphQL API here

Facebook created GraphQL in 2012. They needed a better data-fetching approach that they could use across the entirety of the company’s products and services. They also wanted something that was understandable by developers, designers, as well as non-technical folk. After using it internally for some time, Facebook open-sourced GraphQL in 2015.

Queries that fetch data are synonymous to GET calls in REST, while mutations signal that we’d like to invoke a change the in system, similar to REST’s POST or DELETE methods.

GraphiQL

IDE For GraphQL : Mac OS: https://formulae.brew.sh/cask/graphiql

brew install --cask graphiql

  • Reading requests are made using queries
  • Writing requests are made using mutations
  • Normally always pulled from backend. Can be pushed from server using subscriptions and live queries
  • All data is circulated in JSON

Integrate GraphQL with [[ Rails ]]

GraphQL Ruby Documentation here

  1. Add gem 'graphql Documentation here
  2. bundle install
  3. rails g graphql:install
  • GraphiQL:

Either install GraphiQL Query editor: https://formulae.brew.sh/cask/graphiql OR add gem "graphiql-rails" in Gemfile and update routes.rb with:

if Rails.env.development?
    mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
end

graphql gem generators:

Refer the generators https://github.com/rmosolgo/graphql-ruby/tree/master/lib/generators/graphql

  • To generate graphql type for a model

rails g graphql:object author

  • To generate graphql mutation for a model

rails g graphql:mutation CreateUser

RubyConf Talk Notes: What is GraphQL - HTTP Query interface for Graphs Comparison of REST vs GraphQL Why do we need it? We can fetch tailored data for every consumer based on their exact requirements.

  • Minimum data transfer - Very useful for Mobile applications
  • Single Query can fetch Multiple entities and get exactly what you need
  • Self documentation
  • Type safe
  • Deprecations
  • Also supports subscription for async

In 2012 GraphQL was internally released by facebook. In 2015, GraphQL was open-sourced by Facebook. Currently GraphQL is used in Facebook webapp, messenger and many more.

In 2018, GraphQL foundation was created and hosted under Linux foundation. It’s open for everyone to join. More than 100 big companies use GraphQL including Netflix, Github, Pinterest, intuit, Coursera, Shopify, Salesforce etc.

Currently REST is still dominating the market, but more companies have started to invest on GraphQL. GraphQL also appeared on thoughtworks tech radar

GraphQL has been gaining popularity in State of JS reports and it’s gaining momentum every year.

Important Components of GraphQL

  • Schema
  • Types and Mutation

Challenges:

  • Caching due to nested queries
  • Errors - All the requests in GraphQL are POST requests and responds with 200 in case of success. Error messages are sometimes difficult to debug

Currently supported by many frameworks.

Deep dive in GraphQL DSL is converted to Ruby/Rails query.

This is a sapling 🌱 in my digital garden 🏡.

Notes mentioning this note

There are no notes linking to this note.


Here are all the notes in this garden, along with their links, visualized as a graph.