Not a protocol or an architecture.
GraphQL is an interface technology.
query language
for APIs
SELECT * FROM
in REST
joins
and getting a single response back of multiple tablesruntime
for executing those queries by using a type system you define for your data.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.
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
e.g. If you need data from 3 places, you don't make 3 calls or make custom endpoint to do optimal data fetch
Since it's an interface technology it belongs between the outermost layer of a service and the client.
GraphQL is reflective of a data access query syntax.
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.SDL
)SDL
= Schema Definition Langauge
.
Schema
works in GraphqlImplement the features without knowing exactly which data the client
application needs.
Involves three steps:
schema
Client
consumes data from GQL API to render the viewReduces 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.
Supports multiple client device types (e.g. some resources may only be needed for mobile vs web).
Also see GraphQL Federation