GraphQL Persisted Queries for optimization

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

GraphQL Persisted Queries

Persisted Queries is a technique where the client sends a query hash instead of full body. Server caches queries by hash. Benefits: reduced traffic (especially mobile), enables GET requests for CDN caching, protects against arbitrary queries in production.

How Automatic Persisted Queries (APQ) Works

Client          Server          CDN/Cache
  │                │                │
  │ POST {hash}    │                │
  │───────────────>│                │
  │ 404 Not Found  │                │
  │<───────────────│                │
  │                │                │
  │ POST {hash + query body}        │
  │───────────────>│                │
  │ {data} [save hash→query]        │
  │<───────────────│                │
  │                │                │
  │ GET ?hash=...  │                │
  │──────────────────────────────>  │
  │            {data} from cache    │
  │<──────────────────────────────  │

Apollo Client: Setting Up APQ

import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client'
import { createPersistedQueryLink } from '@apollo/client/link/persisted-queries'
import { generatePersistedQueryIdsFromManifest } from '@apollo/persisted-query-lists'
import { sha256 } from 'crypto-hash'

// Approach 1: Automatic Persisted Queries (fallback on cache miss)
const persistedQueryLink = createPersistedQueryLink({
  sha256,
  useGETForHashedQueries: true  // GET requests for CDN caching
})

const httpLink = createHttpLink({
  uri: 'https://api.example.com/graphql'
})

const client = new ApolloClient({
  link: persistedQueryLink.concat(httpLink),
  cache: new InMemoryCache()
})

Server-Side APQ Support

// Apollo Server built-in APQ support
import { ApolloServer } from '@apollo/server'
import { createClient } from 'redis'
import { BaseRedisCache } from 'apollo-server-cache-redis'

const redis = createClient({ url: process.env.REDIS_URL })
await redis.connect()

const server = new ApolloServer({
  typeDefs,
  resolvers,

  // APQ cache: in-memory by default, Redis in production
  cache: new BaseRedisCache({
    client: redis,
    ttl: 86400  // 24 hours
  }),

  // Enable APQ (enabled by default)
  persistedQueries: {
    ttl: 86400  // Cache lifetime
  }
})

Registered Persisted Queries (More Secure)

APQ allows executing any query after registration. Registered PQ only allows pre-known queries:

# Generate manifest from client operations (at build time)
npx generate-persisted-query-manifest \
  --documents "src/**/*.graphql" \
  --output persisted-query-manifest.json
// persisted-query-manifest.json
{
  "format": "apollo-persisted-query-manifest",
  "version": 1,
  "operations": [
    {
      "id": "dc67510fb4289672bea757e862d6b00e...",
      "name": "GetPosts",
      "type": "query",
      "body": "query GetPosts($limit: Int) { posts(first: $limit) { ... } }"
    },
    {
      "id": "e8c72e4d9b1a2f8e5c3d7a9b1f4e8c9d...",
      "name": "CreatePost",
      "type": "mutation",
      "body": "mutation CreatePost($input: CreatePostInput!) { createPost(input: $input) { ... } }"
    }
  ]
}
// Server validates against manifest
import { ApolloServer } from '@apollo/server'

const allowedQueries = new Set(
  manifest.operations.map(op => op.id)
)

const server = new ApolloServer({
  typeDefs,
  resolvers,
  persistedQueries: {
    ttl: 86400
  },
  // Only allow registered queries
  plugins: [{
    async requestDidResolveOperation({ request }) {
      const id = request.http?.headers?.get('x-graphql-query-id')
      if (id && !allowedQueries.has(id)) {
        throw new Error('Persisted query not found')
      }
    }
  }]
})

Benefits

  • Reduced bandwidth: 50-70% smaller requests on mobile
  • CDN caching: GET requests can be cached by standard CDN
  • Query whitelisting: prevents unintended queries in production
  • DDoS protection: only known queries execute on server

Timelines

Implementing APQ (automatic approach): 1–2 days. Registered PQ with manifest generation and CI/CD integration: 2–3 days.