{"id":13484097,"url":"https://github.com/Shopify/graphql-batch","last_synced_at":"2025-03-27T15:31:05.358Z","repository":{"id":41252321,"uuid":"42800785","full_name":"Shopify/graphql-batch","owner":"Shopify","description":"A query batching executor for the graphql gem","archived":false,"fork":false,"pushed_at":"2024-10-14T11:39:41.000Z","size":223,"stargazers_count":1426,"open_issues_count":14,"forks_count":105,"subscribers_count":472,"default_branch":"main","last_synced_at":"2025-03-25T04:54:32.245Z","etag":null,"topics":["batch","graphql","promise"],"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/Shopify.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-20T04:57:22.000Z","updated_at":"2025-02-18T17:29:31.000Z","dependencies_parsed_at":"2023-10-10T16:14:45.325Z","dependency_job_id":"c6b23147-02ee-4d95-a201-e20edcc36ef3","html_url":"https://github.com/Shopify/graphql-batch","commit_stats":{"total_commits":154,"total_committers":40,"mean_commits":3.85,"dds":0.6493506493506493,"last_synced_commit":"8f5b102abada9703617807cc32f68e96a7031fa2"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fgraphql-batch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fgraphql-batch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fgraphql-batch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fgraphql-batch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shopify","download_url":"https://codeload.github.com/Shopify/graphql-batch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245401398,"owners_count":20609167,"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":["batch","graphql","promise"],"created_at":"2024-07-31T17:01:19.400Z","updated_at":"2025-03-27T15:31:04.775Z","avatar_url":"https://github.com/Shopify.png","language":"Ruby","readme":"# GraphQL::Batch\n\n[![Build Status](https://github.com/Shopify/graphql-batch/actions/workflows/ci.yml/badge.svg)](https://github.com/Shopify/graphql-batch/actions)\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\n#### Schema Configuration\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 _after_ specifying the mutation\nso that `GraphQL::Batch` can extend the mutation fields to clear the cache after\nthey are resolved.\n\n```ruby\nclass MySchema \u003c GraphQL::Schema\n  query MyQueryType\n  mutation MyMutationType\n\n  use GraphQL::Batch\nend\n```\n\n#### Field Usage\n\nThe loader class can be used from the resolver 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\nfield :product, Types::Product, null: true do\n  argument :id, ID, required: true\nend\n\ndef product(id:)\n  RecordLoader.for(Product).load(id)\nend\n```\n\nThe loader also supports batch loading an array of records instead of just a single record, via `load_many`. For example:\n\n```ruby\nfield :products, [Types::Product, null: true], null: false do\n  argument :ids, [ID], required: true\nend\n\ndef products(ids:)\n  RecordLoader.for(Product).load_many(ids)\nend\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\ndef product_title(id:)\n  RecordLoader.for(Product).load(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\ndef product_image(id:)\n  RecordLoader.for(Product).load(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\ndef all_collections\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(\u0026:sum)\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\ndef product(id:)\n  # Try the cache first ...\n  CacheLoader.for(Product).load(id).then(nil, lambda do |exc|\n    # But if there's a connection error, go to the underlying database\n    raise exc unless exc.is_a?(Redis::BaseConnectionError)\n    logger.warn err.message\n    RecordLoader.for(Product).load(id)\n  end)\nend\n```\n\n### Priming the Cache\n\nYou can prime the loader cache with a specific value, which can be useful in certain situations.\n\n```ruby\ndef liked_products\n  liked_products = Product.where(liked: true).load\n  liked_products.each do |product|\n    RecordLoader.for(Product).prime(product.id, product)\n  end\nend\n```\n\nPriming will add key/value to the loader cache only if it didn't exist before.\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\ndef 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\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\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","funding_links":[],"categories":["Libraries","Ruby","Implementations","GraphQL"],"sub_categories":["Ruby Libraries","Ruby"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FShopify%2Fgraphql-batch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FShopify%2Fgraphql-batch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FShopify%2Fgraphql-batch/lists"}