HomeAbout

GraphQL Philosophy

What is GraphQL

Not a protocol or an architecture.

  • It's a query language.

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.

Why use Graphql

Avoids under/over fetching.

Creates contract between frontend and backend team, allowing a predetermined schema to be used by either team to build at their own pace.

How does it work

GraphQL is about asking for specific fields on objects.

  • Query has exactly the same shape as the result.
# Query { field_name { data_requested } } # return 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 (SDL)

SDL = Schema Definition Langauge.

  • Graphql has its own type system that's used to define the schema of an API.
  • The syntax for writing schemas is called SDL.

How Schema works in Graphql

Implement the features without knowing exactly which data the client application needs.

  • Making them generic enough via node/graph to be available to the client to define exactly what they need.

How GraphQL works high level

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
    • resolvers are the functions that fetch the data from the data sources and return it to the client on specific fields.
  • 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).

Also see GraphQL Federation

AboutContact