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 dataGraphQL 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
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.Implement the feature based on exactly which data the client
application needs. (hence, GraphQL was sold as "working backwards from client").
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).
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.