{"id":13452586,"url":"https://github.com/apollographql/persistgraphql","last_synced_at":"2025-03-23T19:34:33.633Z","repository":{"id":57097040,"uuid":"70954453","full_name":"apollographql/persistgraphql","owner":"apollographql","description":"A build tool for GraphQL projects.","archived":true,"fork":false,"pushed_at":"2019-02-24T21:51:31.000Z","size":146,"stargazers_count":425,"open_issues_count":39,"forks_count":57,"subscribers_count":72,"default_branch":"master","last_synced_at":"2025-02-19T12:56:07.421Z","etag":null,"topics":["apollo-client","graphql","middleware"],"latest_commit_sha":null,"homepage":"","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/apollographql.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}},"created_at":"2016-10-14T23:39:24.000Z","updated_at":"2024-08-08T15:15:42.000Z","dependencies_parsed_at":"2022-08-20T16:11:03.304Z","dependency_job_id":null,"html_url":"https://github.com/apollographql/persistgraphql","commit_stats":null,"previous_names":["poincare/apollo-query-whitelisting"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollographql%2Fpersistgraphql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollographql%2Fpersistgraphql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollographql%2Fpersistgraphql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollographql%2Fpersistgraphql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apollographql","download_url":"https://codeload.github.com/apollographql/persistgraphql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244991161,"owners_count":20543627,"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":["apollo-client","graphql","middleware"],"created_at":"2024-07-31T07:01:28.301Z","updated_at":"2025-03-23T19:34:33.186Z","avatar_url":"https://github.com/apollographql.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# PersistGraphQL\n\n`persistgraphql` is a simple build tool that enables query whitelisting and persisted queries for GraphQL projects that use statically analyze-able GraphQL queries.\n\nIt scans a code directory and extracts GraphQL query documents from `.graphql` files. It then assigns these queries ID values/hashes and produces a JSON file which maps from queries to hashes/IDs. This map can then be used by the client and server to perform query whitelisting, query lookups (i.e. client only sends the hash/id, the server just looks up the corresponding query), etc.\n\nThe npm package also provides a network interface for [Apollo Client](https://github.com/apollostack/apollo-client) that manages the query lookups in `persistgraphql/lib/browser`. To see how to extract the queries on the server, see the [code snippets](#server-side) below.\n\n# Installation\nFor only the CLI tool:\n\n```shell\nnpm install -g persistgraphql\n```\n\nAs a dependency (for Apollo Client network interface):\n\n```shell\nnpm install --save persistgraphql\n```\n\n# Build Tool Semantics\n\nThe build tool binary is called `persistgraphql`. Running it with no other arguments should give:\n\n```\nUsage: persistgraphql input_file [output file] [--add_typename]\n```\n\nIt can be called on a file containing GraphQL query definitions with extension `.graphql`:\n\n```shell\npersistgraphql queries.graphql\n```\n\nIt can also be called on a directory, which it will step through recursively:\n\n```shell\npersistgraphql src/\n```\n\nBy default, the output will be placed in `extracted_queries.json`. An output file can be specified as the second argument:\n\n```\npersistgraphql index.ts output.json\n```\n\n## Adding Typenames to Extracted Queries\n\nIt can also take the `--add_typename` flag which will apply a query transformation to the query documents, adding the `__typename` field at every level of the query. You must pass this option if your client code uses this query transformation.\n\n```\npersistgraphql src/ --add_typename\n```\n\n## Extracting Queries from TypeScript\n\nTo extract GraphQL queries from TypeScript files, use `--js --extension=ts`.\n\n```\npersistgraphql src/index.js --js --extension=ts\n```\n\n## Extracting Queries from JavaScript\n\nIt is also possible to extract GraphQL queries from JavaScript files using `--extension=js --js`.\n\n```\npersistgraphql src/index.js --js --extension=js\n```\n\n# Apollo Client Network Interface\n\nThis package provides an implementation of an Apollo Client network interface that provides persisted query support. It serves as a drop-in replacement for the standard network interface and uses the query map given by `persistgraphql` in order to send only query hashes/ids to the serverather than the query document.\n\nThis package also provides a way for you to alter any generic NetworkInterface to use persisted queries from a provided query map with the `addPersistedQueries(networkInterface: NetworkInterface, queryMap: OutputMap)` function.\nThis overrides the `query` member function of your network interface instance to replace your query with an id based on the query map provided.\n\nSee the implementation as well as some documentation for it within `src/network_interface/ApolloNetworkInterface.ts`.\n\n# Server-side\n\nIf you use the client network interface provided by this package, you can quickly roll your own middleware to get the GraphQL query instead of the query ID that the network interface sends. Here's an example with Express using the `lodash` `invert` method:\n\n```js\nimport queryMap from ‘../extracted_queries.json’;\nimport { invert } from 'lodash';\napp.use(\n  '/graphql',\n  (req, resp, next) =\u003e {\n    if (config.persistedQueries) {\n      const invertedMap = invert(queryMap);\n      req.body.query = invertedMap[req.body.id];\n    }\n    next();\n  },\n);\n```\nHere's an example with a Hapi server extension using the `lodash` `invert` method:\n\n```js\nimport queryMap from ‘../extracted_queries.json’;\nimport { invert } from 'lodash';\nserver.ext('onPreHandler', (req: Request, reply) =\u003e {\n  if (config.persistedQueries \u0026\u0026 req.url.path.indexOf('/graphql') \u003e= 0 \u0026\u0026 req.payload.id) {\n    const invertedMap = invert(queryMap);\n    req.payload.query = invertedMap[req.payload.id]\n  }\n  return reply.continue();\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollographql%2Fpersistgraphql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapollographql%2Fpersistgraphql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollographql%2Fpersistgraphql/lists"}