Basker Docs

Persisted queries

Send a SHA-256 hash in place of the query text to keep FrontStage API URLs short

A persisted query is a query that the site already knows. You send its SHA-256 hash in place of the query text. The URL stays short and stays the same for every visitor, so the response caches well.

The FrontStage API uses automatic persisted queries. You do not upload queries in advance. The first request that sends both the query and its hash registers the query for that site.

How the exchange works

  1. Compute the SHA-256 hash of the exact query text, as lowercase hexadecimal.
  2. Send a GET request with only the hash in the extensions parameter.
  3. If the site knows the hash, it runs the stored query and returns the result.
  4. If the site does not know the hash, it returns the error code PERSISTED_QUERY_NOT_FOUND.
  5. Send the request again with the hash and the full query. The site checks the hash, stores the query, and returns the result.

The extensions value is this JSON:

{ "persistedQuery": { "version": 1, "sha256Hash": "96ba8c63b3a81d939f5dec44fc17c75d6c7ec8d5a97b02083ac26505a3f7d38c" } }

Try it with curl

The hash of { site { name } } is 96ba8c63b3a81d939f5dec44fc17c75d6c7ec8d5a97b02083ac26505a3f7d38c.

QUERY='{ site { name } }'
HASH=$(printf '%s' "$QUERY" | sha256sum | cut -d' ' -f1)
EXT="{\"persistedQuery\":{\"version\":1,\"sha256Hash\":\"$HASH\"}}"

# 1. Hash only. A new query returns PERSISTED_QUERY_NOT_FOUND.
curl -s -G https://www.example.org/graphql --data-urlencode "extensions=$EXT"

# 2. Hash and query. Registers the query and returns data.
curl -s -G https://www.example.org/graphql \
  --data-urlencode "extensions=$EXT" --data-urlencode "query=$QUERY"

# 3. Hash only. Returns data.
curl -s -G https://www.example.org/graphql --data-urlencode "extensions=$EXT"

The first request returns:

{ "errors": [{ "message": "PersistedQueryNotFound", "extensions": { "code": "PERSISTED_QUERY_NOT_FOUND" } }] }

Use the client

@baskerhq/frontstage-client runs the exchange for you. It minifies the query, hashes the minified text, sends the hash, and retries once with the full query when the site answers PERSISTED_QUERY_NOT_FOUND.

import { createFrontstageClient } from "@baskerhq/frontstage-client";

const basker = createFrontstageClient({ endpoint: "https://www.example.org/graphql" });
const { site } = await basker.query<{ site: { name: string } }>("{ site { name } }");

To turn persisted queries off, pass persistedQueries: false. The client then sends the full query in every request.

Write your own client

Hash the same text that you send in query. If you minify the query, hash the minified text.

async function sha256Hex(text: string): Promise<string> {
  const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
  return Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, "0")).join("");
}

async function frontstage(endpoint: string, query: string, variables?: Record<string, unknown>) {
  const hash = await sha256Hex(query);
  const send = async (withQuery: boolean) => {
    const url = new URL(endpoint);
    url.searchParams.set("extensions", JSON.stringify({ persistedQuery: { version: 1, sha256Hash: hash } }));
    if (withQuery) {
      url.searchParams.set("query", query);
    }
    if (variables) {
      url.searchParams.set("variables", JSON.stringify(variables));
    }
    return (await fetch(url)).json();
  };

  const first = await send(false);
  const notFound = first.errors?.some(
    (error: { extensions?: { code?: string } }) => error.extensions?.code === "PERSISTED_QUERY_NOT_FOUND",
  );
  return notFound ? send(true) : first;
}

Variables are not part of the hash. One registered query serves every combination of variables.

Errors

CodeHTTP statusCauseWhat to do
PERSISTED_QUERY_NOT_FOUND200The site does not know the hash.Send the request again with query.
PERSISTED_QUERY_HASH_MISMATCH400The hash is not the SHA-256 of the query text.Hash the exact text that you send.
PERSISTED_QUERY_TOO_LARGE400The query is longer than 32,000 characters.Split the query.

Basker stores registered queries for each site separately, for 30 days from registration. Using a query does not extend that time, and Basker can forget a registered query at any time. Always handle PERSISTED_QUERY_NOT_FOUND, also for a query that worked before. Basker never caches a response with an error, so the retry reaches the site.

On this page