Skip to content

Commit 86d6778

Browse files
committed
Add docs and CLI option
1 parent a49a936 commit 86d6778

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

guides/javascript_client/sync.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ JavaScript support for GraphQL projects using [graphql-pro](https://graphql.pro)
1717
- [Apollo Link support](#use-with-apollo-link)
1818
- [Apollo Codegen Support](#use-with-apollo-codegen)
1919
- [Apollo Android support](#use-with-apollo-android)
20+
- [Apollo Persisted Queries Support](#use-with-apollo-persisted-queries)
2021
- [Plain JS support](#use-with-plain-javascript)
2122
- [Authorization](#authorization)
2223

@@ -263,6 +264,29 @@ end
263264

264265
You may also have to __update your app__ to send an identifier, so that the server can determine the "client name" used with the operation store. (Apollo Android sends a query hash, but the operation store expects IDs in the form `#{client_name}/#{query_hash}`.)
265266

267+
## Use with Apollo Persisted Queries
268+
269+
Apollo client has a [Persisted Queries Link](https://www.apollographql.com/docs/react/api/link/persisted-queries/). You can use that link with GraphQL-Pro's {% internal_link "OperationStore", "/operation_store/overview" %}. First, create a manifest with [`generate-persisted-query-manifest`](https://www.apollographql.com/docs/react/api/link/persisted-queries/#1-generate-operation-manifests), then, pass the path to that file to `sync`:
270+
271+
```sh
272+
$ graphql-ruby-client sync --apollo-persisted-query-manifest=path/to/manifest.json ...
273+
```
274+
275+
Then, configure Apollo Client to [use your persisted query manifest](https://www.apollographql.com/docs/react/api/link/persisted-queries/#persisted-queries-implementation).
276+
277+
Finally, update your controller to receive the operation ID and pass it as `context[:operation_id]`:
278+
279+
```ruby
280+
client_name = "..." # TODO: send the client name as a query param or header
281+
persisted_query_hash = params[:extensions][:persistedQuery][:sha256Hash]
282+
context = {
283+
# ...
284+
operation_id: "#{client_name}/#{persisted_query_hash}"
285+
}
286+
```
287+
288+
The `operation_id` will also need your client name. Using Apollo Client, you could send this as a [custom header](https://www.apollographql.com/docs/react/networking/basic-http-networking/#customizing-request-headers) or another way that works for your application (eg, session or user agent).
289+
266290
## Use with plain JavaScript
267291

268292
`OperationStoreClient.getOperationId` takes an operation name as input and returns the server-side alias for that operation:

javascript_client/src/__tests__/syncTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ describe("sync operations", () => {
4646
it("works with persisted query manifest", () => {
4747
var options = {
4848
client: "test-1",
49+
outfile: "./src/OperationStoreClient.js",
4950
apolloPersistedQueryManifest: "./src/sync/__tests__/generate-persisted-query-manifest.json",
5051
}
5152

javascript_client/src/cli.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ optional arguments:
2424
(Outfile generation is skipped by default.)
2525
--apollo-android-operation-output=<path> Path to a .json file from Apollo-Android's "generateOperationOutput" feature.
2626
(Outfile generation is skipped by default.)
27+
--apollo-persisted-query-manifest=<path> Path to a .json file from Apollo's "generate-persisted-query-manifest" tool.
28+
(Outfile generation is skipped by default.)
2729
--mode=<mode> Treat files like a certain kind of project:
2830
relay: treat files like relay-compiler output
2931
project: treat files like a cohesive project (fragments are shared, names must be unique)
@@ -63,6 +65,7 @@ optional arguments:
6365
relayPersistedOutput: argv["relay-persisted-output"],
6466
apolloCodegenJsonOutput: argv["apollo-codegen-json-output"],
6567
apolloAndroidOperationOutput: argv["apollo-android-operation-output"],
68+
apolloPersistedQueryManifest: argv["apollo-persisted-query-manifest"],
6669
url: argv.url,
6770
client: argv.client,
6871
outfile: argv.outfile,

javascript_client/src/sync/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function sync(options: SyncOptions) {
129129
var outfile: string | null
130130
if (options.outfile) {
131131
outfile = options.outfile
132-
} else if (options.relayPersistedOutput || options.apolloAndroidOperationOutput || options.apolloCodegenJsonOutput) {
132+
} else if (options.relayPersistedOutput || options.apolloAndroidOperationOutput || options.apolloCodegenJsonOutput || options.apolloPersistedQueryManifest) {
133133
// These artifacts have embedded IDs in its generated files,
134134
// no need to generate an outfile.
135135
outfile = null

0 commit comments

Comments
 (0)