{"id":13631704,"url":"https://github.com/chatterbugapp/cacheql","last_synced_at":"2025-04-17T22:31:36.154Z","repository":{"id":31896331,"uuid":"129274876","full_name":"chatterbugapp/cacheql","owner":"chatterbugapp","description":"A slew of caching + instrumentation tools for graphql-ruby","archived":false,"fork":false,"pushed_at":"2023-03-08T21:43:15.000Z","size":28,"stargazers_count":95,"open_issues_count":7,"forks_count":5,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-01T22:49:29.273Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/chatterbugapp.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2018-04-12T15:44:13.000Z","updated_at":"2023-03-23T01:24:15.000Z","dependencies_parsed_at":"2024-08-01T22:40:05.159Z","dependency_job_id":"084b2ed2-1806-438c-8de3-df606c28532d","html_url":"https://github.com/chatterbugapp/cacheql","commit_stats":{"total_commits":21,"total_committers":5,"mean_commits":4.2,"dds":"0.19047619047619047","last_synced_commit":"a119a9fefb5397e2bce28729a87e86d8c0ea33c7"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chatterbugapp%2Fcacheql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chatterbugapp%2Fcacheql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chatterbugapp%2Fcacheql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chatterbugapp%2Fcacheql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chatterbugapp","download_url":"https://codeload.github.com/chatterbugapp/cacheql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223768647,"owners_count":17199356,"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":[],"created_at":"2024-08-01T22:02:35.070Z","updated_at":"2024-11-08T23:31:27.675Z","avatar_url":"https://github.com/chatterbugapp.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# CacheQL\n\nNeed to cache and instrument your GraphQL code in Ruby? Look no further!\n\nThis is a collection of utilities for [graphql-ruby](http://graphql-ruby.org)\nthat were collected from various places on GitHub + docs.\n\nThis code was extracted from [Chatterbug](https://chatterbug.com). Check out the talk from [Railsconf 2018](https://speakerdeck.com/qrush/the-graphql-way-a-new-path-for-json-apis) about this gem!\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'cacheql'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install cacheql\n\n## Usage\n\nThere's four major parts to this gem:\n\n### Cache helpers\n\nNeed to cache a single resolve function? Wrap it in `CacheQL`:\n\n``` ruby\nresolve CacheQL -\u003e (obj, args, ctx) {\n  # run expensive operation\n  # this resolve function's result will be cached on obj.cache_key\n}\n```\n\nWant to cache the entire response after GraphQL has generated it? Try this in\nyour controller:\n\n``` ruby\ndef execute\n  # other graphql stuff...\n\n  FIELDS = %w(users curriculum)\n  render json: CacheQL.fetch(FIELDS, query, variables) { |document|\n    YourSchema.execute(\n      document: document,\n      variables: variables,\n      context: { },\n      operation_name: params[:operationName]).to_h\n  }\nend\n```\n\nThis will cache the entire response when the query includes `FIELDS`.\n\n### Loaders\n\nThese are all based off of community-written examples using [graphql-batch](https://github.com/Shopify/graphql-batch).\nThese will reduce N+1 queries in your GraphQL code.\n\nBefore using loaders, you'll have to [use `GraphQL::Batch`](https://github.com/Shopify/graphql-batch#basic-usage) as a plugin in your schema:\n\n``` ruby\nYourSchema = GraphQL::Schema.define do\n  query YourQueryType\n\n  use GraphQL::Batch\nend\n```\n\nBatch up `belongs_to` calls:\n\n``` ruby\n# when obj has a belongs_to :language\n\nresolve -\u003e (obj, args, ctx) {\n  CacheQL::RecordLoader.for(Language).load(obj.language_id)\n}\n```\n\nBatch up `belongs_to polymorphic: true` calls:\n\n``` ruby\n# when obj has a belongs_to :respondable, polymorphic: true\n\nresolve -\u003e (obj, args, ctx) {\n  CacheQL::PolymorphicKeyLoader.for(Response, :respondable).load(obj.respondable)\n}\n```\n\nBatch up entire associations:\n\n``` ruby\n# when obj has_many :clozes\n\nresolve -\u003e (obj, args, ctx) {\n  CacheQL::AssociationLoader.for(obj.class, :clozes).load(obj)\n}\n```\n\n### Logging\n\nWant to get your GraphQL fields logging locally? In your controller, add:\n\n\n``` ruby\naround_action :log_field_instrumentation\n\nprivate\n\ndef log_field_instrumentation(\u0026block)\n  CacheQL::FieldInstrumentation.log(\u0026block)\nend\n```\n\nThis will then spit out timing logs for each field run during each request.\nFor example:\n\n```\n[CacheQL::Tracing] User.displayLanguage took 7.591ms\n[CacheQL::Tracing] User.createdAt took 0.117ms\n[CacheQL::Tracing] User.intercomHash took 0.095ms\n[CacheQL::Tracing] User.id took 0.09ms\n[CacheQL::Tracing] User.friendlyTimezone took 0.087ms\n[CacheQL::Tracing] User.utmContent took 0.075ms\n[GraphQL::Tracing] User.timezone took 0.048ms\n[CacheQL::Tracing] User.email took 0.046ms\n[CacheQL::Tracing] User.name took 0.042ms\n[CacheQL::Tracing] Query.currentUser took 0.041ms\n```\n\n### Instrumentation\n\nThis gem includes an instrumenter for [Scout](https://scoutapp.com) that will\nshow the timing breakdown of each field in your metrics. Assuming you have Scout\nsetup, add to your schema:\n\n\n``` ruby\nYourSchema = GraphQL::Schema.define do\n  instrument :field, CacheQL::FieldInstrumentation.new(ScoutApm::Tracer)\nend\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/chatterbugapp/cacheql.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchatterbugapp%2Fcacheql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchatterbugapp%2Fcacheql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchatterbugapp%2Fcacheql/lists"}