Artifact image

Persisted Queries

In a REST API, we create multiple endpoints, each returning a pre-defined set of data. In a GraphQL API, in contrast, we provide any query to a single endpoint, which returns exactly the requested data.

Persisted queries are normal GraphQL queries, however they are stored in the server and accessed under their own URL, thus emulating a REST endpoint. They provide the advantages from these two APIs, while avoiding their disadvantages:

AdvantagesDisadvantages
✅ Accessed via GET or POST❌ Accessed only via POST
✅ Can be cached on the server or CDN❌ Needs to provide an extra layer in client-side just for caching
✅ It's secure: only intended data is exposed❌ Data is exposed to anyone, including malicious actors
✅ No under/over fetching of data, all data is retrieved in a single request❌ It can be slow, since the application may need several requests to retrieve all the data
✅ It enables rapid iteration of the project❌ It's tedious to create all the endpoints
✅ It can be self-documented❌ Producing documentation is mandatory
✅ It provides clients to create and publish the query❌ Publishing endpoints is done via code
Persisted queries with description
Persisted queries with description