Generate Persisted Query IDs
A plugin for graphql-code-generator
Install
Install graphql-code-generator and this plugin
npm i -D graphql-code-generator graphql-codegen-persisted-query-ids
Usage
Create codegen.yml
schema: http://app.test/graphqldocuments: "./src/**/*.js"generates: persisted-query-ids/client.json: - graphql-codegen-persisted-query-ids: output: client algorithm: sha256 persisted-query-ids/server.json: - graphql-codegen-persisted-query-ids: output: server algorithm: sha256
Run the generator
mkdir persisted-query-ids
./node_modules/.bin/gql-gen --overwrite
This will generate two json files. The server.json
is a query id mapping to
the actual queries which should be consumed by the server.
Example
The client.json
file is an operation name mapping to the query id to be
consumed by the GraphQL clients.
Integrating with WPGraphQL
Use the wp-graphql-lock plugin
cd wp-content/plugins
git clone https://github.com/valu-digital/wp-graphql-lock
In your theme's functions.php
add
add_filter( 'graphql_lock_load_query', function( string $query, string $query_id ) { $queries = json_decode( file_get_contents( __DIR__ . '/../persisted-query-ids/server.json' ), true ); return $queries[ $query_id ] ?? null;}, 10, 2 );
Integrating with Apollo Client
Add custom generateHash
to apollo-link-persisted-queries
;; ; ; // And pass it to ApolloClient ;