{"id":15048330,"url":"https://github.com/github/graphql-batch","last_synced_at":"2025-10-04T08:31:56.460Z","repository":{"id":65974861,"uuid":"114139096","full_name":"github/graphql-batch","owner":"github","description":"A query batching executor for the graphql gem","archived":true,"fork":true,"pushed_at":"2020-06-04T16:32:20.000Z","size":102,"stargazers_count":5,"open_issues_count":1,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-25T21:10:44.266Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"Shopify/graphql-batch","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/github.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-13T15:45:26.000Z","updated_at":"2024-07-31T03:17:38.000Z","dependencies_parsed_at":"2023-02-19T18:01:12.305Z","dependency_job_id":null,"html_url":"https://github.com/github/graphql-batch","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgraphql-batch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgraphql-batch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgraphql-batch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgraphql-batch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/github","download_url":"https://codeload.github.com/github/graphql-batch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219876581,"owners_count":16554769,"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-09-24T21:10:53.089Z","updated_at":"2025-10-04T08:31:51.162Z","avatar_url":"https://github.com/github.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GraphQL::Batch\n\n[![Build Status](https://travis-ci.org/Shopify/graphql-batch.svg?branch=master)](https://travis-ci.org/Shopify/graphql-batch)\n[![Gem Version](https://badge.fury.io/rb/graphql-batch.svg)](https://rubygems.org/gems/graphql-batch)\n\nProvides an executor for the [`graphql` gem](https://github.com/rmosolgo/graphql-ruby) which allows queries to be batched.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'graphql-batch'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install graphql-batch\n\n## Usage\n\n### Basic Usage\n\nRequire the library\n\n```ruby\nrequire 'graphql/batch'\n```\n\nDefine a custom loader, which is initialized with arguments that are used for grouping and a perform method for performing the batch load.\n\n```ruby\nclass RecordLoader \u003c GraphQL::Batch::Loader\n  def initialize(model)\n    @model = model\n  end\n\n  def perform(ids)\n    @model.where(id: ids).each { |record| fulfill(record.id, record) }\n    ids.each { |id| fulfill(id, nil) unless fulfilled?(id) }\n  end\nend\n```\n\nUse `GraphQL::Batch` as a plugin in your schema (for graphql \u003e= `1.5.0`).\n\n```ruby\nMySchema = GraphQL::Schema.define do\n  query MyQueryType\n\n  use GraphQL::Batch\nend\n```\n\nFor pre `1.5.0` versions:\n\n```ruby\nMySchema = GraphQL::Schema.define do\n  query MyQueryType\n\n  GraphQL::Batch.use(self)\nend\n```\n\nThe loader class can be used from the resolve proc for a graphql field by calling `.for` with the grouping arguments to get a loader instance, then call `.load` on that instance with the key to load.\n\n```ruby\nresolve -\u003e (obj, args, context) { RecordLoader.for(Product).load(args[\"id\"]) }\n```\n\nAlthough this library doesn't have a dependency on active record,\nthe [examples directory](examples) has record and association loaders\nfor active record which handles edge cases like type casting ids\nand overriding GraphQL::Batch::Loader#cache_key to load associations\non records with the same id.\n\n### Promises\n\nGraphQL::Batch::Loader#load returns a Promise using the [promise.rb gem](https://rubygems.org/gems/promise.rb) to provide a promise based API, so you can transform the query results using `.then`\n\n```ruby\nresolve -\u003e (obj, args, context) do\n  RecordLoader.for(Product).load(args[\"id\"]).then do |product|\n    product.title\n  end\nend\n```\n\nYou may also need to do another query that depends on the first one to get the result, in which case the query block can return another query.\n\n```ruby\nresolve -\u003e (obj, args, context) do\n  RecordLoader.for(Product).load(args[\"id\"]).then do |product|\n    RecordLoader.for(Image).load(product.image_id)\n  end\nend\n```\n\nIf the second query doesn't depend on the first one, then you can use Promise.all, which allows each query in the group to be batched with other queries.\n\n```ruby\nresolve -\u003e (obj, args, context) do\n  Promise.all([\n    CountLoader.for(Shop, :smart_collections).load(context.shop_id),\n    CountLoader.for(Shop, :custom_collections).load(context.shop_id),\n  ]).then do |results|\n    results.reduce(\u0026:+)\n  end\nend\n```\n\n`.then` can optionally take two lambda arguments, the first of which is equivalent to passing a block to `.then`, and the second one handles exceptions.  This can be used to provide a fallback\n\n```ruby\nresolve -\u003e (obj, args, context) do\n  CacheLoader.for(Product).load(args[\"id\"]).then(nil, lambda do |exc|\n    raise exc unless exc.is_a?(Redis::BaseConnectionError)\n    logger.warn err.message\n    RecordLoader.for(Product).load(args[\"id\"])\n  end)\nend\n```\n\n## Unit Testing\n\nYour loaders can be tested outside of a GraphQL query by doing the\nbatch loads in a block passed to GraphQL::Batch.batch.  That method\nwill set up thread-local state to store the loaders, batch load any\npromise returned from the block then clear the thread-local state\nto avoid leaking state between tests.\n\n```ruby\n  def test_single_query\n    product = products(:snowboard)\n    title = GraphQL::Batch.batch do\n      RecordLoader.for(Product).load(product.id).then(\u0026:title)\n    end\n    assert_equal product.title, title\n  end\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\n## Contributing\n\nSee our [contributing guidelines](CONTRIBUTING.md) for more information.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub%2Fgraphql-batch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgithub%2Fgraphql-batch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub%2Fgraphql-batch/lists"}