HomeToolsAbout a20k

GraphQL Philosophy

What is GraphQL

GraphQL is an interface technology

  • GraphQL is a query language for APIs
    • think not doing SELECT * FROM in REST
      • instead, doing joins and getting a single response back of multiple tables
  • GraphQL is also a runtime for executing those queries by using a type system you define for your data

GraphQL is about asking for specific fields on objects

  • Query has exactly the same shape as the result
# Query { field_name { data_requested } } # retrun value { "data": { "field_name": { "data_requested": "some data you need" } } }

Benefits

You only make one request and get back exactly the data you need

  • reduction of payload and request calls

e.g. If you need data from 3 places, you don't make 3 calls or make custom endpoint to do optimal data fetch

Where does GraphQL sit in the architecture

Since it's an interface technology it belongs between the outermost layer of a service and the client

  • GraphQL should immediately call the business logic below it

Why does GraphQL seem like a data access pattern?

Source

The reason it may seem like a data access pattern is because it's reflective of one

  • In GraphQL you define queries and mutations - both of which are seemingly inspired by data access
    • but it's meant to be applied against the business logic not data persistence

Schema-first design

Implement the feature based on exactly which data the client application needs

Involves three steps:

  • Defining the schema
    • Identify which data our feature requires, and then we structure our schema to provide that data as intuitively as possible
  • Backend implementation
    • Build out GraphQL API using Apollo server and fetch the required data from the data sources
  • Front end implementation
    • Client consumes data from GQL API to render the view

Benefits

Reduces development time by allowing frontend and backend teams to work in parallel

When tons of resources have to be pulled in various configurations, it can reduce the payload size vs REST

© VincentVanKoh