Introduction to GraphQL

https://dyncan.com

Agenda

  • What is GraphQL?
  • Who is using GraphQL?

  • GraphQL History

  • Core Concepts

  • Schema Definition

  • REST vs GraphQL

What is GraphQL?

  • A data query language for data fetching and manipulations
  • GraphQL is a PATTERN not a technology

  • Client queries are shaped just like the data it returns

  • Language agnostic

  • GraphQL has a formal specification

GraphQL is NOT

  • A Graph database query language
  • An alternative to SQL

  • Facebook Graph AP

  • Limited to specific database(s)

  • Limited to HTTP

  • Limited to JavaScript/NodeJS on the backend

  • A REST replacement

GraphQL History

Who is using GraphQL?

What a GraphQL looks like?

Core Concepts

  • Schema: defines the API structure itself-Core concept

  • Type: definition of a model(Person, Order, Product.)

  • Scalar Type: Int, Float, Boolean, String, ID and custom scalar type(e.g. Date)

  • Object Type: An object type contains a collection of fields, each of which has its own type.

  • Query: Definitions to query data(fetch)

  • Mutation: Definition to mutate data 

  • Subscription: Allows a server to send data to its clients when a specific event happens

Schema definition

-- To define a schema we use a Schema Definition Language(SDL)
-- Declare a type using the type keyword, followed by the name of the type (PascalCase is best practice)
-- The exclamation mark (!) depicts that the fields are required.

Operation Type

There are three types of operations in a GraphQL service:
      -  Query: a read-only fetch
      -  Mutation: a write followed by a fetch
      -  Subscription: a long-lived request that fetches data in response to source events(WS)

Query Operation Type

- Retrieve data
- Specify which fields are returned
- Can pass in variables, good for
pagination

Mutation Operation Type

REST vs GraphQL

USE CASE:

REST way to Fetch Data

REST way to Fetch Data

REST way to Fetch Data

GraphQL way to Fetch Data

REST vs GraphQL

REST way:

  • Fetch data from multiple endpoints
  • Overfetching: call too much data that won't be all used
  • Underfetching: call an endpoint that has not enough data-forcing to call another endpoint

GraphQL way:

  • Ask for specific fields in a single APl call
  • Can validate requests at build time
  • No under-fetching and over-fetching

REST vs GraphQL

BUT:

  • Schema definition can become very complex in a big application
  • Steep learning curve
  • Resources and community ecosystems are still growing
  • GraphQL Caching
  • Support rate limiting
  • Monitoring and Error Reporting

Refrence

  • https://graphql.org/learn/
  • https://spec.graphql.org/
  • https://docs.github.com/en/graphql
  • https://www.redhat.com/en/topics/api/what-is-graphql

Thanks