{"id":21178801,"url":"https://github.com/valu-digital/graphql-codegen-persisted-query-ids","last_synced_at":"2025-05-08T00:28:22.463Z","repository":{"id":46313085,"uuid":"172915271","full_name":"valu-digital/graphql-codegen-persisted-query-ids","owner":"valu-digital","description":null,"archived":false,"fork":false,"pushed_at":"2024-07-29T11:47:29.000Z","size":44,"stargazers_count":50,"open_issues_count":5,"forks_count":13,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-31T15:46:48.382Z","etag":null,"topics":["npm"],"latest_commit_sha":null,"homepage":"https://npm.im/graphql-codegen-persisted-query-ids","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/valu-digital.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-27T12:53:31.000Z","updated_at":"2024-11-30T05:20:38.000Z","dependencies_parsed_at":"2024-11-20T22:37:30.272Z","dependency_job_id":null,"html_url":"https://github.com/valu-digital/graphql-codegen-persisted-query-ids","commit_stats":null,"previous_names":["epeli/graphql-codegen-persisted-query-ids"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valu-digital%2Fgraphql-codegen-persisted-query-ids","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valu-digital%2Fgraphql-codegen-persisted-query-ids/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valu-digital%2Fgraphql-codegen-persisted-query-ids/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valu-digital%2Fgraphql-codegen-persisted-query-ids/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/valu-digital","download_url":"https://codeload.github.com/valu-digital/graphql-codegen-persisted-query-ids/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252976322,"owners_count":21834568,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["npm"],"created_at":"2024-11-20T17:26:29.784Z","updated_at":"2025-05-08T00:28:22.424Z","avatar_url":"https://github.com/valu-digital.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Generate Persisted Query IDs\n\nA plugin for graphql-code-generator\n\n## Install\n\nInstall graphql-code-generator and this plugin\n\n    npm i -D graphql-code-generator graphql-codegen-persisted-query-ids\n\n## Usage\n\nCreate codegen.yml\n\n```yaml\nschema: http://app.test/graphql\ndocuments: \"./src/**/*.js\"\ngenerates:\n    persisted-query-ids/client.json:\n        - graphql-codegen-persisted-query-ids:\n              output: client\n              algorithm: sha256\n\n    persisted-query-ids/server.json:\n        - graphql-codegen-persisted-query-ids:\n              output: server\n              algorithm: sha256\n```\n\nRun the generator\n\n    mkdir persisted-query-ids\n    ./node_modules/.bin/gql-gen --overwrite\n\nThis will generate two json files. The `server.json` is a query id mapping to\nthe actual queries which should be consumed by the server.\n\nExample\n\n```json\n{\n    \"093eb2253f63de7afc7c4637bf19273a09591c2139bc068de320ae78e39755d9\": \"query Thing { field }\"\n}\n```\n\nThe `client.json` file is an operation name mapping to the query id to be\nconsumed by the GraphQL clients.\n\n```json\n{\n    \"Thing\": \"093eb2253f63de7afc7c4637bf19273a09591c2139bc068de320ae78e39755d9\"\n}\n```\n\n### Integrating with WPGraphQL\n\nUse the [wp-graphql-lock][] plugin\n\n    cd wp-content/plugins\n    git clone https://github.com/valu-digital/wp-graphql-lock\n\n[wp-graphql-lock]: https://github.com/valu-digital/wp-graphql-lock\n\nIn your theme's `functions.php` add\n\n```php\nadd_filter( 'graphql_lock_load_query', function( string $query, string $query_id ) {\n    $queries = json_decode( file_get_contents( __DIR__ . '/../persisted-query-ids/server.json' ), true );\n    return $queries[ $query_id ] ?? null;\n}, 10, 2 );\n\n```\n\n### Integrating with Apollo Client\n\nAdd custom `generateHash` to [apollo-link-persisted-queries](https://github.com/apollographql/apollo-link-persisted-queries)\n\n```ts\nimport { createPersistedQueryLink } from \"@apollo/client/link/persisted-queries\";\n// import {createPersistedQueryLink } from \"apollo-link-persisted-queries\"; // For Apollo Client v2\nimport { usePregeneratedHashes } from \"graphql-codegen-persisted-query-ids/lib/apollo\";\n\nconst hashes = require(\"../persisted-query-ids/client.json\");\n\nconst persistedLink = createPersistedQueryLink({\n    useGETForHashedQueries: true, // Optional but allows better caching\n    generateHash: usePregeneratedHashes(hashes),\n});\n\n// And pass it to ApolloClient\n\nconst client = new ApolloClient({\n    link: persistedLink.concat(createHttpLink({ uri: \"/graphql\" })),\n    cache: new InMemoryCache(),\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalu-digital%2Fgraphql-codegen-persisted-query-ids","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvalu-digital%2Fgraphql-codegen-persisted-query-ids","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalu-digital%2Fgraphql-codegen-persisted-query-ids/lists"}