Skip to content

Commit eb54291

Browse files
author
Vincent
authored
Merge pull request #44 from firstlookmedia/persisted-queries
Persisted queries
2 parents a5de30b + 521394b commit eb54291

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/PersistedQueries.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Persisted Static Queries
2+
3+
## Background
4+
5+
- Relay Classic generates queries at runtime. It can do things like allow string interpolation in the queries themselves. Unfortunately, this leads to a big performance cost at runtime and there is no compiled query that one could save and store. One of the biggest changes in Relay Modern is that all queries are compiled at build time. This [blog post](https://code.fb.com/data-infrastructure/relay-modern-simpler-faster-more-extensible/) goes into detail about this and other aspects of Relay Modern.
6+
7+
- With Relay Modern (but before persisted queries support), we have access to static queries that are generated before runtime, but we still send each query to the client. The query is then included in the `POST` request body from the client.
8+
9+
**Persisted static queries TLDR:** instead of sending the queries to the client and including them in the `POST` request, we now have the option to persist the queries on AWS. Each query has an associated deterministic (MD5) hash that we send to the client. The client then makes `GET` requests with the query hash and the query variables. This:
10+
11+
1. reduces the bundle size we ship to the client
12+
13+
2. reduces the size of the requests we make for each query
14+
15+
3. opens up everything else `GET` requests get, like better caching, CDN
16+
17+
## Prerequisites
18+
19+
1. Codebase must be entirely Relay Modern
20+
21+
2. An S3 bucket for storing queries
22+
23+
3. `react-scripts` version >= 2.0.0-rc2
24+
25+
4. First Look Media fork of `relay-compiler`: `https://github.com/firstlookmedia/relay/releases/download/v1.5.0-flm.1/relay-compiler-1.5.0-flm.1.tar.gz`
26+
27+
5. `PERSIST_QUERIES: "true"` and `QUERIES_S3_BUCKET` circle configuration
28+
29+
## Front-end details
30+
31+
1. Upgrading to `react-scripts` with persisted queries support requires no changes for apps that do not want to use persisted queries
32+
33+
2. Persisted queries are always turned off during local development
34+
35+
3. The client needs access to the `PERSIST_QUERIES` environment variable (`env-config`)
36+
37+
4. Most of the code changes live in `fetcher.js`. The APIs for `ClientFetcher` & `ServerFetcher` have not changed. Under the hood they make `GET` or `POST` requests depending on the value of `PERSIST_QUERIES`
38+
39+
5. The fork of `relay-compiler` takes a `--persist` flag to turn persisted queries on. There is [a PR open to merge this feature into Relay itself](https://github.com/facebook/relay/pull/2354), for now we will will need to maintain a fork that we can upgrade when we upgrade Relay. This requires an update to the fork of Relay, a build of the complier package, and a release of our fork

0 commit comments

Comments
 (0)