{"id":13879692,"url":"https://github.com/DmitryTsepelev/graphql-ruby-persisted_queries","last_synced_at":"2025-07-16T15:32:50.560Z","repository":{"id":35169079,"uuid":"213179845","full_name":"DmitryTsepelev/graphql-ruby-persisted_queries","owner":"DmitryTsepelev","description":"Persisted queries for graphql-ruby","archived":false,"fork":false,"pushed_at":"2024-09-06T14:05:42.000Z","size":153,"stargazers_count":172,"open_issues_count":6,"forks_count":20,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-16T00:02:01.236Z","etag":null,"topics":["apollo","graphql","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/DmitryTsepelev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","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-10-06T14:07:24.000Z","updated_at":"2024-11-08T15:14:31.000Z","dependencies_parsed_at":"2024-03-10T16:27:02.602Z","dependency_job_id":"7db6ade7-dcc8-42d4-a1bb-2c53b8448fd3","html_url":"https://github.com/DmitryTsepelev/graphql-ruby-persisted_queries","commit_stats":{"total_commits":74,"total_committers":10,"mean_commits":7.4,"dds":"0.20270270270270274","last_synced_commit":"5fa33a6b0e65eb6558df6cb55c7ba0ae4a40a969"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmitryTsepelev%2Fgraphql-ruby-persisted_queries","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmitryTsepelev%2Fgraphql-ruby-persisted_queries/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmitryTsepelev%2Fgraphql-ruby-persisted_queries/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmitryTsepelev%2Fgraphql-ruby-persisted_queries/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DmitryTsepelev","download_url":"https://codeload.github.com/DmitryTsepelev/graphql-ruby-persisted_queries/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225693834,"owners_count":17509227,"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","graphql","ruby"],"created_at":"2024-08-06T08:02:29.230Z","updated_at":"2025-07-16T15:32:50.543Z","avatar_url":"https://github.com/DmitryTsepelev.png","language":"Ruby","readme":"# GraphQL::PersistedQueries ![](https://ruby-gem-downloads-badge.herokuapp.com/graphql-persisted_queries?type=total)\n\n`GraphQL::PersistedQueries` is the implementation of [persisted queries](https://www.apollographql.com/docs/react/api/link/persisted-queries/) for [graphql-ruby](https://github.com/rmosolgo/graphql-ruby). With this plugin your backend will cache all the queries, while frontend will send the full query only when it's not found at the backend storage.\n\n- 🗑**Heavy query parameter will be omitted in most of cases** – network requests will become less heavy\n- 🤝**Clients share cached queries** – it's enough to miss cache only once for each unique query\n- 🎅**Works for clients without persisted query support**\n\nUsed in production by:\n\n- [Yammer by Microsoft](https://github.com/DmitryTsepelev/graphql-ruby-persisted_queries/issues/20#issuecomment-587945989)\n- Toptal\n- _Want to be here? Let me know_ 🙂\n\nYou can support my open–source work [here](https://boosty.to/dmitry_tsepelev).\n\n## Getting started\n\nFirst of all, install and configure [apollo's persisted queries](https://www.apollographql.com/docs/react/api/link/persisted-queries/) on the front–end side:\n\n```js\nimport { HttpLink, InMemoryCache, ApolloClient } from \"@apollo/client\";\nimport { createPersistedQueryLink } from \"@apollo/client/link/persisted-queries\";\nimport { sha256 } from 'crypto-hash';\n\nconst httpLink = new HttpLink({ uri: \"/graphql\" });\nconst persistedQueriesLink = createPersistedQueryLink({ sha256 });\nconst client = new ApolloClient({\n  cache: new InMemoryCache(),\n  link: persistedQueriesLink.concat(httpLink);\n});\n```\n\nAdd the gem to your Gemfile `gem 'graphql-persisted_queries'` and add the plugin to your schema class:\n\n```ruby\nclass GraphqlSchema \u003c GraphQL::Schema\n  use GraphQL::PersistedQueries\nend\n```\n\nPass `:extensions` argument as part of a `context` to all calls of `GraphqlSchema#execute`, usually it happens in `GraphqlController`, `GraphqlChannel` and tests:\n\n```ruby\nGraphqlSchema.execute(\n  params[:query],\n  variables: ensure_hash(params[:variables]),\n  context: {\n    extensions: ensure_hash(params[:extensions])\n  },\n  operation_name: params[:operationName]\n)\n```\n\nYou're all set!\n\n## Compiled queries (increases performance up to 2x!)\n\nWhen query arrives to the backend, GraphQL execution engine needs some time to _parse_ it and build the AST. In case of a huge query it might take [a lot](https://gist.github.com/DmitryTsepelev/36e290cf64b4ec0b18294d0a57fb26ff#file-1_result-md) of time. What if we cache the AST instead of a query text and skip parsing completely? The only thing you need to do is to turn `:compiled_queries` option on:\n\n```ruby\nclass GraphqlSchema \u003c GraphQL::Schema\n  use GraphQL::PersistedQueries, compiled_queries: true\nend\n```\n\nUsing this option might make your endpoint up to 2x faster according to the [benchmark](docs/compiled_queries_benchmark.md).\n\n**Heads up!** This feature only works on `graphql-ruby` 1.12.0 or later, but I guess it might be backported.\n\n## Advanced usage\n\nAll the queries are stored in memory by default, but you can easily switch to another storage (e.g., _redis_:\n\n```ruby\nclass GraphqlSchema \u003c GraphQL::Schema\n  use GraphQL::PersistedQueries, store: :redis, redis_client: { redis_url: ENV[\"MY_REDIS_URL\"] }\nend\n```\n\nWe currently support `memory`, `redis`, `redis_with_local_cache` and `memcached` out of the box. The detailed documentation can be found [here](docs/alternative_stores.md).\n\nWhen the error occurs, the gem tries to not interrupt the regular flow of the app (e.g., when something is wrong with the storage, it will just answer that persisted query is not found). You can add a [custom](docs/error_handling.md) error handler and try to fix the problem or just log it.\n\nSince our queries are slim now, we can switch back to HTTP GET, you can find a [guide](docs/http_cache.md) here.\n\n[batch-link](https://www.apollographql.com/docs/react/api/link/apollo-link-batch-http/) allows to group queries on the client side into a single HTTP request before sending to the server. In this case you need to use `GraphqlSchema.multiplex(queries)` instead of `#execute`. The gem supports it too, no action required!\n\n[persisted-queries-link](https://www.apollographql.com/docs/react/api/link/persisted-queries/) uses _SHA256_ for building hashes by default. Check out this [guide](docs/hash.md) if you want to override this behavior.\n\nIt is possible to skip some parts of the query lifecycle for cases when query is persisted - read more [here](docs/skip_query_preprocessing.md).\n\nAn experimental tracing feature can be enabled by setting `tracing: true` when configuring the plugin. Read more about this feature in the [Tracing guide](docs/tracing.md).\n\nYou can skip marshalling for queries stored in memory by passing the `marshal_inmemory_queries: false` option. This only affects the `memory` and `redis_with_local_cache` stores.\n\n\u003e 📖 Read more about the gem internals: [Persisted queries in GraphQL:\nSlim down Apollo requests to your Ruby application](https://evilmartians.com/chronicles/persisted-queries-in-graphql-slim-down-apollo-requests-to-your-ruby-application)\n\n## Credits\n\nInitially sponsored by [Evil Martians](http://evilmartians.com).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/DmitryTsepelev/graphql-ruby-persisted_queries.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDmitryTsepelev%2Fgraphql-ruby-persisted_queries","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDmitryTsepelev%2Fgraphql-ruby-persisted_queries","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDmitryTsepelev%2Fgraphql-ruby-persisted_queries/lists"}