HomeToolsAbout

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" } } }

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?

GraphQL is reflective of a data access query syntax.

  • Defined queries and mutations
    • queries and mutations look like you are querying the DB, but that is not true.

It's meant to be applied against the business logic, not data persistence layer.

  • Resolver return value is arbitrary, defined in the business logic layer.

Schema-first design

Implement the feature based on exactly which data the client application needs. (hence, GraphQL was sold as "working backwards from client").

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
  • Client querying
    • Client consumes data from GQL API to render the view

Benefits of GraphQL

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

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

  • When there are hierarchical and nested data structure, you can query for it better.

Supports multiple client device types (e.g. some resources may only be needed for mobile vs web).

GraphQL as an abstraction layer

GraphQL can be used as an abstraction between service consumers and backend services.

Instead of having each BFF per version of application, you can have one GraphQL layer that can point to any number of arbitrary backend services.

App -- BFF -- old service App -- BFF -- ^ App -- BFF -- ^ App -- BFF -- another service App -- BFF -- v new service
App old service App another service App -- GraphQL -- ^ App v App new service

This could eliminate hundreds of updates, deployments, and dependencies due to the reduced impact of changes to backend services.

AboutContact