[
  {
    "path": ".changeset/README.md",
    "content": "# Changesets\n\nHello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works\nwith multi-package repos, or single-package repos to help you version and publish your code. You can\nfind the full documentation for it [in our repository](https://github.com/changesets/changesets)\n\nWe have a quick list of common questions to get you started engaging with this project in\n[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)\n"
  },
  {
    "path": ".changeset/config.json",
    "content": "{\n  \"$schema\": \"https://unpkg.com/@changesets/config@2.1.0/schema.json\",\n  \"changelog\": [\n    \"@changesets/changelog-github\",\n    { \"repo\": \"graphql/dataloader\" }\n  ],\n  \"commit\": false,\n  \"fixed\": [],\n  \"linked\": [],\n  \"access\": \"public\",\n  \"baseBranch\": \"main\",\n  \"updateInternalDependencies\": \"patch\",\n  \"ignore\": []\n}\n"
  },
  {
    "path": ".eslintignore",
    "content": "dist/\nflow-typed/\n"
  },
  {
    "path": ".eslintrc",
    "content": "{\n  \"globals\": {\n    \"$ReadOnlyArray\": true\n  },\n\n  \"parser\": \"babel-eslint\",\n\n  \"plugins\": [\"prettier\"],\n\n  \"env\": {\n    \"es6\": true,\n    \"node\": true,\n    \"jest\": true\n  },\n\n  \"rules\": {\n    \"block-scoped-var\": 0,\n    \"callback-return\": 2,\n    \"camelcase\": [2, {\"properties\": \"always\"}],\n    \"comma-dangle\": 0,\n    \"comma-spacing\": 0,\n    \"complexity\": 0,\n    \"computed-property-spacing\": [2, \"never\"],\n    \"consistent-return\": 0,\n    \"consistent-this\": 0,\n    \"default-case\": 0,\n    \"dot-location\": [2, \"property\"],\n    \"dot-notation\": 0,\n    \"eol-last\": 2,\n    \"eqeqeq\": 2,\n    \"func-names\": 0,\n    \"func-style\": 0,\n    \"generator-star-spacing\": [0, {\"before\": true, \"after\": false}],\n    \"guard-for-in\": 2,\n    \"handle-callback-err\": [2, \"error\"],\n    \"id-length\": 0,\n    \"id-match\": [2, \"^(?:_?[a-zA-Z0-9]*)|[_A-Z0-9]+$\"],\n    \"init-declarations\": 0,\n    \"key-spacing\": [2, {\"beforeColon\": false, \"afterColon\": true}],\n    \"keyword-spacing\": 2,\n    \"linebreak-style\": 2,\n    \"lines-around-comment\": 0,\n    \"max-depth\": 0,\n    \"max-nested-callbacks\": 0,\n    \"max-params\": 0,\n    \"max-statements\": 0,\n    \"new-cap\": 0,\n    \"new-parens\": 2,\n    \"newline-after-var\": 0,\n    \"no-alert\": 2,\n    \"no-array-constructor\": 2,\n    \"no-bitwise\": 0,\n    \"no-caller\": 2,\n    \"no-catch-shadow\": 0,\n    \"no-class-assign\": 2,\n    \"no-cond-assign\": 2,\n    \"no-console\": 1,\n    \"no-const-assign\": 2,\n    \"no-constant-condition\": 2,\n    \"no-continue\": 0,\n    \"no-control-regex\": 0,\n    \"no-debugger\": 1,\n    \"no-delete-var\": 2,\n    \"no-div-regex\": 2,\n    \"no-dupe-args\": 2,\n    \"no-dupe-keys\": 2,\n    \"no-duplicate-case\": 2,\n    \"no-else-return\": 2,\n    \"no-empty\": 2,\n    \"no-empty-character-class\": 2,\n    \"no-eq-null\": 0,\n    \"no-eval\": 2,\n    \"no-ex-assign\": 2,\n    \"no-extend-native\": 2,\n    \"no-extra-bind\": 2,\n    \"no-extra-boolean-cast\": 2,\n    \"no-extra-parens\": 0,\n    \"no-extra-semi\": 2,\n    \"no-fallthrough\": 2,\n    \"no-floating-decimal\": 2,\n    \"no-func-assign\": 2,\n    \"no-implicit-coercion\": 2,\n    \"no-implied-eval\": 2,\n    \"no-inline-comments\": 0,\n    \"no-inner-declarations\": [2, \"functions\"],\n    \"no-invalid-regexp\": 2,\n    \"no-invalid-this\": 0,\n    \"no-irregular-whitespace\": 2,\n    \"no-iterator\": 2,\n    \"no-label-var\": 2,\n    \"no-labels\": 0,\n    \"no-lone-blocks\": 2,\n    \"no-lonely-if\": 2,\n    \"no-loop-func\": 0,\n    \"no-mixed-requires\": [2, true],\n    \"no-mixed-spaces-and-tabs\": 2,\n    \"no-multi-spaces\": 2,\n    \"no-multi-str\": 2,\n    \"no-multiple-empty-lines\": 0,\n    \"no-native-reassign\": 0,\n    \"no-negated-in-lhs\": 2,\n    \"no-nested-ternary\": 0,\n    \"no-new\": 2,\n    \"no-new-func\": 0,\n    \"no-new-object\": 2,\n    \"no-new-require\": 2,\n    \"no-new-wrappers\": 2,\n    \"no-obj-calls\": 2,\n    \"no-octal\": 2,\n    \"no-octal-escape\": 2,\n    \"no-param-reassign\": 2,\n    \"no-path-concat\": 2,\n    \"no-plusplus\": 0,\n    \"no-process-env\": 0,\n    \"no-process-exit\": 0,\n    \"no-proto\": 2,\n    \"no-redeclare\": 2,\n    \"no-regex-spaces\": 2,\n    \"no-restricted-modules\": 0,\n    \"no-return-assign\": 2,\n    \"no-script-url\": 2,\n    \"no-self-compare\": 0,\n    \"no-sequences\": 2,\n    \"no-shadow\": 2,\n    \"no-shadow-restricted-names\": 2,\n    \"no-spaced-func\": 2,\n    \"no-sparse-arrays\": 2,\n    \"no-sync\": 2,\n    \"no-ternary\": 0,\n    \"no-this-before-super\": 2,\n    \"no-throw-literal\": 2,\n    \"no-trailing-spaces\": 2,\n    \"no-undef\": 2,\n    \"no-undef-init\": 2,\n    \"no-undefined\": 0,\n    \"no-underscore-dangle\": 0,\n    \"no-unexpected-multiline\": 2,\n    \"no-unneeded-ternary\": 2,\n    \"no-unreachable\": 2,\n    \"no-unused-expressions\": 2,\n    \"no-unused-vars\": [2, {\"vars\": \"all\", \"args\": \"after-used\"}],\n    \"no-use-before-define\": 0,\n    \"no-useless-call\": 2,\n    \"no-var\": 0,\n    \"no-void\": 2,\n    \"no-warning-comments\": 0,\n    \"no-with\": 2,\n    \"object-curly-spacing\": [0, \"always\"],\n    \"object-shorthand\": [2, \"always\"],\n    \"one-var\": [2, \"never\"],\n    \"operator-assignment\": [2, \"always\"],\n    \"prefer-const\": 0,\n    \"prefer-reflect\": 0,\n    \"prefer-spread\": 0,\n    \"quote-props\": [2, \"as-needed\"],\n    \"radix\": 2,\n    \"require-yield\": 2,\n    \"semi-spacing\": [2, {\"before\": false, \"after\": true}],\n    \"sort-vars\": 0,\n    \"space-before-blocks\": [2, \"always\"],\n    \"space-before-function-paren\": [2, {\"anonymous\": \"always\", \"named\": \"never\"}],\n    \"space-in-parens\": 0,\n    \"space-infix-ops\": [2, {\"int32Hint\": false}],\n    \"space-unary-ops\": [2, {\"words\": true, \"nonwords\": false}],\n    \"spaced-comment\": [2, \"always\"],\n    \"strict\": 0,\n    \"use-isnan\": 2,\n    \"valid-jsdoc\": 0,\n    \"valid-typeof\": 2,\n    \"vars-on-top\": 0,\n    \"wrap-iife\": 2,\n    \"wrap-regex\": 0,\n    \"yoda\": [2, \"never\", {\"exceptRange\": true}],\n\n    \"prettier/prettier\": 2\n  }\n}\n"
  },
  {
    "path": ".flowconfig",
    "content": "[ignore]\n.*/lib/.*\n.*/dist/.*\n.*/coverage/.*\n.*/resources/.*\n.*/node_modules/y18n/test/.*\n\n[include]\n\n[libs]\n\n[options]\nsuppress_comment=\\\\(.\\\\|\\n\\\\)*\\\\$FlowExpectError\ninclude_warnings=true\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/bug-report.md",
    "content": "---\nname: Bug report\nabout: Create a report to help us improve\ntitle: \"[BUG]\"\nlabels: bug\n---\n\n<!-- Before filing a bug, please consider submitting a pull request with a failing test -->\n\n## Expected Behavior\n<!-- Describe what should happen -->\n\n## Current Behavior\n<!-- Describe what happens instead of the expected behavior -->\n\n## Possible Solution\n<!-- Not obligatory, but ideally suggest a fix or hypothesis for the bug -->\n\n## Steps to Reproduce\n<!-- Provide a link to a live example, or an unambiguous set of steps to reproduce this bug -->\n<!-- Please consider submitting this as a pull request which adds a failing test case -->\n\n## Context\n<!-- Any additional information about your environment or what are you trying to accomplish that might be relevant? -->\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/feature-request.md",
    "content": "---\nname: Feature request\nabout: Suggest an idea for this project\ntitle: \"[REQUEST]\"\nlabels: enhancement\n---\n\n## What problem are you trying to solve?\n<!-- A concise description of what the problem is that this project should help solve -->\n\n## Describe the solution you'd like\n<!-- What should the API and behavior look like to address that problem? -->\n\n## Describe alternatives you've considered\n<!-- Are there any alternative solutions or features you've considered that are inadequate for solving this problem? -->\n\n## Additional context\n<!-- Any other context, examples, or adoptions paths to share? -->\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/question.md",
    "content": "---\nname: Question\nabout: Not a bug or feature request?\ntitle: \"[QUESTION]\"\nlabels: help wanted\n---\n\n<!-- \n\nPlease consider StackOverflow or other community discussion venues for general questions\nsince this issue tracker is intended for bugs and feature requests to DataLoader. Questions \nasked here are unlikely to get an answer in a timely fashion and might be closed without answer.\n\nhttps://graphql.org/community/\nhttps://stackoverflow.com/questions/tagged/graphql+dataloader\n\n-->\n"
  },
  {
    "path": ".github/workflows/validation.yml",
    "content": "name: Flow check, Lint and Tests \n\non: push\n\njobs:\n  validation:\n    name: Testing on Node ${{ matrix.node-version }}\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        node-version: [18, 20, 22]\n    steps:\n      - name: Checkout Repository\n        uses: actions/checkout@v4\n\n      - name: Use Node ${{ matrix.node-version }}\n        uses: actions/setup-node@v4\n        with:\n          node-version: ${{ matrix.node-version }}\n          cache: 'yarn'\n\n      - name: Install Dependencies using Yarn\n        run: yarn --ignore-engines\n\n      - name: Tests\n        run: yarn test:ci \n\n      - name: Upload coverage to Codecov\n        uses: codecov/codecov-action@v4\n        env:\n          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}\n        with:\n          fail_ci_if_error: true\n"
  },
  {
    "path": ".gitignore",
    "content": "*.swp\n*~\n*.iml\n.*.haste_cache.*\n.DS_Store\n.idea\nnpm-debug.log\n\nnode_modules\ncoverage\ndist\n\n# Generated with release scripts\nindex.d.ts\nindex.js\nindex.js.flow\n"
  },
  {
    "path": "CHANGELOG.md",
    "content": "# dataloader\n\n## 2.2.3\n\n### Patch Changes\n\n- [#342](https://github.com/graphql/dataloader/pull/342) [`38fedd4`](https://github.com/graphql/dataloader/commit/38fedd4106e9e3e7eb77bd68e42abc088110bd43) Thanks [@abendi](https://github.com/abendi)! - Ensure `cacheKeyFn` is not called when caching is disabled, since the key is not utilized in that case.\n\n## 2.2.2\n\n### Patch Changes\n\n- [#334](https://github.com/graphql/dataloader/pull/334) [`e286f66`](https://github.com/graphql/dataloader/commit/e286f662657675fa790f33abcd6aa87b5aac2be3) Thanks [@henrinormak](https://github.com/henrinormak)! - Added missing type definition for Dataloader.name\n\n## 2.2.1\n\n### Patch Changes\n\n- [#331](https://github.com/graphql/dataloader/pull/331) [`6d2efb7`](https://github.com/graphql/dataloader/commit/6d2efb7dd0363062de255e723c29a781d0ea9937) Thanks [@saihaj](https://github.com/saihaj)! - `name` is an optional property\n\n## 2.2.0\n\n### Minor Changes\n\n- [#326](https://github.com/graphql/dataloader/pull/326) [`6c758d0`](https://github.com/graphql/dataloader/commit/6c758d03bef628a69b238f053da3b263cd5e3321) Thanks [@SimenB](https://github.com/SimenB)! - Add `name` property to `DataLoader`. Useful in APM tools.\n\n### Patch Changes\n\n- [#318](https://github.com/graphql/dataloader/pull/318) [`588a8b6`](https://github.com/graphql/dataloader/commit/588a8b6c6391aad042b369f10dc440c7e0458312) Thanks [@boopathi](https://github.com/boopathi)! - Fix the propagation of sync throws in the batch function to the loader function instead of crashing the process wtih an uncaught exception.\n\n* [#252](https://github.com/graphql/dataloader/pull/252) [`fae38f1`](https://github.com/graphql/dataloader/commit/fae38f14702e925d1e59051d7e5cb3a9a78bfde8) Thanks [@LinusU](https://github.com/LinusU)! - Fix types for priming cache with promise\n\n- [#321](https://github.com/graphql/dataloader/pull/321) [`3cd3a43`](https://github.com/graphql/dataloader/commit/3cd3a430bdb4f9ef2f7f265a29e93e0255277885) Thanks [@thekevinbrown](https://github.com/thekevinbrown)! - Resolves an issue where the maxBatchSize parameter wouldn't be fully used on each batch sent to the backend loader.\n\n## 2.1.0\n\n### Minor Changes\n\n- 28cf959: - Do not return void results from arrow functions https://github.com/graphql/dataloader/commit/3b0bae94e91453d9a432c02628745252abc5e011\n  - Fix typo in `loader.load()` error message https://github.com/graphql/dataloader/commit/249b2b966a8807c50e07746ff04acb8c48fa4357\n  - Fix typo in SQL example https://github.com/graphql/dataloader/commit/cae1a3d9bfa48e181a49fd443f43813b335dc120\n  - Fix typo in TypeScript declaration https://github.com/graphql/dataloader/commit/ef6d32f97cde16aba84d96dc806c4439eaf8efae\n  - Most of the browsers don't have `setImmediate`. `setImmediate || setTimeout` doesn't work and it throws `setImmediate` is not defined in this case, so we should check setImmediate with typeof. And some environments like Cloudflare Workers don't allow you to set setTimeout directly to another variable. https://github.com/graphql/dataloader/commit/3e62fbe7d42b7ab1ec54818a1491cb0107dd828a\n\n### Patch Changes\n\n- 3135e9a: Fix typo in jsdoc comment; flip \"objects are keys\" to \"keys are objects\"\n"
  },
  {
    "path": "CONTRIBUTING.md",
    "content": "# Contributing to DataLoader\n\nWe want to make contributing to this project as easy and transparent as\npossible.\n\n## Code of Conduct\n\nThis project's code of conduct is described in the GraphQL Foundation's [`CODE_OF_CONDUCT.md`](https://github.com/graphql/foundation/blob/master/CODE-OF-CONDUCT.md)\n\n## Pull Requests\n\nWe actively welcome your pull requests for documentation and code.\n\n1. Fork the repo and create your branch from `master`.\n2. If you've added code that should be tested, add tests with 100% coverage.\n3. If you've changed APIs, update the documentation.\n4. Ensure the test suite passes.\n5. Make sure your code lints.\n6. If you haven't already, complete the Contributor License Agreement (\"CLA\").\n7. Run `yarn changeset` and describe the change you're proposing. Commit the file it creates in `.changeset` to the repo. [You can read more about changeset here.](https://github.com/changesets/changesets)\n8. Open a Pull Request so we can review and incorporate your change.\n\n## Releases\n\nTo release a new version:\n1. Run `yarn changeset version` to bump the version of the package.\n2. Run `yarn release` this will create a new release on GitHub and publish the package to NPM. \n\n## Issues\n\nWe use GitHub issues to track public bugs. Please ensure your description is\nclear and has sufficient instructions to be able to reproduce the issue.\n\n## Coding Style\n\n- 2 spaces for indentation rather than tabs\n- 80 character line length\n- See .eslintrc for the gory details.\n\n## License\n\nBy contributing to DataLoader, you agree that your contributions will be\nlicensed under its MIT license.\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) GraphQL Contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# DataLoader\n\nDataLoader is a generic utility to be used as part of your application's data\nfetching layer to provide a simplified and consistent API over various remote\ndata sources such as databases or web services via batching and caching.\n\n[![Build Status](https://github.com/graphql/dataloader/actions/workflows/validation.yml/badge.svg)](https://github.com/graphql/dataloader/actions/workflows/validation.yml)\n[![Coverage Status](https://coveralls.io/repos/graphql/dataloader/badge.svg?branch=master&service=github)](https://coveralls.io/github/graphql/dataloader?branch=main)\n\nA port of the \"Loader\" API originally developed by [@schrockn][] at Facebook in\n2010 as a simplifying force to coalesce the sundry key-value store back-end\nAPIs which existed at the time. At Facebook, \"Loader\" became one of the\nimplementation details of the \"Ent\" framework, a privacy-aware data entity\nloading and caching layer within web server product code. This ultimately became\nthe underpinning for Facebook's GraphQL server implementation and type\ndefinitions.\n\nDataLoader is a simplified version of this original idea implemented in\nJavaScript for Node.js services. DataLoader is often used when implementing a\n[graphql-js][] service, though it is also broadly useful in other situations.\n\nThis mechanism of batching and caching data requests is certainly not unique to\nNode.js or JavaScript, it is also the primary motivation for\n[Haxl](https://github.com/facebook/Haxl), Facebook's data loading library\nfor Haskell. More about how Haxl works can be read in this [blog post](https://code.facebook.com/posts/302060973291128/open-sourcing-haxl-a-library-for-haskell/).\n\nDataLoader is provided so that it may be useful not just to build GraphQL\nservices for Node.js but also as a publicly available reference implementation\nof this concept in the hopes that it can be ported to other languages. If you\nport DataLoader to another language, please open an issue to include a link from\nthis repository.\n\n## Getting Started\n\nFirst, install DataLoader using npm.\n\n```sh\nnpm install --save dataloader\n```\n\nTo get started, create a `DataLoader`. Each `DataLoader` instance represents a\nunique cache. Typically instances are created per request when used within a\nweb-server like [express][] if different users can see different things.\n\n> Note: DataLoader assumes a JavaScript environment with global ES6 `Promise`\n> and `Map` classes, available in all supported versions of Node.js.\n\n## Batching\n\nBatching is not an advanced feature, it's DataLoader's primary feature.\nCreate loaders by providing a batch loading function.\n\n```js\nconst DataLoader = require('dataloader');\n\nconst userLoader = new DataLoader(keys => myBatchGetUsers(keys));\n```\n\nA batch loading function accepts an Array of keys, and returns a Promise which\nresolves to an Array of values[<sup>\\*</sup>](#batch-function).\n\nThen load individual values from the loader. DataLoader will coalesce all\nindividual loads which occur within a single frame of execution (a single tick\nof the event loop) and then call your batch function with all requested keys.\n\n```js\nconst user = await userLoader.load(1);\nconst invitedBy = await userLoader.load(user.invitedByID);\nconsole.log(`User 1 was invited by ${invitedBy}`);\n\n// Elsewhere in your application\nconst user = await userLoader.load(2);\nconst lastInvited = await userLoader.load(user.lastInvitedID);\nconsole.log(`User 2 last invited ${lastInvited}`);\n```\n\nA naive application may have issued four round-trips to a backend for the\nrequired information, but with DataLoader this application will make at most\ntwo.\n\nDataLoader allows you to decouple unrelated parts of your application without\nsacrificing the performance of batch data-loading. While the loader presents an\nAPI that loads individual values, all concurrent requests will be coalesced and\npresented to your batch loading function. This allows your application to safely\ndistribute data fetching requirements throughout your application and maintain\nminimal outgoing data requests.\n\n#### Batch Function\n\nA batch loading function accepts an Array of keys, and returns a Promise which\nresolves to an Array of values or Error instances. The loader itself is provided\nas the `this` context.\n\n```js\nasync function batchFunction(keys) {\n  const results = await db.fetchAllKeys(keys);\n  return keys.map(key => results[key] || new Error(`No result for ${key}`));\n}\n\nconst loader = new DataLoader(batchFunction);\n```\n\nThere are a few constraints this function must uphold:\n\n- The Array of values must be the same length as the Array of keys.\n- Each index in the Array of values must correspond to the same index in the Array of keys.\n\nFor example, if your batch function was provided the Array of keys: `[ 2, 9, 6, 1 ]`,\nand loading from a back-end service returned the values:\n\n```js\n{ id: 9, name: 'Chicago' }\n{ id: 1, name: 'New York' }\n{ id: 2, name: 'San Francisco' }\n```\n\nOur back-end service returned results in a different order than we requested, likely\nbecause it was more efficient for it to do so. Also, it omitted a result for key `6`,\nwhich we can interpret as no value existing for that key.\n\nTo uphold the constraints of the batch function, it must return an Array of values\nthe same length as the Array of keys, and re-order them to ensure each index aligns\nwith the original keys `[ 2, 9, 6, 1 ]`:\n\n```js\n[\n  { id: 2, name: 'San Francisco' },\n  { id: 9, name: 'Chicago' },\n  null, // or perhaps `new Error()`\n  { id: 1, name: 'New York' },\n];\n```\n\n#### Batch Scheduling\n\nBy default DataLoader will coalesce all individual loads which occur within a\nsingle frame of execution before calling your batch function with all requested\nkeys. This ensures no additional latency while capturing many related requests\ninto a single batch. In fact, this is the same behavior used in Facebook's\noriginal PHP implementation in 2010. See `enqueuePostPromiseJob` in the\n[source code][] for more details about how this works.\n\nHowever sometimes this behavior is not desirable or optimal. Perhaps you expect\nrequests to be spread out over a few subsequent ticks because of an existing use\nof `setTimeout`, or you just want manual control over dispatching regardless of\nthe run loop. DataLoader allows providing a custom batch scheduler to provide\nthese or any other behaviors.\n\nA custom scheduler is provided as `batchScheduleFn` in options. It must be a\nfunction which is passed a callback and is expected to call that callback in the\nimmediate future to execute the batch request.\n\nAs an example, here is a batch scheduler which collects all requests over a\n100ms window of time (and as a consequence, adds 100ms of latency):\n\n```js\nconst myLoader = new DataLoader(myBatchFn, {\n  batchScheduleFn: callback => setTimeout(callback, 100),\n});\n```\n\nAs another example, here is a manually dispatched batch scheduler:\n\n```js\nfunction createScheduler() {\n  let callbacks = [];\n  return {\n    schedule(callback) {\n      callbacks.push(callback);\n    },\n    dispatch() {\n      callbacks.forEach(callback => callback());\n      callbacks = [];\n    },\n  };\n}\n\nconst { schedule, dispatch } = createScheduler();\nconst myLoader = new DataLoader(myBatchFn, { batchScheduleFn: schedule });\n\nmyLoader.load(1);\nmyLoader.load(2);\ndispatch();\n```\n\n## Caching\n\nDataLoader provides a memoization cache for all loads which occur in a single\nrequest to your application. After `.load()` is called once with a given key,\nthe resulting value is cached to eliminate redundant loads.\n\n#### Caching Per-Request\n\nDataLoader caching _does not_ replace Redis, Memcache, or any other shared\napplication-level cache. DataLoader is first and foremost a data loading mechanism,\nand its cache only serves the purpose of not repeatedly loading the same data in\nthe context of a single request to your Application. To do this, it maintains a\nsimple in-memory memoization cache (more accurately: `.load()` is a memoized function).\n\nAvoid multiple requests from different users using the DataLoader instance, which\ncould result in cached data incorrectly appearing in each request. Typically,\nDataLoader instances are created when a Request begins, and are not used once the\nRequest ends.\n\nFor example, when using with [express][]:\n\n```js\nfunction createLoaders(authToken) {\n  return {\n    users: new DataLoader(ids => genUsers(authToken, ids)),\n  };\n}\n\nconst app = express();\n\napp.get('/', function (req, res) {\n  const authToken = authenticateUser(req);\n  const loaders = createLoaders(authToken);\n  res.send(renderPage(req, loaders));\n});\n\napp.listen();\n```\n\n#### Caching and Batching\n\nSubsequent calls to `.load()` with the same key will result in that key not\nappearing in the keys provided to your batch function. _However_, the resulting\nPromise will still wait on the current batch to complete. This way both cached\nand uncached requests will resolve at the same time, allowing DataLoader\noptimizations for subsequent dependent loads.\n\nIn the example below, User `1` happens to be cached. However, because User `1`\nand `2` are loaded in the same tick, they will resolve at the same time. This\nmeans both `user.bestFriendID` loads will also happen in the same tick which\nresults in two total requests (the same as if User `1` had not been cached).\n\n```js\nuserLoader.prime(1, { bestFriend: 3 });\n\nasync function getBestFriend(userID) {\n  const user = await userLoader.load(userID);\n  return await userLoader.load(user.bestFriendID);\n}\n\n// In one part of your application\ngetBestFriend(1);\n\n// Elsewhere\ngetBestFriend(2);\n```\n\nWithout this optimization, if the cached User `1` resolved immediately, this\ncould result in three total requests since each `user.bestFriendID` load would\nhappen at different times.\n\n#### Clearing Cache\n\nIn certain uncommon cases, clearing the request cache may be necessary.\n\nThe most common example when clearing the loader's cache is necessary is after\na mutation or update within the same request, when a cached value could be out of\ndate and future loads should not use any possibly cached value.\n\nHere's a simple example using SQL UPDATE to illustrate.\n\n```js\n// Request begins...\nconst userLoader = new DataLoader(...);\n\n// And a value happens to be loaded (and cached).\nconst user = await userLoader.load(4);\n\n// A mutation occurs, invalidating what might be in cache.\nawait sqlRun('UPDATE users WHERE id=4 SET username=\"zuck\"');\nuserLoader.clear(4);\n\n// Later the value load is loaded again so the mutated data appears.\nconst user = await userLoader.load(4);\n\n// Request completes.\n```\n\n#### Caching Errors\n\nIf a batch load fails (that is, a batch function throws or returns a rejected\nPromise), then the requested values will not be cached. However if a batch\nfunction returns an `Error` instance for an individual value, that `Error` will\nbe cached to avoid frequently loading the same `Error`.\n\nIn some circumstances you may wish to clear the cache for these individual Errors:\n\n```js\ntry {\n  const user = await userLoader.load(1);\n} catch (error) {\n  if (/* determine if the error should not be cached */) {\n    userLoader.clear(1);\n  }\n  throw error\n}\n```\n\n#### Disabling Cache\n\nIn certain uncommon cases, a DataLoader which _does not_ cache may be desirable.\nCalling `new DataLoader(myBatchFn, { cache: false })` will ensure that every\ncall to `.load()` will produce a _new_ Promise, and requested keys will not be\nsaved in memory.\n\nHowever, when the memoization cache is disabled, your batch function will\nreceive an array of keys which may contain duplicates! Each key will be\nassociated with each call to `.load()`. Your batch loader should provide a value\nfor each instance of the requested key.\n\nFor example:\n\n```js\nconst myLoader = new DataLoader(\n  keys => {\n    console.log(keys);\n    return someBatchLoadFn(keys);\n  },\n  { cache: false },\n);\n\nmyLoader.load('A');\nmyLoader.load('B');\nmyLoader.load('A');\n\n// > [ 'A', 'B', 'A' ]\n```\n\nMore complex cache behavior can be achieved by calling `.clear()` or `.clearAll()`\nrather than disabling the cache completely. For example, this DataLoader will\nprovide unique keys to a batch function due to the memoization cache being\nenabled, but will immediately clear its cache when the batch function is called\nso later requests will load new values.\n\n```js\nconst myLoader = new DataLoader(keys => {\n  myLoader.clearAll();\n  return someBatchLoadFn(keys);\n});\n```\n\n#### Custom Cache\n\nAs mentioned above, DataLoader is intended to be used as a per-request cache.\nSince requests are short-lived, DataLoader uses an infinitely growing [Map][] as\na memoization cache. This should not pose a problem as most requests are\nshort-lived and the entire cache can be discarded after the request completes.\n\nHowever this memoization caching strategy isn't safe when using a long-lived\nDataLoader, since it could consume too much memory. If using DataLoader in this\nway, you can provide a custom Cache instance with whatever behavior you prefer,\nas long as it follows the same API as [Map][].\n\nThe example below uses an LRU (least recently used) cache to limit total memory\nto hold at most 100 cached values via the [lru_map][] npm package.\n\n```js\nimport { LRUMap } from 'lru_map';\n\nconst myLoader = new DataLoader(someBatchLoadFn, {\n  cacheMap: new LRUMap(100),\n});\n```\n\nMore specifically, any object that implements the methods `get()`, `set()`,\n`delete()` and `clear()` methods can be provided. This allows for custom Maps\nwhich implement various [cache algorithms][] to be provided.\n\n## API\n\n#### class DataLoader\n\nDataLoader creates a public API for loading data from a particular\ndata back-end with unique keys such as the `id` column of a SQL table or\ndocument name in a MongoDB database, given a batch loading function.\n\nEach `DataLoader` instance contains a unique memoized cache. Use caution when\nused in long-lived applications or those which serve many users with different\naccess permissions and consider creating a new instance per web request.\n\n##### `new DataLoader(batchLoadFn [, options])`\n\nCreate a new `DataLoader` given a batch loading function and options.\n\n- _batchLoadFn_: A function which accepts an Array of keys, and returns a\n  Promise which resolves to an Array of values.\n\n- _options_: An optional object of options:\n\n| Option Key        | Type     | Default                                   | Description                                                                                                                                                                                |\n| ----------------- | -------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| `batch`           | Boolean  | `true`                                    | Set to `false` to disable batching, invoking `batchLoadFn` with a single load key. This is equivalent to setting `maxBatchSize` to `1`.                                                    |\n| `maxBatchSize`    | Number   | `Infinity`                                | Limits the number of items that get passed in to the `batchLoadFn`. May be set to `1` to disable batching.                                                                                 |\n| `batchScheduleFn` | Function | See [Batch scheduling](#batch-scheduling) | A function to schedule the later execution of a batch. The function is expected to call the provided callback in the immediate future.                                                     |\n| `cache`           | Boolean  | `true`                                    | Set to `false` to disable memoization caching, creating a new Promise and new key in the `batchLoadFn` for every load of the same key. This is equivalent to setting `cacheMap` to `null`. |\n| `cacheKeyFn`      | Function | `key => key`                              | Produces cache key for a given load key. Useful when objects are keys and two objects should be considered equivalent.                                                                     |\n| `cacheMap`        | Object   | `new Map()`                               | Instance of [Map][] (or an object with a similar API) to be used as cache. May be set to `null` to disable caching.                                                                        |\n| `name`            | String   | `null`                                    | The name given to this `DataLoader` instance. Useful for APM tools.                                                                                                                        |\n\n##### `load(key)`\n\nLoads a key, returning a `Promise` for the value represented by that key.\n\n- _key_: A key value to load.\n\n##### `loadMany(keys)`\n\nLoads multiple keys, promising an array of values:\n\n```js\nconst [a, b] = await myLoader.loadMany(['a', 'b']);\n```\n\nThis is similar to the more verbose:\n\n```js\nconst [a, b] = await Promise.all([myLoader.load('a'), myLoader.load('b')]);\n```\n\nHowever it is different in the case where any load fails. Where\nPromise.all() would reject, loadMany() always resolves, however each result\nis either a value or an Error instance.\n\n```js\nvar [a, b, c] = await myLoader.loadMany(['a', 'b', 'badkey']);\n// c instanceof Error\n```\n\n- _keys_: An array of key values to load.\n\n##### `clear(key)`\n\nClears the value at `key` from the cache, if it exists. Returns itself for\nmethod chaining.\n\n- _key_: A key value to clear.\n\n##### `clearAll()`\n\nClears the entire cache. To be used when some event results in unknown\ninvalidations across this particular `DataLoader`. Returns itself for\nmethod chaining.\n\n##### `prime(key, value)`\n\nPrimes the cache with the provided key and value. If the key already exists, no\nchange is made. (To forcefully prime the cache, clear the key first with\n`loader.clear(key).prime(key, value)`.) Returns itself for method chaining.\n\nTo prime the cache with an error at a key, provide an Error instance.\n\n## Using with GraphQL\n\nDataLoader pairs nicely well with [GraphQL][graphql-js]. GraphQL fields are\ndesigned to be stand-alone functions. Without a caching or batching mechanism,\nit's easy for a naive GraphQL server to issue new database requests each time a\nfield is resolved.\n\nConsider the following GraphQL request:\n\n```\n{\n  me {\n    name\n    bestFriend {\n      name\n    }\n    friends(first: 5) {\n      name\n      bestFriend {\n        name\n      }\n    }\n  }\n}\n```\n\nNaively, if `me`, `bestFriend` and `friends` each need to request the backend,\nthere could be at most 13 database requests!\n\nWhen using DataLoader, we could define the `User` type using the\n[SQLite](examples/SQL.md) example with clearer code and at most 4 database requests,\nand possibly fewer if there are cache hits.\n\n```js\nconst UserType = new GraphQLObjectType({\n  name: 'User',\n  fields: () => ({\n    name: { type: GraphQLString },\n    bestFriend: {\n      type: UserType,\n      resolve: user => userLoader.load(user.bestFriendID),\n    },\n    friends: {\n      args: {\n        first: { type: GraphQLInt },\n      },\n      type: new GraphQLList(UserType),\n      resolve: async (user, { first }) => {\n        const rows = await queryLoader.load([\n          'SELECT toID FROM friends WHERE fromID=? LIMIT ?',\n          user.id,\n          first,\n        ]);\n        return rows.map(row => userLoader.load(row.toID));\n      },\n    },\n  }),\n});\n```\n\n## Common Patterns\n\n### Creating a new DataLoader per request.\n\nIn many applications, a web server using DataLoader serves requests to many\ndifferent users with different access permissions. It may be dangerous to use\none cache across many users, and is encouraged to create a new DataLoader\nper request:\n\n```js\nfunction createLoaders(authToken) {\n  return {\n    users: new DataLoader(ids => genUsers(authToken, ids)),\n    cdnUrls: new DataLoader(rawUrls => genCdnUrls(authToken, rawUrls)),\n    stories: new DataLoader(keys => genStories(authToken, keys)),\n  };\n}\n\n// When handling an incoming web request:\nconst loaders = createLoaders(request.query.authToken);\n\n// Then, within application logic:\nconst user = await loaders.users.load(4);\nconst pic = await loaders.cdnUrls.load(user.rawPicUrl);\n```\n\nCreating an object where each key is a `DataLoader` is one common pattern which\nprovides a single value to pass around to code which needs to perform\ndata loading, such as part of the `rootValue` in a [graphql-js][] request.\n\n### Loading by alternative keys.\n\nOccasionally, some kind of value can be accessed in multiple ways. For example,\nperhaps a \"User\" type can be loaded not only by an \"id\" but also by a \"username\"\nvalue. If the same user is loaded by both keys, then it may be useful to fill\nboth caches when a user is loaded from either source:\n\n```js\nconst userByIDLoader = new DataLoader(async ids => {\n  const users = await genUsersByID(ids);\n  for (let user of users) {\n    usernameLoader.prime(user.username, user);\n  }\n  return users;\n});\n\nconst usernameLoader = new DataLoader(async names => {\n  const users = await genUsernames(names);\n  for (let user of users) {\n    userByIDLoader.prime(user.id, user);\n  }\n  return users;\n});\n```\n\n### Freezing results to enforce immutability\n\nSince DataLoader caches values, it's typically assumed these values will be\ntreated as if they were immutable. While DataLoader itself doesn't enforce\nthis, you can create a higher-order function to enforce immutability\nwith Object.freeze():\n\n```js\nfunction freezeResults(batchLoader) {\n  return keys => batchLoader(keys).then(values => values.map(Object.freeze));\n}\n\nconst myLoader = new DataLoader(freezeResults(myBatchLoader));\n```\n\n### Batch functions which return Objects instead of Arrays\n\nDataLoader expects batch functions which return an Array of the same length as\nthe provided keys. However this is not always a common return format from other\nlibraries. A DataLoader higher-order function can convert from one format to another. The example below converts a `{ key: value }` result to the format\nDataLoader expects.\n\n```js\nfunction objResults(batchLoader) {\n  return keys =>\n    batchLoader(keys).then(objValues =>\n      keys.map(key => objValues[key] || new Error(`No value for ${key}`)),\n    );\n}\n\nconst myLoader = new DataLoader(objResults(myBatchLoader));\n```\n\n## Common Back-ends\n\nLooking to get started with a specific back-end? Try the [loaders in the examples directory](/examples).\n\n## Other Implementations\n\nListed in alphabetical order\n\n- Elixir\n  - [dataloader](https://github.com/absinthe-graphql/dataloader)\n- Golang\n  - [Dataloader](https://github.com/nicksrandall/dataloader)\n- Java\n  - [java-dataloader](https://github.com/graphql-java/java-dataloader)\n- .Net\n  - [GraphQL .NET DataLoader](https://graphql-dotnet.github.io/docs/guides/dataloader/)\n  - [Green Donut](https://github.com/ChilliCream/graphql-platform?tab=readme-ov-file#green-donut)\n- Perl\n  - [perl-DataLoader](https://github.com/richardjharris/perl-DataLoader)\n- PHP\n  - [DataLoaderPHP](https://github.com/overblog/dataloader-php)\n- Python\n  - [aiodataloader](https://github.com/syrusakbary/aiodataloader)\n- ReasonML\n  - [bs-dataloader](https://github.com/ulrikstrid/bs-dataloader)\n- Ruby\n  - [BatchLoader](https://github.com/exaspark/batch-loader)\n  - [Dataloader](https://github.com/sheerun/dataloader)\n  - [GraphQL Batch](https://github.com/Shopify/graphql-batch)\n- Rust\n  - [Dataloader](https://github.com/cksac/dataloader-rs)\n- Swift\n  - [SwiftDataLoader](https://github.com/kimdv/SwiftDataLoader)\n- C++\n  - [cppdataloader](https://github.com/jafarlihi/cppdataloader)\n\n## Video Source Code Walkthrough\n\n**DataLoader Source Code Walkthrough (YouTube):**\n\nA walkthrough of the DataLoader v1 source code. While the source has changed\nsince this video was made, it is still a good overview of the rationale of\nDataLoader and how it works.\n\n<a href=\"https://youtu.be/OQTnXNCDywA\" target=\"_blank\" alt=\"DataLoader Source Code Walkthrough\"><img src=\"https://img.youtube.com/vi/OQTnXNCDywA/0.jpg\" /></a>\n\n[@schrockn]: https://github.com/schrockn\n[Map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map\n[graphql-js]: https://github.com/graphql/graphql-js\n[cache algorithms]: https://en.wikipedia.org/wiki/Cache_algorithms\n[express]: http://expressjs.com/\n[babel/polyfill]: https://babeljs.io/docs/usage/polyfill/\n[lru_map]: https://github.com/rsms/js-lru\n[source code]: https://github.com/graphql/dataloader/blob/main/src/index.js\n\n# Contributing to this repo\n\nThis repository is managed by EasyCLA. Project participants must sign the free ([GraphQL Specification Membership agreement](https://preview-spec-membership.graphql.org) before making a contribution. You only need to do this one time, and it can be signed by [individual contributors](http://individual-spec-membership.graphql.org/) or their [employers](http://corporate-spec-membership.graphql.org/).\n\nTo initiate the signature process please open a PR against this repo. The EasyCLA bot will block the merge if we still need a membership agreement from you.\n\nYou can find [detailed information here](https://github.com/graphql/graphql-wg/tree/main/membership). If you have issues, please email [operations@graphql.org](mailto:operations@graphql.org).\n\nIf your company benefits from GraphQL and you would like to provide essential financial support for the systems and people that power our community, please also consider membership in the [GraphQL Foundation](https://foundation.graphql.org/join).\n"
  },
  {
    "path": "babel.config.js",
    "content": "module.exports = api => ({\n  presets: api.env('test')\n    ? ['@babel/preset-flow']\n    : [['@babel/preset-env', { loose: true }], '@babel/preset-flow'],\n});\n"
  },
  {
    "path": "examples/CouchDB.md",
    "content": "# Using DataLoader with CouchDB\n\nCouchDB is a \"NoSQL\" document database which supports batch loading via the\n[HTTP Bulk Document API](http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API),\nmaking it well suited for use with DataLoader.\n\nThis example uses the [nano][] CouchDB client which offers a `fetch` method\nsupporting the bulk document API.\n\n```js\nconst DataLoader = require('dataloader');\nconst nano = require('nano');\n\nconst couch = nano('http://localhost:5984');\n\nconst userDB = couch.use('users');\nconst userLoader = new DataLoader(\n  keys =>\n    new Promise((resolve, reject) => {\n      userDB.fetch({ keys: keys }, (error, docs) => {\n        if (error) {\n          return reject(error);\n        }\n        resolve(\n          docs.rows.map(row => (row.error ? new Error(row.error) : row.doc)),\n        );\n      });\n    }),\n);\n\n// Usage\n\nconst promise1 = userLoader.load('8fce1902834ac6458e9886fa7f89c0ef');\nconst promise2 = userLoader.load('00a271787f89c0ef2e10e88a0c00048b');\nconst [user1, user2] = await Promise.all([promise1, promise2]);\nconsole.log(user1, user2);\n```\n\n[nano]: https://github.com/dscape/nano\n"
  },
  {
    "path": "examples/GoogleDatastore.md",
    "content": "# Using DataLoader with Google Datastore\n\nGoogle Datastore is a \"NoSQL\" document database which supports [batch operations](https://cloud.google.com/datastore/docs/concepts/entities#batch_operations),\nmaking it well suited for use with DataLoader.\n\nHere we build an example Google Datastore DataLoader using [@google-cloud/datastore](https://cloud.google.com/nodejs/docs/reference/datastore/1.3.x/Datastore).\n\n```js\nconst Datastore = require('@google-cloud/datastore');\n\nconst datastore = new Datastore();\n\nconst datastoreLoader = new DataLoader(\n  async keys => {\n    const results = await datastore.get(keys);\n    // Sort resulting entities by the keys they were requested with.\n    const entities = results[0];\n    const entitiesByKey = {};\n    entities.forEach(entity => {\n      entitiesByKey[JSON.stringify(entity[datastore.KEY])] = entity;\n    });\n    return keys.map(key => entitiesByKey[JSON.stringify(key)] || null);\n  },\n  {\n    // Datastore complex keys need to be converted to a string for use as cache keys\n    cacheKeyFn: key => JSON.stringify(key),\n  },\n);\n```\n"
  },
  {
    "path": "examples/Knex.md",
    "content": "# Using DataLoader with Knex.js\n\nThis example demonstrates how to use **DataLoader** with SQL databases via\n[Knex.js][knex], which is a SQL query builder and a client for popular\ndatabases such as **PostgreSQL**, **MySQL**, **MariaDB** etc.\n\nSimilarly to the [SQL](./SQL.md) example, you can use \"where in\" clause to\nfetch multiple records by the list of IDs with the only difference that you\ndon't have to write any SQL code by hand.\n\n```js\nconst DataLoader = require('dataloader');\nconst db = require('./db'); // an instance of Knex client\n\n// The list of data loaders\n\nconst loaders = {\n  user: new DataLoader(ids =>\n    db\n      .table('users')\n      .whereIn('id', ids)\n      .select()\n      .then(rows => ids.map(id => rows.find(x => x.id === id))),\n  ),\n\n  story: new DataLoader(ids =>\n    db\n      .table('stories')\n      .whereIn('id', ids)\n      .select()\n      .then(rows => ids.map(id => rows.find(x => x.id === id))),\n  ),\n\n  storiesByUserId: new DataLoader(ids =>\n    db\n      .table('stories')\n      .whereIn('author_id', ids)\n      .select()\n      .then(rows => ids.map(id => rows.filter(x => x.author_id === id))),\n  ),\n};\n\n// Usage\n\nconst [user, stories] = await Promise.all([\n  loaders.user.load('1234'),\n  loaders.storiesByUserId.load('1234'),\n]);\n```\n\nFor a complete example visit [kriasoft/nodejs-api-starter][nsk].\n\n[knex]: http://knexjs.org/\n[nsk]: https://github.com/kriasoft/nodejs-api-starter#readme\n"
  },
  {
    "path": "examples/Redis.md",
    "content": "# Using DataLoader with Redis\n\nRedis is a very simple key-value store which provides the batch load method\n[MGET](http://redis.io/commands/mget) which makes it very well suited for use\nwith DataLoader.\n\nHere we build an example Redis DataLoader using [node_redis][].\n\n```js\nconst DataLoader = require('dataloader');\nconst redis = require('redis');\n\nconst client = redis.createClient();\n\nconst redisLoader = new DataLoader(\n  keys =>\n    new Promise((resolve, reject) => {\n      client.mget(keys, (error, results) => {\n        if (error) {\n          return reject(error);\n        }\n        resolve(\n          results.map((result, index) =>\n            result !== null ? result : new Error(`No key: ${keys[index]}`),\n          ),\n        );\n      });\n    }),\n);\n```\n\n[node_redis]: https://github.com/NodeRedis/node_redis\n"
  },
  {
    "path": "examples/RethinkDB.md",
    "content": "# RethinkDb\n\nRethinkDb offers a batching method called `getAll` but there are a few caveats :\n\n- Order of results is not guaranteed ([rethinkdb/rethinkdb#5187](https://github.com/rethinkdb/rethinkdb/issues/5187))\n- Non-existent keys will not return an empty record\n\nFor example, against a table `example_table` with these records:\n\n```js\n[\n  { id: 1, name: 'Document 1' },\n  { id: 2, name: 'Document 2' },\n];\n```\n\nA query `r.getAll(1, 2, 3)` could return:\n\n```js\n[\n  { id: 2, name: 'Document 2' },\n  { id: 1, name: 'Document 1' },\n];\n```\n\nBecause query keys and values are associated by position in the dataloader\ncache, this naive implementation won't work (with the same table as above):\n\n```js\nconst r = require('rethinkdb');\nconst db = await r.connect();\n\nconst exampleLoader = new DataLoader(async keys => {\n  const result = await db.table('example_table').getAll(...keys);\n  return result.toArray();\n});\n\nawait exampleLoader.loadMany([1, 2, 3]); // Throws (values length !== keys length)\n\nawait exampleLoader.loadMany([1, 2]);\nawait exampleLoader.load(1); // {\"id\": 2, \"name\": \"Document 2\"}\n```\n\nA solution is to normalize results returned by `getAll` to match the structure\nof supplied `keys`.\n\nTo achieve this efficiently, we first write an indexing function. This function\nwill return a `Map` indexing results.\n\nParameters:\n\n- `results`: Array of RethinkDb results\n- `indexField`: String indicating which field was used as index for this batch query\n- `cacheKeyFn`: Optional function used to serialize non-scalar index field values\n\n```js\nfunction indexResults(results, indexField, cacheKeyFn = key => key) {\n  const indexedResults = new Map();\n  results.forEach(res => {\n    indexedResults.set(cacheKeyFn(res[indexField]), res);\n  });\n  return indexedResults;\n}\n```\n\nThen, we can leverage our Map to normalize RethinkDb results with another\nutility function which will produce a normalizing function.\n\n```js\nfunction normalizeRethinkDbResults(keys, indexField, cacheKeyFn = key => key) {\n  return results => {\n    const indexedResults = indexResults(results, indexField, cacheKeyFn);\n    return keys.map(\n      val =>\n        indexedResults.get(cacheKeyFn(val)) ||\n        new Error(`Key not found : ${val}`),\n    );\n  };\n}\n```\n\nFull dataloader implementation:\n\n```js\nconst r = require('rethinkdb');\nconst db = await r.connect();\n\nconst exampleLoader = new DataLoader(async keys => {\n  const results = await db.table('example_table').getAll(...keys);\n  return normalizeRethinkDbResults(res.toArray(), 'id');\n});\n\n// [{\"id\": 1, \"name\": \"Document 1\"}, {\"id\": 2, \"name\": \"Document 2\"}, Error];\nawait exampleLoader.loadMany([1, 2, 3]);\n\n// {\"id\": 1, \"name\": \"Document 1\"}\nawait exampleLoader.load(1);\n```\n"
  },
  {
    "path": "examples/SQL.md",
    "content": "# Using DataLoader with SQLite\n\nWhile not a key-value store, SQL offers a natural batch mechanism with\n`SELECT * WHERE IN` statements. While `DataLoader` is best suited for key-value\nstores, it is still suited for SQL when queries remain simple. This example\nrequests the entire row at a given `id`, however your usage may differ.\n\n```js\nconst DataLoader = require('dataloader');\nconst sqlite3 = require('sqlite3');\n\nconst db = new sqlite3.Database('./to/your/db.sql');\n\n// Dispatch a WHERE-IN query, ensuring response has rows in correct order.\nconst userLoader = new DataLoader(\n  ids =>\n    new Promise((resolve, reject) => {\n      db.all(\n        'SELECT * FROM users WHERE id IN $ids',\n        { $ids: ids },\n        (error, rows) => {\n          if (error) {\n            reject(error);\n          } else {\n            resolve(\n              ids.map(\n                id =>\n                  rows.find(row => row.id === id) ||\n                  new Error(`Row not found: ${id}`),\n              ),\n            );\n          }\n        },\n      );\n    }),\n);\n\n// Usage\n\nconst promise1 = userLoader.load('1234');\nconst promise2 = userLoader.load('5678');\nconst [user1, user2] = await Promise.all([promise1, promise2]);\nconsole.log(user1, user2);\n```\n\n[sqlite3]: https://github.com/mapbox/node-sqlite3\n"
  },
  {
    "path": "flow-typed/npm/jest_v24.x.x.js",
    "content": "// flow-typed signature: 27f8467378a99b6130bd20f54f31a644\n// flow-typed version: 6cb9e99836/jest_v24.x.x/flow_>=v0.104.x\n\ntype JestMockFn<TArguments: $ReadOnlyArray<*>, TReturn> = {\n (...args: TArguments): TReturn,\n /**\n  * An object for introspecting mock calls\n  */\n mock: {\n  /**\n   * An array that represents all calls that have been made into this mock\n   * function. Each call is represented by an array of arguments that were\n   * passed during the call.\n   */\n  calls: Array<TArguments>,\n  /**\n   * An array that contains all the object instances that have been\n   * instantiated from this mock function.\n   */\n  instances: Array<TReturn>,\n  /**\n   * An array that contains all the object results that have been\n   * returned by this mock function call\n   */\n  results: Array<{\n   isThrow: boolean,\n   value: TReturn,\n   ...\n  }>,\n  ...\n },\n /**\n  * Resets all information stored in the mockFn.mock.calls and\n  * mockFn.mock.instances arrays. Often this is useful when you want to clean\n  * up a mock's usage data between two assertions.\n  */\n mockClear(): void,\n /**\n  * Resets all information stored in the mock. This is useful when you want to\n  * completely restore a mock back to its initial state.\n  */\n mockReset(): void,\n /**\n  * Removes the mock and restores the initial implementation. This is useful\n  * when you want to mock functions in certain test cases and restore the\n  * original implementation in others. Beware that mockFn.mockRestore only\n  * works when mock was created with jest.spyOn. Thus you have to take care of\n  * restoration yourself when manually assigning jest.fn().\n  */\n mockRestore(): void,\n /**\n  * Accepts a function that should be used as the implementation of the mock.\n  * The mock itself will still record all calls that go into and instances\n  * that come from itself -- the only difference is that the implementation\n  * will also be executed when the mock is called.\n  */\n mockImplementation(\n   fn: (...args: TArguments) => TReturn\n ): JestMockFn<TArguments, TReturn>,\n /**\n  * Accepts a function that will be used as an implementation of the mock for\n  * one call to the mocked function. Can be chained so that multiple function\n  * calls produce different results.\n  */\n mockImplementationOnce(\n   fn: (...args: TArguments) => TReturn\n ): JestMockFn<TArguments, TReturn>,\n /**\n  * Accepts a string to use in test result output in place of \"jest.fn()\" to\n  * indicate which mock function is being referenced.\n  */\n mockName(name: string): JestMockFn<TArguments, TReturn>,\n /**\n  * Just a simple sugar function for returning `this`\n  */\n mockReturnThis(): void,\n /**\n  * Accepts a value that will be returned whenever the mock function is called.\n  */\n mockReturnValue(value: TReturn): JestMockFn<TArguments, TReturn>,\n /**\n  * Sugar for only returning a value once inside your mock\n  */\n mockReturnValueOnce(value: TReturn): JestMockFn<TArguments, TReturn>,\n /**\n  * Sugar for jest.fn().mockImplementation(() => Promise.resolve(value))\n  */\n mockResolvedValue(value: TReturn): JestMockFn<TArguments, Promise<TReturn>>,\n /**\n  * Sugar for jest.fn().mockImplementationOnce(() => Promise.resolve(value))\n  */\n mockResolvedValueOnce(\n   value: TReturn\n ): JestMockFn<TArguments, Promise<TReturn>>,\n /**\n  * Sugar for jest.fn().mockImplementation(() => Promise.reject(value))\n  */\n mockRejectedValue(value: TReturn): JestMockFn<TArguments, Promise<any>>,\n /**\n  * Sugar for jest.fn().mockImplementationOnce(() => Promise.reject(value))\n  */\n mockRejectedValueOnce(value: TReturn): JestMockFn<TArguments, Promise<any>>,\n ...\n};\n\ntype JestAsymmetricEqualityType = { /**\n * A custom Jasmine equality tester\n */\nasymmetricMatch(value: mixed): boolean, ... };\n\ntype JestCallsType = {\n allArgs(): mixed,\n all(): mixed,\n any(): boolean,\n count(): number,\n first(): mixed,\n mostRecent(): mixed,\n reset(): void,\n ...\n};\n\ntype JestClockType = {\n install(): void,\n mockDate(date: Date): void,\n tick(milliseconds?: number): void,\n uninstall(): void,\n ...\n};\n\ntype JestMatcherResult = {\n message?: string | (() => string),\n pass: boolean,\n ...\n};\n\ntype JestMatcher = (\n  received: any,\n  ...actual: Array<any>\n) => JestMatcherResult | Promise<JestMatcherResult>;\n\ntype JestPromiseType = {\n /**\n  * Use rejects to unwrap the reason of a rejected promise so any other\n  * matcher can be chained. If the promise is fulfilled the assertion fails.\n  */\n rejects: JestExpectType,\n /**\n  * Use resolves to unwrap the value of a fulfilled promise so any other\n  * matcher can be chained. If the promise is rejected the assertion fails.\n  */\n resolves: JestExpectType,\n ...\n};\n\n/**\n * Jest allows functions and classes to be used as test names in test() and\n * describe()\n */\ntype JestTestName = string | Function;\n\n/**\n *  Plugin: jest-styled-components\n */\n\ntype JestStyledComponentsMatcherValue =\n  | string\n  | JestAsymmetricEqualityType\n  | RegExp\n  | typeof undefined;\n\ntype JestStyledComponentsMatcherOptions = {\n media?: string,\n modifier?: string,\n supports?: string,\n ...\n};\n\ntype JestStyledComponentsMatchersType = { toHaveStyleRule(\n  property: string,\n  value: JestStyledComponentsMatcherValue,\n  options?: JestStyledComponentsMatcherOptions\n): void, ... };\n\n/**\n *  Plugin: jest-enzyme\n */\ntype EnzymeMatchersType = {\n // 5.x\n toBeEmpty(): void,\n toBePresent(): void,\n // 6.x\n toBeChecked(): void,\n toBeDisabled(): void,\n toBeEmptyRender(): void,\n toContainMatchingElement(selector: string): void,\n toContainMatchingElements(n: number, selector: string): void,\n toContainExactlyOneMatchingElement(selector: string): void,\n toContainReact(element: React$Element<any>): void,\n toExist(): void,\n toHaveClassName(className: string): void,\n toHaveHTML(html: string): void,\n toHaveProp: ((propKey: string, propValue?: any) => void) &\n   ((props: {...}) => void),\n toHaveRef(refName: string): void,\n toHaveState: ((stateKey: string, stateValue?: any) => void) &\n   ((state: {...}) => void),\n toHaveStyle: ((styleKey: string, styleValue?: any) => void) &\n   ((style: {...}) => void),\n toHaveTagName(tagName: string): void,\n toHaveText(text: string): void,\n toHaveValue(value: any): void,\n toIncludeText(text: string): void,\n toMatchElement(\n   element: React$Element<any>,\n   options?: {| ignoreProps?: boolean, verbose?: boolean |}\n ): void,\n toMatchSelector(selector: string): void,\n // 7.x\n toHaveDisplayName(name: string): void,\n ...\n};\n\n// DOM testing library extensions (jest-dom)\n// https://github.com/testing-library/jest-dom\ntype DomTestingLibraryType = {\n /**\n  * @deprecated\n  */\n toBeInTheDOM(container?: HTMLElement): void,\n toBeInTheDocument(): void,\n toBeVisible(): void,\n toBeEmpty(): void,\n toBeDisabled(): void,\n toBeEnabled(): void,\n toBeInvalid(): void,\n toBeRequired(): void,\n toBeValid(): void,\n toContainElement(element: HTMLElement | null): void,\n toContainHTML(htmlText: string): void,\n toHaveAttribute(attr: string, value?: any): void,\n toHaveClass(...classNames: string[]): void,\n toHaveFocus(): void,\n toHaveFormValues(expectedValues: { [name: string]: any, ... }): void,\n toHaveStyle(css: string): void,\n toHaveTextContent(\n   text: string | RegExp,\n   options?: { normalizeWhitespace: boolean, ... }\n ): void,\n toHaveValue(value?: string | string[] | number): void,\n ...\n};\n\n// Jest JQuery Matchers: https://github.com/unindented/custom-jquery-matchers\ntype JestJQueryMatchersType = {\n toExist(): void,\n toHaveLength(len: number): void,\n toHaveId(id: string): void,\n toHaveClass(className: string): void,\n toHaveTag(tag: string): void,\n toHaveAttr(key: string, val?: any): void,\n toHaveProp(key: string, val?: any): void,\n toHaveText(text: string | RegExp): void,\n toHaveData(key: string, val?: any): void,\n toHaveValue(val: any): void,\n toHaveCss(css: { [key: string]: any, ... }): void,\n toBeChecked(): void,\n toBeDisabled(): void,\n toBeEmpty(): void,\n toBeHidden(): void,\n toBeSelected(): void,\n toBeVisible(): void,\n toBeFocused(): void,\n toBeInDom(): void,\n toBeMatchedBy(sel: string): void,\n toHaveDescendant(sel: string): void,\n toHaveDescendantWithText(sel: string, text: string | RegExp): void,\n ...\n};\n\n// Jest Extended Matchers: https://github.com/jest-community/jest-extended\ntype JestExtendedMatchersType = {\n /**\n  * Note: Currently unimplemented\n  * Passing assertion\n  *\n  * @param {String} message\n  */\n //  pass(message: string): void;\n\n /**\n  * Note: Currently unimplemented\n  * Failing assertion\n  *\n  * @param {String} message\n  */\n //  fail(message: string): void;\n\n /**\n  * Use .toBeEmpty when checking if a String '', Array [] or Object {} is empty.\n  */\n toBeEmpty(): void,\n /**\n  * Use .toBeOneOf when checking if a value is a member of a given Array.\n  * @param {Array.<*>} members\n  */\n toBeOneOf(members: any[]): void,\n /**\n  * Use `.toBeNil` when checking a value is `null` or `undefined`.\n  */\n toBeNil(): void,\n /**\n  * Use `.toSatisfy` when you want to use a custom matcher by supplying a predicate function that returns a `Boolean`.\n  * @param {Function} predicate\n  */\n toSatisfy(predicate: (n: any) => boolean): void,\n /**\n  * Use `.toBeArray` when checking if a value is an `Array`.\n  */\n toBeArray(): void,\n /**\n  * Use `.toBeArrayOfSize` when checking if a value is an `Array` of size x.\n  * @param {Number} x\n  */\n toBeArrayOfSize(x: number): void,\n /**\n  * Use `.toIncludeAllMembers` when checking if an `Array` contains all of the same members of a given set.\n  * @param {Array.<*>} members\n  */\n toIncludeAllMembers(members: any[]): void,\n /**\n  * Use `.toIncludeAnyMembers` when checking if an `Array` contains any of the members of a given set.\n  * @param {Array.<*>} members\n  */\n toIncludeAnyMembers(members: any[]): void,\n /**\n  * Use `.toSatisfyAll` when you want to use a custom matcher by supplying a predicate function that returns a `Boolean` for all values in an array.\n  * @param {Function} predicate\n  */\n toSatisfyAll(predicate: (n: any) => boolean): void,\n /**\n  * Use `.toBeBoolean` when checking if a value is a `Boolean`.\n  */\n toBeBoolean(): void,\n /**\n  * Use `.toBeTrue` when checking a value is equal (===) to `true`.\n  */\n toBeTrue(): void,\n /**\n  * Use `.toBeFalse` when checking a value is equal (===) to `false`.\n  */\n toBeFalse(): void,\n /**\n  * Use .toBeDate when checking if a value is a Date.\n  */\n toBeDate(): void,\n /**\n  * Use `.toBeFunction` when checking if a value is a `Function`.\n  */\n toBeFunction(): void,\n /**\n  * Use `.toHaveBeenCalledBefore` when checking if a `Mock` was called before another `Mock`.\n  *\n  * Note: Required Jest version >22\n  * Note: Your mock functions will have to be asynchronous to cause the timestamps inside of Jest to occur in a differentJS event loop, otherwise the mock timestamps will all be the same\n  *\n  * @param {Mock} mock\n  */\n toHaveBeenCalledBefore(mock: JestMockFn<any, any>): void,\n /**\n  * Use `.toBeNumber` when checking if a value is a `Number`.\n  */\n toBeNumber(): void,\n /**\n  * Use `.toBeNaN` when checking a value is `NaN`.\n  */\n toBeNaN(): void,\n /**\n  * Use `.toBeFinite` when checking if a value is a `Number`, not `NaN` or `Infinity`.\n  */\n toBeFinite(): void,\n /**\n  * Use `.toBePositive` when checking if a value is a positive `Number`.\n  */\n toBePositive(): void,\n /**\n  * Use `.toBeNegative` when checking if a value is a negative `Number`.\n  */\n toBeNegative(): void,\n /**\n  * Use `.toBeEven` when checking if a value is an even `Number`.\n  */\n toBeEven(): void,\n /**\n  * Use `.toBeOdd` when checking if a value is an odd `Number`.\n  */\n toBeOdd(): void,\n /**\n  * Use `.toBeWithin` when checking if a number is in between the given bounds of: start (inclusive) and end (exclusive).\n  *\n  * @param {Number} start\n  * @param {Number} end\n  */\n toBeWithin(start: number, end: number): void,\n /**\n  * Use `.toBeObject` when checking if a value is an `Object`.\n  */\n toBeObject(): void,\n /**\n  * Use `.toContainKey` when checking if an object contains the provided key.\n  *\n  * @param {String} key\n  */\n toContainKey(key: string): void,\n /**\n  * Use `.toContainKeys` when checking if an object has all of the provided keys.\n  *\n  * @param {Array.<String>} keys\n  */\n toContainKeys(keys: string[]): void,\n /**\n  * Use `.toContainAllKeys` when checking if an object only contains all of the provided keys.\n  *\n  * @param {Array.<String>} keys\n  */\n toContainAllKeys(keys: string[]): void,\n /**\n  * Use `.toContainAnyKeys` when checking if an object contains at least one of the provided keys.\n  *\n  * @param {Array.<String>} keys\n  */\n toContainAnyKeys(keys: string[]): void,\n /**\n  * Use `.toContainValue` when checking if an object contains the provided value.\n  *\n  * @param {*} value\n  */\n toContainValue(value: any): void,\n /**\n  * Use `.toContainValues` when checking if an object contains all of the provided values.\n  *\n  * @param {Array.<*>} values\n  */\n toContainValues(values: any[]): void,\n /**\n  * Use `.toContainAllValues` when checking if an object only contains all of the provided values.\n  *\n  * @param {Array.<*>} values\n  */\n toContainAllValues(values: any[]): void,\n /**\n  * Use `.toContainAnyValues` when checking if an object contains at least one of the provided values.\n  *\n  * @param {Array.<*>} values\n  */\n toContainAnyValues(values: any[]): void,\n /**\n  * Use `.toContainEntry` when checking if an object contains the provided entry.\n  *\n  * @param {Array.<String, String>} entry\n  */\n toContainEntry(entry: [string, string]): void,\n /**\n  * Use `.toContainEntries` when checking if an object contains all of the provided entries.\n  *\n  * @param {Array.<Array.<String, String>>} entries\n  */\n toContainEntries(entries: [string, string][]): void,\n /**\n  * Use `.toContainAllEntries` when checking if an object only contains all of the provided entries.\n  *\n  * @param {Array.<Array.<String, String>>} entries\n  */\n toContainAllEntries(entries: [string, string][]): void,\n /**\n  * Use `.toContainAnyEntries` when checking if an object contains at least one of the provided entries.\n  *\n  * @param {Array.<Array.<String, String>>} entries\n  */\n toContainAnyEntries(entries: [string, string][]): void,\n /**\n  * Use `.toBeExtensible` when checking if an object is extensible.\n  */\n toBeExtensible(): void,\n /**\n  * Use `.toBeFrozen` when checking if an object is frozen.\n  */\n toBeFrozen(): void,\n /**\n  * Use `.toBeSealed` when checking if an object is sealed.\n  */\n toBeSealed(): void,\n /**\n  * Use `.toBeString` when checking if a value is a `String`.\n  */\n toBeString(): void,\n /**\n  * Use `.toEqualCaseInsensitive` when checking if a string is equal (===) to another ignoring the casing of both strings.\n  *\n  * @param {String} string\n  */\n toEqualCaseInsensitive(string: string): void,\n /**\n  * Use `.toStartWith` when checking if a `String` starts with a given `String` prefix.\n  *\n  * @param {String} prefix\n  */\n toStartWith(prefix: string): void,\n /**\n  * Use `.toEndWith` when checking if a `String` ends with a given `String` suffix.\n  *\n  * @param {String} suffix\n  */\n toEndWith(suffix: string): void,\n /**\n  * Use `.toInclude` when checking if a `String` includes the given `String` substring.\n  *\n  * @param {String} substring\n  */\n toInclude(substring: string): void,\n /**\n  * Use `.toIncludeRepeated` when checking if a `String` includes the given `String` substring the correct number of times.\n  *\n  * @param {String} substring\n  * @param {Number} times\n  */\n toIncludeRepeated(substring: string, times: number): void,\n /**\n  * Use `.toIncludeMultiple` when checking if a `String` includes all of the given substrings.\n  *\n  * @param {Array.<String>} substring\n  */\n toIncludeMultiple(substring: string[]): void,\n ...\n};\n\ninterface JestExpectType {\n  not: JestExpectType &\n    EnzymeMatchersType &\n    DomTestingLibraryType &\n    JestJQueryMatchersType &\n    JestStyledComponentsMatchersType &\n    JestExtendedMatchersType;\n  /**\n   * If you have a mock function, you can use .lastCalledWith to test what\n   * arguments it was last called with.\n   */\n  lastCalledWith(...args: Array<any>): void;\n  /**\n   * toBe just checks that a value is what you expect. It uses === to check\n   * strict equality.\n   */\n  toBe(value: any): void;\n  /**\n   * Use .toBeCalledWith to ensure that a mock function was called with\n   * specific arguments.\n   */\n  toBeCalledWith(...args: Array<any>): void;\n  /**\n   * Using exact equality with floating point numbers is a bad idea. Rounding\n   * means that intuitive things fail.\n   */\n  toBeCloseTo(num: number, delta: any): void;\n  /**\n   * Use .toBeDefined to check that a variable is not undefined.\n   */\n  toBeDefined(): void;\n  /**\n   * Use .toBeFalsy when you don't care what a value is, you just want to\n   * ensure a value is false in a boolean context.\n   */\n  toBeFalsy(): void;\n  /**\n   * To compare floating point numbers, you can use toBeGreaterThan.\n   */\n  toBeGreaterThan(number: number): void;\n  /**\n   * To compare floating point numbers, you can use toBeGreaterThanOrEqual.\n   */\n  toBeGreaterThanOrEqual(number: number): void;\n  /**\n   * To compare floating point numbers, you can use toBeLessThan.\n   */\n  toBeLessThan(number: number): void;\n  /**\n   * To compare floating point numbers, you can use toBeLessThanOrEqual.\n   */\n  toBeLessThanOrEqual(number: number): void;\n  /**\n   * Use .toBeInstanceOf(Class) to check that an object is an instance of a\n   * class.\n   */\n  toBeInstanceOf(cls: Class<*>): void;\n  /**\n   * .toBeNull() is the same as .toBe(null) but the error messages are a bit\n   * nicer.\n   */\n  toBeNull(): void;\n  /**\n   * Use .toBeTruthy when you don't care what a value is, you just want to\n   * ensure a value is true in a boolean context.\n   */\n  toBeTruthy(): void;\n  /**\n   * Use .toBeUndefined to check that a variable is undefined.\n   */\n  toBeUndefined(): void;\n  /**\n   * Use .toContain when you want to check that an item is in a list. For\n   * testing the items in the list, this uses ===, a strict equality check.\n   */\n  toContain(item: any): void;\n  /**\n   * Use .toContainEqual when you want to check that an item is in a list. For\n   * testing the items in the list, this matcher recursively checks the\n   * equality of all fields, rather than checking for object identity.\n   */\n  toContainEqual(item: any): void;\n  /**\n   * Use .toEqual when you want to check that two objects have the same value.\n   * This matcher recursively checks the equality of all fields, rather than\n   * checking for object identity.\n   */\n  toEqual(value: any): void;\n  /**\n   * Use .toHaveBeenCalled to ensure that a mock function got called.\n   */\n  toHaveBeenCalled(): void;\n  toBeCalled(): void;\n  /**\n   * Use .toHaveBeenCalledTimes to ensure that a mock function got called exact\n   * number of times.\n   */\n  toHaveBeenCalledTimes(number: number): void;\n  toBeCalledTimes(number: number): void;\n  /**\n   *\n   */\n  toHaveBeenNthCalledWith(nthCall: number, ...args: Array<any>): void;\n  nthCalledWith(nthCall: number, ...args: Array<any>): void;\n  /**\n   *\n   */\n  toHaveReturned(): void;\n  toReturn(): void;\n  /**\n   *\n   */\n  toHaveReturnedTimes(number: number): void;\n  toReturnTimes(number: number): void;\n  /**\n   *\n   */\n  toHaveReturnedWith(value: any): void;\n  toReturnWith(value: any): void;\n  /**\n   *\n   */\n  toHaveLastReturnedWith(value: any): void;\n  lastReturnedWith(value: any): void;\n  /**\n   *\n   */\n  toHaveNthReturnedWith(nthCall: number, value: any): void;\n  nthReturnedWith(nthCall: number, value: any): void;\n  /**\n   * Use .toHaveBeenCalledWith to ensure that a mock function was called with\n   * specific arguments.\n   */\n  toHaveBeenCalledWith(...args: Array<any>): void;\n  toBeCalledWith(...args: Array<any>): void;\n  /**\n   * Use .toHaveBeenLastCalledWith to ensure that a mock function was last called\n   * with specific arguments.\n   */\n  toHaveBeenLastCalledWith(...args: Array<any>): void;\n  lastCalledWith(...args: Array<any>): void;\n  /**\n   * Check that an object has a .length property and it is set to a certain\n   * numeric value.\n   */\n  toHaveLength(number: number): void;\n  /**\n   *\n   */\n  toHaveProperty(propPath: string | $ReadOnlyArray<string>, value?: any): void;\n  /**\n   * Use .toMatch to check that a string matches a regular expression or string.\n   */\n  toMatch(regexpOrString: RegExp | string): void;\n  /**\n   * Use .toMatchObject to check that a javascript object matches a subset of the properties of an object.\n   */\n  toMatchObject(object: Object | Array<Object>): void;\n  /**\n   * Use .toStrictEqual to check that a javascript object matches a subset of the properties of an object.\n   */\n  toStrictEqual(value: any): void;\n  /**\n   * This ensures that an Object matches the most recent snapshot.\n   */\n  toMatchSnapshot(propertyMatchers?: any, name?: string): void;\n  /**\n   * This ensures that an Object matches the most recent snapshot.\n   */\n  toMatchSnapshot(name: string): void;\n\n  toMatchInlineSnapshot(snapshot?: string): void;\n  toMatchInlineSnapshot(propertyMatchers?: any, snapshot?: string): void;\n  /**\n   * Use .toThrow to test that a function throws when it is called.\n   * If you want to test that a specific error gets thrown, you can provide an\n   * argument to toThrow. The argument can be a string for the error message,\n   * a class for the error, or a regex that should match the error.\n   *\n   * Alias: .toThrowError\n   */\n  toThrow(message?: string | Error | Class<Error> | RegExp): void;\n  toThrowError(message?: string | Error | Class<Error> | RegExp): void;\n  /**\n   * Use .toThrowErrorMatchingSnapshot to test that a function throws a error\n   * matching the most recent snapshot when it is called.\n   */\n  toThrowErrorMatchingSnapshot(): void;\n  toThrowErrorMatchingInlineSnapshot(snapshot?: string): void;\n}\n\ntype JestObjectType = {\n /**\n  *  Disables automatic mocking in the module loader.\n  *\n  *  After this method is called, all `require()`s will return the real\n  *  versions of each module (rather than a mocked version).\n  */\n disableAutomock(): JestObjectType,\n /**\n  * An un-hoisted version of disableAutomock\n  */\n autoMockOff(): JestObjectType,\n /**\n  * Enables automatic mocking in the module loader.\n  */\n enableAutomock(): JestObjectType,\n /**\n  * An un-hoisted version of enableAutomock\n  */\n autoMockOn(): JestObjectType,\n /**\n  * Clears the mock.calls and mock.instances properties of all mocks.\n  * Equivalent to calling .mockClear() on every mocked function.\n  */\n clearAllMocks(): JestObjectType,\n /**\n  * Resets the state of all mocks. Equivalent to calling .mockReset() on every\n  * mocked function.\n  */\n resetAllMocks(): JestObjectType,\n /**\n  * Restores all mocks back to their original value.\n  */\n restoreAllMocks(): JestObjectType,\n /**\n  * Removes any pending timers from the timer system.\n  */\n clearAllTimers(): void,\n /**\n  * Returns the number of fake timers still left to run.\n  */\n getTimerCount(): number,\n /**\n  * The same as `mock` but not moved to the top of the expectation by\n  * babel-jest.\n  */\n doMock(moduleName: string, moduleFactory?: any): JestObjectType,\n /**\n  * The same as `unmock` but not moved to the top of the expectation by\n  * babel-jest.\n  */\n dontMock(moduleName: string): JestObjectType,\n /**\n  * Returns a new, unused mock function. Optionally takes a mock\n  * implementation.\n  */\n fn<TArguments: $ReadOnlyArray<*>, TReturn>(\n   implementation?: (...args: TArguments) => TReturn\n ): JestMockFn<TArguments, TReturn>,\n /**\n  * Determines if the given function is a mocked function.\n  */\n isMockFunction(fn: Function): boolean,\n /**\n  * Given the name of a module, use the automatic mocking system to generate a\n  * mocked version of the module for you.\n  */\n genMockFromModule(moduleName: string): any,\n /**\n  * Mocks a module with an auto-mocked version when it is being required.\n  *\n  * The second argument can be used to specify an explicit module factory that\n  * is being run instead of using Jest's automocking feature.\n  *\n  * The third argument can be used to create virtual mocks -- mocks of modules\n  * that don't exist anywhere in the system.\n  */\n mock(\n   moduleName: string,\n   moduleFactory?: any,\n   options?: Object\n ): JestObjectType,\n /**\n  * Returns the actual module instead of a mock, bypassing all checks on\n  * whether the module should receive a mock implementation or not.\n  */\n requireActual(moduleName: string): any,\n /**\n  * Returns a mock module instead of the actual module, bypassing all checks\n  * on whether the module should be required normally or not.\n  */\n requireMock(moduleName: string): any,\n /**\n  * Resets the module registry - the cache of all required modules. This is\n  * useful to isolate modules where local state might conflict between tests.\n  */\n resetModules(): JestObjectType,\n /**\n  * Creates a sandbox registry for the modules that are loaded inside the\n  * callback function. This is useful to isolate specific modules for every\n  * test so that local module state doesn't conflict between tests.\n  */\n isolateModules(fn: () => void): JestObjectType,\n /**\n  * Exhausts the micro-task queue (usually interfaced in node via\n  * process.nextTick).\n  */\n runAllTicks(): void,\n /**\n  * Exhausts the macro-task queue (i.e., all tasks queued by setTimeout(),\n  * setInterval(), and setImmediate()).\n  */\n runAllTimers(): void,\n /**\n  * Exhausts all tasks queued by setImmediate().\n  */\n runAllImmediates(): void,\n /**\n  * Executes only the macro task queue (i.e. all tasks queued by setTimeout()\n  * or setInterval() and setImmediate()).\n  */\n advanceTimersByTime(msToRun: number): void,\n /**\n  * Executes only the macro task queue (i.e. all tasks queued by setTimeout()\n  * or setInterval() and setImmediate()).\n  *\n  * Renamed to `advanceTimersByTime`.\n  */\n runTimersToTime(msToRun: number): void,\n /**\n  * Executes only the macro-tasks that are currently pending (i.e., only the\n  * tasks that have been queued by setTimeout() or setInterval() up to this\n  * point)\n  */\n runOnlyPendingTimers(): void,\n /**\n  * Explicitly supplies the mock object that the module system should return\n  * for the specified module. Note: It is recommended to use jest.mock()\n  * instead.\n  */\n setMock(moduleName: string, moduleExports: any): JestObjectType,\n /**\n  * Indicates that the module system should never return a mocked version of\n  * the specified module from require() (e.g. that it should always return the\n  * real module).\n  */\n unmock(moduleName: string): JestObjectType,\n /**\n  * Instructs Jest to use fake versions of the standard timer functions\n  * (setTimeout, setInterval, clearTimeout, clearInterval, nextTick,\n  * setImmediate and clearImmediate).\n  */\n useFakeTimers(): JestObjectType,\n /**\n  * Instructs Jest to use the real versions of the standard timer functions.\n  */\n useRealTimers(): JestObjectType,\n /**\n  * Creates a mock function similar to jest.fn but also tracks calls to\n  * object[methodName].\n  */\n spyOn(\n   object: Object,\n   methodName: string,\n   accessType?: 'get' | 'set'\n ): JestMockFn<any, any>,\n /**\n  * Set the default timeout interval for tests and before/after hooks in milliseconds.\n  * Note: The default timeout interval is 5 seconds if this method is not called.\n  */\n setTimeout(timeout: number): JestObjectType,\n ...\n};\n\ntype JestSpyType = { calls: JestCallsType, ... };\n\ntype JestDoneFn = {|\n (): void,\n fail: (error: Error) => void,\n|};\n\n/** Runs this function after every test inside this context */\ndeclare function afterEach(\n  fn: (done: JestDoneFn) => ?Promise<mixed>,\n  timeout?: number\n): void;\n/** Runs this function before every test inside this context */\ndeclare function beforeEach(\n  fn: (done: JestDoneFn) => ?Promise<mixed>,\n  timeout?: number\n): void;\n/** Runs this function after all tests have finished inside this context */\ndeclare function afterAll(\n  fn: (done: JestDoneFn) => ?Promise<mixed>,\n  timeout?: number\n): void;\n/** Runs this function before any tests have started inside this context */\ndeclare function beforeAll(\n  fn: (done: JestDoneFn) => ?Promise<mixed>,\n  timeout?: number\n): void;\n\n/** A context for grouping tests together */\ndeclare var describe: {\n /**\n  * Creates a block that groups together several related tests in one \"test suite\"\n  */\n (name: JestTestName, fn: () => void): void,\n /**\n  * Only run this describe block\n  */\n only(name: JestTestName, fn: () => void): void,\n /**\n  * Skip running this describe block\n  */\n skip(name: JestTestName, fn: () => void): void,\n /**\n  * each runs this test against array of argument arrays per each run\n  *\n  * @param {table} table of Test\n  */\n each(\n   ...table: Array<Array<mixed> | mixed> | [Array<string>, string]\n ): (\n   name: JestTestName,\n   fn?: (...args: Array<any>) => ?Promise<mixed>,\n   timeout?: number\n ) => void,\n ...\n};\n\n/** An individual test unit */\ndeclare var it: {\n /**\n  * An individual test unit\n  *\n  * @param {JestTestName} Name of Test\n  * @param {Function} Test\n  * @param {number} Timeout for the test, in milliseconds.\n  */\n (\n   name: JestTestName,\n   fn?: (done: JestDoneFn) => ?Promise<mixed>,\n   timeout?: number\n ): void,\n /**\n  * Only run this test\n  *\n  * @param {JestTestName} Name of Test\n  * @param {Function} Test\n  * @param {number} Timeout for the test, in milliseconds.\n  */\n only: {|\n   (\n     name: JestTestName,\n     fn?: (done: JestDoneFn) => ?Promise<mixed>,\n     timeout?: number\n   ): void,\n   each(\n     ...table: Array<Array<mixed> | mixed> | [Array<string>, string]\n   ): (\n     name: JestTestName,\n     fn?: (...args: Array<any>) => ?Promise<mixed>,\n     timeout?: number\n   ) => void\n |},\n /**\n  * Skip running this test\n  *\n  * @param {JestTestName} Name of Test\n  * @param {Function} Test\n  * @param {number} Timeout for the test, in milliseconds.\n  */\n skip(\n   name: JestTestName,\n   fn?: (done: JestDoneFn) => ?Promise<mixed>,\n   timeout?: number\n ): void,\n /**\n  * Highlight planned tests in the summary output\n  *\n  * @param {String} Name of Test to do\n  */\n todo(name: string): void,\n /**\n  * Run the test concurrently\n  *\n  * @param {JestTestName} Name of Test\n  * @param {Function} Test\n  * @param {number} Timeout for the test, in milliseconds.\n  */\n concurrent(\n   name: JestTestName,\n   fn?: (done: JestDoneFn) => ?Promise<mixed>,\n   timeout?: number\n ): void,\n /**\n  * each runs this test against array of argument arrays per each run\n  *\n  * @param {table} table of Test\n  */\n each(\n   ...table: Array<Array<mixed> | mixed> | [Array<string>, string]\n ): (\n   name: JestTestName,\n   fn?: (...args: Array<any>) => ?Promise<mixed>,\n   timeout?: number\n ) => void,\n ...\n};\n\ndeclare function fit(\n  name: JestTestName,\n  fn: (done: JestDoneFn) => ?Promise<mixed>,\n  timeout?: number\n): void;\n/** An individual test unit */\ndeclare var test: typeof it;\n/** A disabled group of tests */\ndeclare var xdescribe: typeof describe;\n/** A focused group of tests */\ndeclare var fdescribe: typeof describe;\n/** A disabled individual test */\ndeclare var xit: typeof it;\n/** A disabled individual test */\ndeclare var xtest: typeof it;\n\ntype JestPrettyFormatColors = {\n comment: {\n  close: string,\n  open: string,\n  ...\n },\n content: {\n  close: string,\n  open: string,\n  ...\n },\n prop: {\n  close: string,\n  open: string,\n  ...\n },\n tag: {\n  close: string,\n  open: string,\n  ...\n },\n value: {\n  close: string,\n  open: string,\n  ...\n },\n ...\n};\n\ntype JestPrettyFormatIndent = string => string;\ntype JestPrettyFormatRefs = Array<any>;\ntype JestPrettyFormatPrint = any => string;\ntype JestPrettyFormatStringOrNull = string | null;\n\ntype JestPrettyFormatOptions = {|\n  callToJSON: boolean,\n  edgeSpacing: string,\n  escapeRegex: boolean,\n  highlight: boolean,\n  indent: number,\n  maxDepth: number,\n  min: boolean,\n  plugins: JestPrettyFormatPlugins,\n  printFunctionName: boolean,\n  spacing: string,\n  theme: {|\n    comment: string,\n    content: string,\n    prop: string,\n    tag: string,\n    value: string,\n  |},\n|};\n\ntype JestPrettyFormatPlugin = {\n print: (\n   val: any,\n   serialize: JestPrettyFormatPrint,\n   indent: JestPrettyFormatIndent,\n   opts: JestPrettyFormatOptions,\n   colors: JestPrettyFormatColors\n ) => string,\n test: any => boolean,\n ...\n};\n\ntype JestPrettyFormatPlugins = Array<JestPrettyFormatPlugin>;\n\n/** The expect function is used every time you want to test a value */\ndeclare var expect: {\n /** The object that you want to make assertions against */\n (\n   value: any\n ): JestExpectType &\n   JestPromiseType &\n   EnzymeMatchersType &\n   DomTestingLibraryType &\n   JestJQueryMatchersType &\n   JestStyledComponentsMatchersType &\n   JestExtendedMatchersType,\n /** Add additional Jasmine matchers to Jest's roster */\n extend(matchers: { [name: string]: JestMatcher, ... }): void,\n /** Add a module that formats application-specific data structures. */\n addSnapshotSerializer(pluginModule: JestPrettyFormatPlugin): void,\n assertions(expectedAssertions: number): void,\n hasAssertions(): void,\n any(value: mixed): JestAsymmetricEqualityType,\n anything(): any,\n arrayContaining(value: Array<mixed>): Array<mixed>,\n objectContaining(value: Object): Object,\n /** Matches any received string that contains the exact expected string. */\n stringContaining(value: string): string,\n stringMatching(value: string | RegExp): string,\n not: {\n  arrayContaining: (value: $ReadOnlyArray<mixed>) => Array<mixed>,\n  objectContaining: (value: {...}) => Object,\n  stringContaining: (value: string) => string,\n  stringMatching: (value: string | RegExp) => string,\n  ...\n },\n ...\n};\n\n// TODO handle return type\n// http://jasmine.github.io/2.4/introduction.html#section-Spies\ndeclare function spyOn(value: mixed, method: string): Object;\n\n/** Holds all functions related to manipulating test runner */\ndeclare var jest: JestObjectType;\n\n/**\n * The global Jasmine object, this is generally not exposed as the public API,\n * using features inside here could break in later versions of Jest.\n */\ndeclare var jasmine: {\n DEFAULT_TIMEOUT_INTERVAL: number,\n any(value: mixed): JestAsymmetricEqualityType,\n anything(): any,\n arrayContaining(value: Array<mixed>): Array<mixed>,\n clock(): JestClockType,\n createSpy(name: string): JestSpyType,\n createSpyObj(\n   baseName: string,\n   methodNames: Array<string>\n ): { [methodName: string]: JestSpyType, ... },\n objectContaining(value: Object): Object,\n stringMatching(value: string): string,\n ...\n};\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"dataloader\",\n  \"version\": \"2.2.3\",\n  \"description\": \"A data loading utility to reduce requests to a backend via batching and caching.\",\n  \"contributors\": [\n    \"Lee Byron <lee@leebyron.com> (http://leebyron.com/)\",\n    \"Daniel Schafer <dschafer@fb.com>\",\n    \"Nicholas Schrock <schrockn@fb.com>\"\n  ],\n  \"license\": \"MIT\",\n  \"homepage\": \"https://github.com/graphql/dataloader\",\n  \"bugs\": {\n    \"url\": \"https://github.com/graphql/dataloader/issues\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"http://github.com/graphql/dataloader.git\"\n  },\n  \"main\": \"index.js\",\n  \"typings\": \"index.d.ts\",\n  \"scripts\": {\n    \"test\": \"npm run lint && npm run check && npm run testonly\",\n    \"test:ci\": \"npm run lint && npm run check && npm run testonly -- --coverage\",\n    \"lint\": \"eslint .\",\n    \"check\": \"flow check --max-warnings 0\",\n    \"build\": \"babel src --ignore src/__tests__ --out-dir dist/ ; cp src/index.js dist/index.js.flow ; cp src/index.d.ts dist/\",\n    \"watch\": \"babel resources/watch.js | node\",\n    \"testonly\": \"jest src\",\n    \"prerelease\": \". ./resources/prepublish.sh\",\n    \"release\": \"changeset publish\"\n  },\n  \"files\": [\n    \"index.js\",\n    \"index.js.flow\",\n    \"index.d.ts\",\n    \"README.md\",\n    \"LICENSE\",\n    \"PATENTS\"\n  ],\n  \"devDependencies\": {\n    \"@babel/cli\": \"7.7.0\",\n    \"@babel/core\": \"7.7.2\",\n    \"@babel/node\": \"7.7.0\",\n    \"@babel/preset-env\": \"7.7.1\",\n    \"@babel/preset-flow\": \"7.0.0\",\n    \"@changesets/changelog-github\": \"0.4.6\",\n    \"@changesets/cli\": \"2.24.3\",\n    \"babel-eslint\": \"10.0.3\",\n    \"eslint\": \"6.6.0\",\n    \"eslint-plugin-prettier\": \"^3.4.1\",\n    \"flow-bin\": \"0.112.0\",\n    \"jest\": \"24.9.0\",\n    \"prettier\": \"^2.8.3\",\n    \"sane\": \"4.1.0\"\n  },\n  \"publishConfig\": {\n    \"access\": \"public\"\n  },\n  \"prettier\": {\n    \"arrowParens\": \"avoid\",\n    \"singleQuote\": true,\n    \"trailingComma\": \"all\",\n    \"overrides\": [\n      {\n        \"files\": \"src/**/*.js\",\n        \"options\": {\n          \"parser\": \"babel-flow\"\n        }\n      }\n    ]\n  }\n}\n"
  },
  {
    "path": "renovate.json",
    "content": "{\n  \"$schema\": \"https://docs.renovatebot.com/renovate-schema.json\",\n  \"extends\": [\n    \"github>the-guild-org/shared-config:renovate\",\n    \":preserveSemverRanges\"\n  ],\n  \"packageRules\": []\n}\n"
  },
  {
    "path": "resources/prepublish.sh",
    "content": "# Remove existing build artifacts\nrm -rf dist;\n\n# Build before publishing\nnpm run build;\n\n# When Travis CI publishes to NPM, the published files are available in the root\n# directory, which produces a cleaner distribution.\n#\ncp dist/* .\n\n# Ensure a vanilla package.json before deploying so other tools do not interpret\n# The built output as requiring any further transformation.\nnode -e \"var package = require('./package.json'); \\\n  delete package.scripts; \\\n  delete package.devDependencies; \\\n  delete package.publishConfig; \\\n  require('fs').writeFileSync('dist/package.json', JSON.stringify(package, null, 2));\"\n"
  },
  {
    "path": "resources/watch.js",
    "content": "/* eslint-disable no-console */\nimport sane from 'sane';\nimport { resolve as resolvePath } from 'path';\nimport { spawn } from 'child_process';\nimport flowBinPath from 'flow-bin';\n\nprocess.env.PATH += ':./node_modules/.bin';\n\nvar cmd = resolvePath(__dirname);\nvar srcDir = resolvePath(cmd, './src');\n\nfunction exec(command, options) {\n  return new Promise((resolve, reject) => {\n    var child = spawn(command, options, {\n      cmd,\n      env: process.env,\n      stdio: 'inherit',\n    });\n    child.on('exit', code => {\n      if (code === 0) {\n        resolve(true);\n      } else {\n        reject(new Error('Error code: ' + code));\n      }\n    });\n  });\n}\n\nvar flowServer = spawn(flowBinPath, ['server'], {\n  cmd,\n  env: process.env,\n});\n\nvar watcher = sane(srcDir, { glob: ['**/*.js'] })\n  .on('ready', startWatch)\n  .on('add', changeFile)\n  .on('delete', deleteFile)\n  .on('change', changeFile);\n\nprocess.on('SIGINT', () => {\n  watcher.close();\n  flowServer.kill();\n  console.log(CLEARLINE + yellow(invert('stopped watching')));\n  process.exit();\n});\n\nvar isChecking;\nvar needsCheck;\nvar toCheck = {};\nvar timeout;\n\nfunction startWatch() {\n  process.stdout.write(CLEARSCREEN + green(invert('watching...')));\n}\n\nfunction changeFile(filepath, root, stat) {\n  if (!stat.isDirectory()) {\n    toCheck[filepath] = true;\n    debouncedCheck();\n  }\n}\n\nfunction deleteFile(filepath) {\n  delete toCheck[filepath];\n  debouncedCheck();\n}\n\nfunction debouncedCheck() {\n  needsCheck = true;\n  clearTimeout(timeout);\n  timeout = setTimeout(guardedCheck, 250);\n}\n\nfunction guardedCheck() {\n  if (isChecking || !needsCheck) {\n    return;\n  }\n  isChecking = true;\n  var filepaths = Object.keys(toCheck);\n  toCheck = {};\n  needsCheck = false;\n  checkFiles(filepaths).then(() => {\n    isChecking = false;\n    process.nextTick(guardedCheck);\n  });\n}\n\nfunction checkFiles(filepaths) {\n  console.log('\\u001b[2J');\n\n  return parseFiles(filepaths)\n    .then(() => runTests(filepaths))\n    .then(testSuccess =>\n      lintFiles(filepaths).then(lintSuccess =>\n        typecheckStatus().then(\n          typecheckSuccess => testSuccess && lintSuccess && typecheckSuccess,\n        ),\n      ),\n    )\n    .catch(() => false)\n    .then(success => {\n      process.stdout.write(\n        '\\n' + (success ? '' : '\\x07') + green(invert('watching...')),\n      );\n    });\n}\n\n// Checking steps\n\nfunction parseFiles(filepaths) {\n  console.log('Checking Syntax');\n\n  return Promise.all(\n    filepaths.map(filepath => {\n      if (isJS(filepath) && !isTest(filepath)) {\n        return exec('babel', [\n          '--optional',\n          'runtime',\n          '--out-file',\n          '/dev/null',\n          srcPath(filepath),\n        ]);\n      }\n    }),\n  );\n}\n\nfunction runTests(filepaths) {\n  console.log('\\nRunning Tests');\n\n  return exec(\n    'jest',\n    allTests(filepaths) ? filepaths.map(srcPath) : ['src'],\n  ).catch(() => false);\n}\n\nfunction lintFiles(filepaths) {\n  console.log('Linting Code\\n');\n\n  return filepaths.reduce(\n    (prev, filepath) =>\n      prev.then(prevSuccess => {\n        if (isJS(filepath)) {\n          process.stdout.write('  ' + filepath + ' ...');\n          return exec('eslint', [srcPath(filepath)])\n            .catch(() => false)\n            .then(success => {\n              console.log(\n                CLEARLINE + '  ' + (success ? CHECK : X) + ' ' + filepath,\n              );\n              return prevSuccess && success;\n            });\n        }\n        return prevSuccess;\n      }),\n    Promise.resolve(true),\n  );\n}\n\nfunction typecheckStatus() {\n  console.log('\\nType Checking\\n');\n  return exec(flowBinPath, ['status']).catch(() => false);\n}\n\n// Filepath\n\nfunction srcPath(filepath) {\n  return resolvePath(srcDir, filepath);\n}\n\n// Predicates\n\nfunction isJS(filepath) {\n  return filepath.indexOf('.js') === filepath.length - 3;\n}\n\nfunction allTests(filepaths) {\n  return filepaths.length > 0 && filepaths.every(isTest);\n}\n\nfunction isTest(filepath) {\n  return isJS(filepath) && filepath.indexOf('__tests__/') !== -1;\n}\n\n// Print helpers\n\nvar CLEARSCREEN = '\\u001b[2J';\nvar CLEARLINE = '\\r\\x1B[K';\nvar CHECK = green('\\u2713');\nvar X = red('\\u2718');\n\nfunction invert(str) {\n  return `\\u001b[7m ${str} \\u001b[27m`;\n}\n\nfunction red(str) {\n  return `\\x1B[K\\u001b[1m\\u001b[31m${str}\\u001b[39m\\u001b[22m`;\n}\n\nfunction green(str) {\n  return `\\x1B[K\\u001b[1m\\u001b[32m${str}\\u001b[39m\\u001b[22m`;\n}\n\nfunction yellow(str) {\n  return `\\x1B[K\\u001b[1m\\u001b[33m${str}\\u001b[39m\\u001b[22m`;\n}\n"
  },
  {
    "path": "src/__tests__/abuse.test.js",
    "content": "/**\n * Copyright (c) 2019-present, GraphQL Foundation\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n * @flow\n */\n\nconst DataLoader = require('..');\n\ndescribe('Provides descriptive error messages for API abuse', () => {\n  it('Loader creation requires a function', () => {\n    expect(() => {\n      // $FlowExpectError\n      new DataLoader(); // eslint-disable-line no-new\n    }).toThrow(\n      'DataLoader must be constructed with a function which accepts ' +\n        'Array<key> and returns Promise<Array<value>>, but got: undefined.',\n    );\n\n    expect(() => {\n      // $FlowExpectError\n      new DataLoader({}); // eslint-disable-line no-new\n    }).toThrow(\n      'DataLoader must be constructed with a function which accepts ' +\n        'Array<key> and returns Promise<Array<value>>, but got: [object Object].',\n    );\n  });\n\n  it('Load function requires an key', () => {\n    const idLoader = new DataLoader<number, number>(async keys => keys);\n\n    expect(() => {\n      // $FlowExpectError\n      idLoader.load();\n    }).toThrow(\n      'The loader.load() function must be called with a value, ' +\n        'but got: undefined.',\n    );\n\n    expect(() => {\n      // $FlowExpectError\n      idLoader.load(null);\n    }).toThrow(\n      'The loader.load() function must be called with a value, ' +\n        'but got: null.',\n    );\n\n    // Falsey values like the number 0 is acceptable\n    expect(() => {\n      idLoader.load(0);\n    }).not.toThrow();\n  });\n\n  it('LoadMany function requires a list of key', () => {\n    const idLoader = new DataLoader<number, number>(async keys => keys);\n\n    expect(() => {\n      // $FlowExpectError\n      idLoader.loadMany();\n    }).toThrow(\n      'The loader.loadMany() function must be called with Array<key> ' +\n        'but got: undefined.',\n    );\n\n    expect(() => {\n      // $FlowExpectError\n      idLoader.loadMany(1, 2, 3);\n    }).toThrow(\n      'The loader.loadMany() function must be called with Array<key> ' +\n        'but got: 1.',\n    );\n\n    // Empty array is acceptable\n    expect(() => {\n      idLoader.loadMany([]);\n    }).not.toThrow();\n  });\n\n  it('Batch function must return a Promise, not null', async () => {\n    // $FlowExpectError\n    const badLoader = new DataLoader<number, number>(() => null);\n\n    let caughtError;\n    try {\n      await badLoader.load(1);\n    } catch (error) {\n      caughtError = error;\n    }\n    expect(caughtError).toBeInstanceOf(Error);\n    expect((caughtError: any).message).toBe(\n      'DataLoader must be constructed with a function which accepts ' +\n        'Array<key> and returns Promise<Array<value>>, but the function did ' +\n        'not return a Promise: null.',\n    );\n  });\n\n  it('Batch function must return a Promise, not error synchronously', async () => {\n    const badLoader = new DataLoader<number, number>(() => {\n      throw new Error('Mock Synchronous Error');\n    });\n\n    let caughtError;\n    try {\n      await badLoader.load(1);\n    } catch (error) {\n      caughtError = error;\n    }\n    expect(caughtError).toBeInstanceOf(Error);\n    expect((caughtError: any).message).toBe(\n      'DataLoader must be constructed with a function which accepts ' +\n        'Array<key> and returns Promise<Array<value>>, but the function ' +\n        'errored synchronously: Error: Mock Synchronous Error.',\n    );\n  });\n\n  it('Batch function must return a Promise, not a value', async () => {\n    // Note: this is returning the keys directly, rather than a promise to keys.\n    // $FlowExpectError\n    const badLoader = new DataLoader<number, number>(keys => keys);\n\n    let caughtError;\n    try {\n      await badLoader.load(1);\n    } catch (error) {\n      caughtError = error;\n    }\n    expect(caughtError).toBeInstanceOf(Error);\n    expect((caughtError: any).message).toBe(\n      'DataLoader must be constructed with a function which accepts ' +\n        'Array<key> and returns Promise<Array<value>>, but the function did ' +\n        'not return a Promise: 1.',\n    );\n  });\n\n  it('Batch function must return a Promise of an Array, not null', async () => {\n    // Note: this resolves to undefined\n    // $FlowExpectError\n    const badLoader = new DataLoader<number, number>(async () => null);\n\n    let caughtError;\n    try {\n      await badLoader.load(1);\n    } catch (error) {\n      caughtError = error;\n    }\n    expect(caughtError).toBeInstanceOf(Error);\n    expect((caughtError: any).message).toBe(\n      'DataLoader must be constructed with a function which accepts ' +\n        'Array<key> and returns Promise<Array<value>>, but the function did ' +\n        'not return a Promise of an Array: null.',\n    );\n  });\n\n  it('Batch function must promise an Array of correct length', async () => {\n    // Note: this resolves to empty array\n    const badLoader = new DataLoader<number, number>(async () => []);\n\n    let caughtError;\n    try {\n      await badLoader.load(1);\n    } catch (error) {\n      caughtError = error;\n    }\n    expect(caughtError).toBeInstanceOf(Error);\n    expect((caughtError: any).message).toBe(\n      'DataLoader must be constructed with a function which accepts ' +\n        'Array<key> and returns Promise<Array<value>>, but the function did ' +\n        'not return a Promise of an Array of the same length as the Array ' +\n        'of keys.' +\n        '\\n\\nKeys:\\n1' +\n        '\\n\\nValues:\\n',\n    );\n  });\n\n  it('Cache should have get, set, delete, and clear methods', async () => {\n    class IncompleteMap {\n      get() {}\n    }\n\n    expect(() => {\n      // $FlowExpectError\n      const incompleteMap = new IncompleteMap();\n      const options = { cacheMap: incompleteMap };\n      new DataLoader(async keys => keys, options); // eslint-disable-line no-new\n    }).toThrow('Custom cacheMap missing methods: set, delete, clear');\n  });\n\n  it('Requires a number for maxBatchSize', () => {\n    expect(\n      () =>\n        // $FlowExpectError\n        new DataLoader(async keys => keys, { maxBatchSize: null }),\n    ).toThrow('maxBatchSize must be a positive number: null');\n  });\n\n  it('Requires a positive number for maxBatchSize', () => {\n    expect(\n      () => new DataLoader(async keys => keys, { maxBatchSize: 0 }),\n    ).toThrow('maxBatchSize must be a positive number: 0');\n  });\n\n  it('Requires a function for cacheKeyFn', () => {\n    expect(\n      () =>\n        // $FlowExpectError\n        new DataLoader(async keys => keys, { cacheKeyFn: null }),\n    ).toThrow('cacheKeyFn must be a function: null');\n  });\n\n  it('Requires a function for batchScheduleFn', () => {\n    expect(\n      () =>\n        // $FlowExpectError\n        new DataLoader(async keys => keys, { batchScheduleFn: null }),\n    ).toThrow('batchScheduleFn must be a function: null');\n  });\n});\n"
  },
  {
    "path": "src/__tests__/browser.test.js",
    "content": "/**\n * Copyright (c) 2019-present, GraphQL Foundation\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n * @flow\n */\n\n// Mock out process.nextTick as not existing for this test before requiring.\nprocess.nextTick = (null: any);\nconst DataLoader = require('..');\n\ndescribe('Browser support', () => {\n  it('batches multiple requests without process.nextTick', async () => {\n    const loadCalls = [];\n    const identityLoader = new DataLoader<number, number>(async keys => {\n      loadCalls.push(keys);\n      return keys;\n    });\n\n    const promise1 = identityLoader.load(1);\n    const promise2 = identityLoader.load(2);\n\n    const [value1, value2] = await Promise.all([promise1, promise2]);\n    expect(value1).toBe(1);\n    expect(value2).toBe(2);\n\n    expect(loadCalls).toEqual([[1, 2]]);\n  });\n});\n"
  },
  {
    "path": "src/__tests__/dataloader.test.js",
    "content": "/**\n * Copyright (c) 2019-present, GraphQL Foundation\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n * @flow\n */\n\nimport type { Options } from '..';\nconst DataLoader = require('..');\n\nfunction idLoader<K, C = K>(\n  options?: Options<K, K, C>,\n): [DataLoader<K, K, C>, Array<$ReadOnlyArray<K>>] {\n  const loadCalls = [];\n  const identityLoader = new DataLoader(keys => {\n    loadCalls.push(keys);\n    return Promise.resolve(keys);\n  }, options);\n  return [identityLoader, loadCalls];\n}\n\ndescribe('Primary API', () => {\n  it('builds a really really simple data loader', async () => {\n    const identityLoader = new DataLoader<number, number>(async keys => keys);\n\n    const promise1 = identityLoader.load(1);\n    expect(promise1).toBeInstanceOf(Promise);\n\n    const value1 = await promise1;\n    expect(value1).toBe(1);\n  });\n\n  it('references the loader as \"this\" in the batch function', async () => {\n    let that;\n    const loader = new DataLoader<number, number>(async function (keys) {\n      that = this;\n      return keys;\n    });\n\n    // Trigger the batch function\n    await loader.load(1);\n\n    expect(that).toBe(loader);\n  });\n\n  it('references the loader as \"this\" in the cache key function', async () => {\n    let that;\n    const loader = new DataLoader<number, number>(async keys => keys, {\n      cacheKeyFn(key) {\n        that = this;\n        return key;\n      },\n    });\n\n    // Trigger the cache key function\n    await loader.load(1);\n\n    expect(that).toBe(loader);\n  });\n\n  it('supports loading multiple keys in one call', async () => {\n    const identityLoader = new DataLoader<number, number>(async keys => keys);\n\n    const promiseAll = identityLoader.loadMany([1, 2]);\n    expect(promiseAll).toBeInstanceOf(Promise);\n\n    const values = await promiseAll;\n    expect(values).toEqual([1, 2]);\n\n    const promiseEmpty = identityLoader.loadMany([]);\n    expect(promiseEmpty).toBeInstanceOf(Promise);\n\n    const empty = await promiseEmpty;\n    expect(empty).toEqual([]);\n  });\n\n  it('supports loading multiple keys in one call with errors', async () => {\n    const identityLoader = new DataLoader(keys =>\n      Promise.resolve(\n        keys.map(key => (key === 'bad' ? new Error('Bad Key') : key)),\n      ),\n    );\n\n    const promiseAll = identityLoader.loadMany(['a', 'b', 'bad']);\n    expect(promiseAll).toBeInstanceOf(Promise);\n\n    const values = await promiseAll;\n    expect(values).toEqual(['a', 'b', new Error('Bad Key')]);\n  });\n\n  it('batches multiple requests', async () => {\n    const [identityLoader, loadCalls] = idLoader<number>();\n\n    const promise1 = identityLoader.load(1);\n    const promise2 = identityLoader.load(2);\n\n    const [value1, value2] = await Promise.all([promise1, promise2]);\n    expect(value1).toBe(1);\n    expect(value2).toBe(2);\n\n    expect(loadCalls).toEqual([[1, 2]]);\n  });\n\n  it('batches multiple requests with max batch sizes', async () => {\n    const [identityLoader, loadCalls] = idLoader<number>({ maxBatchSize: 2 });\n\n    const promise1 = identityLoader.load(1);\n    const promise2 = identityLoader.load(2);\n    const promise3 = identityLoader.load(3);\n\n    const [value1, value2, value3] = await Promise.all([\n      promise1,\n      promise2,\n      promise3,\n    ]);\n    expect(value1).toBe(1);\n    expect(value2).toBe(2);\n    expect(value3).toBe(3);\n\n    expect(loadCalls).toEqual([[1, 2], [3]]);\n  });\n\n  it('applies maxBatchSize correctly with duplicate keys', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>({\n      maxBatchSize: 3,\n      batchScheduleFn: callback => {\n        setTimeout(callback, 100);\n      },\n    });\n\n    const values = ['a', 'b', 'a', 'a', 'a', 'b', 'c'];\n    const results = await Promise.all(\n      values.map(value => identityLoader.load(value)),\n    );\n\n    expect(results).toEqual(values);\n    expect(loadCalls).toEqual([['a', 'b', 'c']]);\n  });\n\n  it('batches cached requests', async () => {\n    const loadCalls = [];\n    let resolveBatch = () => {};\n    const identityLoader = new DataLoader<number, number>(keys => {\n      loadCalls.push(keys);\n      return new Promise(resolve => {\n        resolveBatch = () => resolve(keys);\n      });\n    });\n\n    identityLoader.prime(1, 1);\n\n    const promise1 = identityLoader.load(1);\n    const promise2 = identityLoader.load(2);\n\n    // Track when each resolves.\n    let promise1Resolved = false;\n    let promise2Resolved = false;\n    promise1.then(() => {\n      promise1Resolved = true;\n    });\n    promise2.then(() => {\n      promise2Resolved = true;\n    });\n\n    // Move to next macro-task (tick)\n    await new Promise(setImmediate);\n\n    expect(promise1Resolved).toBe(false);\n    expect(promise2Resolved).toBe(false);\n\n    resolveBatch();\n    // Move to next macro-task (tick)\n    await new Promise(setImmediate);\n\n    expect(promise1Resolved).toBe(true);\n    expect(promise2Resolved).toBe(true);\n\n    const [value1, value2] = await Promise.all([promise1, promise2]);\n    expect(value1).toBe(1);\n    expect(value2).toBe(2);\n\n    expect(loadCalls).toEqual([[2]]);\n  });\n\n  it('max batch size respects cached results', async () => {\n    const loadCalls = [];\n    let resolveBatch = () => {};\n    const identityLoader = new DataLoader<number, number>(\n      keys => {\n        loadCalls.push(keys);\n        return new Promise(resolve => {\n          resolveBatch = () => resolve(keys);\n        });\n      },\n      { maxBatchSize: 1 },\n    );\n\n    identityLoader.prime(1, 1);\n\n    const promise1 = identityLoader.load(1);\n    const promise2 = identityLoader.load(2);\n\n    // Track when each resolves.\n    let promise1Resolved = false;\n    let promise2Resolved = false;\n    promise1.then(() => {\n      promise1Resolved = true;\n    });\n    promise2.then(() => {\n      promise2Resolved = true;\n    });\n\n    // Move to next macro-task (tick)\n    await new Promise(setImmediate);\n\n    // Promise 1 resolves first since max batch size is 1,\n    // but it still hasn't resolved yet.\n    expect(promise1Resolved).toBe(false);\n    expect(promise2Resolved).toBe(false);\n\n    resolveBatch();\n    // Move to next macro-task (tick)\n    await new Promise(setImmediate);\n\n    expect(promise1Resolved).toBe(true);\n    expect(promise2Resolved).toBe(true);\n\n    const [value1, value2] = await Promise.all([promise1, promise2]);\n    expect(value1).toBe(1);\n    expect(value2).toBe(2);\n\n    expect(loadCalls).toEqual([[2]]);\n  });\n\n  it('coalesces identical requests', async () => {\n    const [identityLoader, loadCalls] = idLoader<number>();\n\n    const promise1a = identityLoader.load(1);\n    const promise1b = identityLoader.load(1);\n\n    const [value1a, value1b] = await Promise.all([promise1a, promise1b]);\n    expect(value1a).toBe(1);\n    expect(value1b).toBe(1);\n\n    expect(loadCalls).toEqual([[1]]);\n  });\n\n  it('coalesces identical requests across sized batches', async () => {\n    const [identityLoader, loadCalls] = idLoader<number>({ maxBatchSize: 2 });\n\n    const promise1a = identityLoader.load(1);\n    const promise2 = identityLoader.load(2);\n    const promise1b = identityLoader.load(1);\n    const promise3 = identityLoader.load(3);\n\n    const [value1a, value2, value1b, value3] = await Promise.all([\n      promise1a,\n      promise2,\n      promise1b,\n      promise3,\n    ]);\n    expect(value1a).toBe(1);\n    expect(value2).toBe(2);\n    expect(value1b).toBe(1);\n    expect(value3).toBe(3);\n\n    expect(loadCalls).toEqual([[1, 2], [3]]);\n  });\n\n  it('caches repeated requests', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>();\n\n    const [a, b] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n    ]);\n\n    expect(a).toBe('A');\n    expect(b).toBe('B');\n\n    expect(loadCalls).toEqual([['A', 'B']]);\n\n    const [a2, c] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('C'),\n    ]);\n\n    expect(a2).toBe('A');\n    expect(c).toBe('C');\n\n    expect(loadCalls).toEqual([['A', 'B'], ['C']]);\n\n    const [a3, b2, c2] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n      identityLoader.load('C'),\n    ]);\n\n    expect(a3).toBe('A');\n    expect(b2).toBe('B');\n    expect(c2).toBe('C');\n\n    expect(loadCalls).toEqual([['A', 'B'], ['C']]);\n  });\n\n  it('clears single value in loader', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>();\n\n    const [a, b] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n    ]);\n\n    expect(a).toBe('A');\n    expect(b).toBe('B');\n\n    expect(loadCalls).toEqual([['A', 'B']]);\n\n    identityLoader.clear('A');\n\n    const [a2, b2] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n    ]);\n\n    expect(a2).toBe('A');\n    expect(b2).toBe('B');\n\n    expect(loadCalls).toEqual([['A', 'B'], ['A']]);\n  });\n\n  it('clears all values in loader', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>();\n\n    const [a, b] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n    ]);\n\n    expect(a).toBe('A');\n    expect(b).toBe('B');\n\n    expect(loadCalls).toEqual([['A', 'B']]);\n\n    identityLoader.clearAll();\n\n    const [a2, b2] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n    ]);\n\n    expect(a2).toBe('A');\n    expect(b2).toBe('B');\n\n    expect(loadCalls).toEqual([\n      ['A', 'B'],\n      ['A', 'B'],\n    ]);\n  });\n\n  it('allows priming the cache', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>();\n\n    identityLoader.prime('A', 'A');\n\n    const [a, b] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n    ]);\n\n    expect(a).toBe('A');\n    expect(b).toBe('B');\n\n    expect(loadCalls).toEqual([['B']]);\n  });\n\n  it('does not prime keys that already exist', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>();\n\n    identityLoader.prime('A', 'X');\n\n    const a1 = await identityLoader.load('A');\n    const b1 = await identityLoader.load('B');\n    expect(a1).toBe('X');\n    expect(b1).toBe('B');\n\n    identityLoader.prime('A', 'Y');\n    identityLoader.prime('B', 'Y');\n\n    const a2 = await identityLoader.load('A');\n    const b2 = await identityLoader.load('B');\n    expect(a2).toBe('X');\n    expect(b2).toBe('B');\n\n    expect(loadCalls).toEqual([['B']]);\n  });\n\n  it('allows forcefully priming the cache', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>();\n\n    identityLoader.prime('A', 'X');\n\n    const a1 = await identityLoader.load('A');\n    const b1 = await identityLoader.load('B');\n    expect(a1).toBe('X');\n    expect(b1).toBe('B');\n\n    identityLoader.clear('A').prime('A', 'Y');\n    identityLoader.clear('B').prime('B', 'Y');\n\n    const a2 = await identityLoader.load('A');\n    const b2 = await identityLoader.load('B');\n    expect(a2).toBe('Y');\n    expect(b2).toBe('Y');\n\n    expect(loadCalls).toEqual([['B']]);\n  });\n\n  it('allows priming the cache with a promise', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>();\n\n    identityLoader.prime('A', Promise.resolve('A'));\n\n    const [a, b] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n    ]);\n\n    expect(a).toBe('A');\n    expect(b).toBe('B');\n\n    expect(loadCalls).toEqual([['B']]);\n  });\n\n  it('allows giving the loader a name', () => {\n    expect(new DataLoader(() => Promise.resolve([])).name).toBeNull();\n    expect(new DataLoader(() => Promise.resolve([]), {}).name).toBeNull();\n\n    expect(\n      new DataLoader(() => Promise.resolve([]), { name: 'Some name' }).name,\n    ).toBe('Some name');\n  });\n});\n\ndescribe('Represents Errors', () => {\n  it('Resolves to error to indicate failure', async () => {\n    const loadCalls = [];\n    const evenLoader = new DataLoader(keys => {\n      loadCalls.push(keys);\n      return Promise.resolve(\n        keys.map(key => (key % 2 === 0 ? key : new Error(`Odd: ${key}`))),\n      );\n    });\n\n    let caughtError;\n    try {\n      await evenLoader.load(1);\n    } catch (error) {\n      caughtError = error;\n    }\n    expect(caughtError).toBeInstanceOf(Error);\n    expect((caughtError: any).message).toBe('Odd: 1');\n\n    const value2 = await evenLoader.load(2);\n    expect(value2).toBe(2);\n\n    expect(loadCalls).toEqual([[1], [2]]);\n  });\n\n  it('Can represent failures and successes simultaneously', async () => {\n    const loadCalls = [];\n    const evenLoader = new DataLoader(keys => {\n      loadCalls.push(keys);\n      return Promise.resolve(\n        keys.map(key => (key % 2 === 0 ? key : new Error(`Odd: ${key}`))),\n      );\n    });\n\n    const promise1 = evenLoader.load(1);\n    const promise2 = evenLoader.load(2);\n\n    let caughtError;\n    try {\n      await promise1;\n    } catch (error) {\n      caughtError = error;\n    }\n    expect(caughtError).toBeInstanceOf(Error);\n    expect((caughtError: any).message).toBe('Odd: 1');\n\n    expect(await promise2).toBe(2);\n\n    expect(loadCalls).toEqual([[1, 2]]);\n  });\n\n  it('Caches failed fetches', async () => {\n    const loadCalls = [];\n    const errorLoader = new DataLoader(keys => {\n      loadCalls.push(keys);\n      return Promise.resolve(keys.map(key => new Error(`Error: ${key}`)));\n    });\n\n    let caughtErrorA;\n    try {\n      await errorLoader.load(1);\n    } catch (error) {\n      caughtErrorA = error;\n    }\n    expect(caughtErrorA).toBeInstanceOf(Error);\n    expect((caughtErrorA: any).message).toBe('Error: 1');\n\n    let caughtErrorB;\n    try {\n      await errorLoader.load(1);\n    } catch (error) {\n      caughtErrorB = error;\n    }\n    expect(caughtErrorB).toBeInstanceOf(Error);\n    expect((caughtErrorB: any).message).toBe('Error: 1');\n\n    expect(loadCalls).toEqual([[1]]);\n  });\n\n  it('Handles priming the cache with an error', async () => {\n    const [identityLoader, loadCalls] = idLoader<number>();\n\n    identityLoader.prime(1, new Error('Error: 1'));\n\n    // Wait a bit.\n    await new Promise(setImmediate);\n\n    let caughtErrorA;\n    try {\n      await identityLoader.load(1);\n    } catch (error) {\n      caughtErrorA = error;\n    }\n    expect(caughtErrorA).toBeInstanceOf(Error);\n    expect((caughtErrorA: any).message).toBe('Error: 1');\n\n    expect(loadCalls).toEqual([]);\n  });\n\n  it('Can clear values from cache after errors', async () => {\n    const loadCalls = [];\n    const errorLoader = new DataLoader(keys => {\n      loadCalls.push(keys);\n      return Promise.resolve(keys.map(key => new Error(`Error: ${key}`)));\n    });\n\n    let caughtErrorA;\n    try {\n      await errorLoader.load(1).catch(error => {\n        // Presumably determine if this error is transient, and only clear the\n        // cache in that case.\n        errorLoader.clear(1);\n        throw error;\n      });\n    } catch (error) {\n      caughtErrorA = error;\n    }\n    expect(caughtErrorA).toBeInstanceOf(Error);\n    expect((caughtErrorA: any).message).toBe('Error: 1');\n\n    let caughtErrorB;\n    try {\n      await errorLoader.load(1).catch(error => {\n        // Again, only do this if you can determine the error is transient.\n        errorLoader.clear(1);\n        throw error;\n      });\n    } catch (error) {\n      caughtErrorB = error;\n    }\n    expect(caughtErrorB).toBeInstanceOf(Error);\n    expect((caughtErrorB: any).message).toBe('Error: 1');\n\n    expect(loadCalls).toEqual([[1], [1]]);\n  });\n\n  it('Propagates error to all loads', async () => {\n    const loadCalls = [];\n    const failLoader = new DataLoader(keys => {\n      loadCalls.push(keys);\n      return Promise.reject(new Error('I am a terrible loader'));\n    });\n\n    const promise1 = failLoader.load(1);\n    const promise2 = failLoader.load(2);\n\n    let caughtErrorA;\n    try {\n      await promise1;\n    } catch (error) {\n      caughtErrorA = error;\n    }\n    expect(caughtErrorA).toBeInstanceOf(Error);\n    expect((caughtErrorA: any).message).toBe('I am a terrible loader');\n\n    let caughtErrorB;\n    try {\n      await promise2;\n    } catch (error) {\n      caughtErrorB = error;\n    }\n    expect(caughtErrorB).toBe(caughtErrorA);\n\n    expect(loadCalls).toEqual([[1, 2]]);\n  });\n});\n\ndescribe('Accepts any kind of key', () => {\n  it('Accepts objects as keys', async () => {\n    const [identityLoader, loadCalls] = idLoader<{}>();\n\n    const keyA = {};\n    const keyB = {};\n\n    // Fetches as expected\n\n    const [valueA, valueB] = await Promise.all([\n      identityLoader.load(keyA),\n      identityLoader.load(keyB),\n    ]);\n\n    expect(valueA).toBe(keyA);\n    expect(valueB).toBe(keyB);\n\n    expect(loadCalls).toHaveLength(1);\n    expect(loadCalls[0]).toHaveLength(2);\n    expect(loadCalls[0][0]).toBe(keyA);\n    expect(loadCalls[0][1]).toBe(keyB);\n\n    // Caching\n\n    identityLoader.clear(keyA);\n\n    const [valueA2, valueB2] = await Promise.all([\n      identityLoader.load(keyA),\n      identityLoader.load(keyB),\n    ]);\n\n    expect(valueA2).toBe(keyA);\n    expect(valueB2).toBe(keyB);\n\n    expect(loadCalls).toHaveLength(2);\n    expect(loadCalls[1]).toHaveLength(1);\n    expect(loadCalls[1][0]).toBe(keyA);\n  });\n});\n\ndescribe('Accepts options', () => {\n  // Note: mirrors 'batches multiple requests' above.\n  it('May disable batching', async () => {\n    const [identityLoader, loadCalls] = idLoader<number>({ batch: false });\n\n    const promise1 = identityLoader.load(1);\n    const promise2 = identityLoader.load(2);\n\n    const [value1, value2] = await Promise.all([promise1, promise2]);\n    expect(value1).toBe(1);\n    expect(value2).toBe(2);\n\n    expect(loadCalls).toEqual([[1], [2]]);\n  });\n\n  // Note: mirror's 'caches repeated requests' above.\n  it('May disable caching', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>({ cache: false });\n\n    const [a, b] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n    ]);\n\n    expect(a).toBe('A');\n    expect(b).toBe('B');\n\n    expect(loadCalls).toEqual([['A', 'B']]);\n\n    const [a2, c] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('C'),\n    ]);\n\n    expect(a2).toBe('A');\n    expect(c).toBe('C');\n\n    expect(loadCalls).toEqual([\n      ['A', 'B'],\n      ['A', 'C'],\n    ]);\n\n    const [a3, b2, c2] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n      identityLoader.load('C'),\n    ]);\n\n    expect(a3).toBe('A');\n    expect(b2).toBe('B');\n    expect(c2).toBe('C');\n\n    expect(loadCalls).toEqual([\n      ['A', 'B'],\n      ['A', 'C'],\n      ['A', 'B', 'C'],\n    ]);\n  });\n\n  it('Keys are repeated in batch when cache disabled', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>({ cache: false });\n\n    const [values1, values2, values3, values4] = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('C'),\n      identityLoader.load('D'),\n      identityLoader.loadMany(['C', 'D', 'A', 'A', 'B']),\n    ]);\n\n    expect(values1).toBe('A');\n    expect(values2).toBe('C');\n    expect(values3).toBe('D');\n    expect(values4).toEqual(['C', 'D', 'A', 'A', 'B']);\n\n    expect(loadCalls).toEqual([['A', 'C', 'D', 'C', 'D', 'A', 'A', 'B']]);\n  });\n\n  it('cacheMap may be set to null to disable cache', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>({ cacheMap: null });\n\n    await identityLoader.load('A');\n    await identityLoader.load('A');\n\n    expect(loadCalls).toEqual([['A'], ['A']]);\n  });\n\n  it('Does not interact with a cache when cache is disabled', () => {\n    const promiseX = Promise.resolve('X');\n    const cacheMap = new Map([['X', promiseX]]);\n    const [identityLoader] = idLoader<string>({ cache: false, cacheMap });\n\n    identityLoader.prime('A', 'A');\n    expect(cacheMap.get('A')).toBe(undefined);\n    identityLoader.clear('X');\n    expect(cacheMap.get('X')).toBe(promiseX);\n    identityLoader.clearAll();\n    expect(cacheMap.get('X')).toBe(promiseX);\n  });\n\n  it('Does not call cacheKeyFn when cache is disabled', async () => {\n    const cacheKeyFnCalls = [];\n    const [identityLoader] = idLoader<string>({\n      cache: false,\n      cacheKeyFn: key => {\n        cacheKeyFnCalls.push(key);\n        return key;\n      },\n    });\n\n    await identityLoader.load('A');\n    expect(cacheKeyFnCalls).toEqual([]);\n  });\n\n  it('Complex cache behavior via clearAll()', async () => {\n    // This loader clears its cache as soon as a batch function is dispatched.\n    const loadCalls = [];\n    const identityLoader = new DataLoader<string, string>(keys => {\n      identityLoader.clearAll();\n      loadCalls.push(keys);\n      return Promise.resolve(keys);\n    });\n\n    const values1 = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n      identityLoader.load('A'),\n    ]);\n\n    expect(values1).toEqual(['A', 'B', 'A']);\n\n    const values2 = await Promise.all([\n      identityLoader.load('A'),\n      identityLoader.load('B'),\n      identityLoader.load('A'),\n    ]);\n\n    expect(values2).toEqual(['A', 'B', 'A']);\n\n    expect(loadCalls).toEqual([\n      ['A', 'B'],\n      ['A', 'B'],\n    ]);\n  });\n\n  describe('Accepts object key in custom cacheKey function', () => {\n    function cacheKey(key: { [string]: any }): string {\n      return Object.keys(key)\n        .sort()\n        .map(k => k + ':' + key[k])\n        .join();\n    }\n\n    type Obj = { [string]: number };\n\n    it('Accepts objects with a complex key', async () => {\n      const identityLoadCalls = [];\n      const identityLoader = new DataLoader<Obj, Obj, string>(\n        keys => {\n          identityLoadCalls.push(keys);\n          return Promise.resolve(keys);\n        },\n        { cacheKeyFn: cacheKey },\n      );\n\n      const key1 = { id: 123 };\n      const key2 = { id: 123 };\n\n      const value1 = await identityLoader.load(key1);\n      const value2 = await identityLoader.load(key2);\n\n      expect(identityLoadCalls).toEqual([[key1]]);\n      expect(value1).toBe(key1);\n      expect(value2).toBe(key1);\n    });\n\n    it('Clears objects with complex key', async () => {\n      const identityLoadCalls = [];\n      const identityLoader = new DataLoader<Obj, Obj, string>(\n        keys => {\n          identityLoadCalls.push(keys);\n          return Promise.resolve(keys);\n        },\n        { cacheKeyFn: cacheKey },\n      );\n\n      const key1 = { id: 123 };\n      const key2 = { id: 123 };\n\n      const value1 = await identityLoader.load(key1);\n      identityLoader.clear(key2); // clear equivalent object key\n      const value2 = await identityLoader.load(key1);\n\n      expect(identityLoadCalls).toEqual([[key1], [key1]]);\n      expect(value1).toBe(key1);\n      expect(value2).toBe(key1);\n    });\n\n    it('Accepts objects with different order of keys', async () => {\n      const identityLoadCalls = [];\n      const identityLoader = new DataLoader<Obj, Obj, string>(\n        keys => {\n          identityLoadCalls.push(keys);\n          return Promise.resolve(keys);\n        },\n        { cacheKeyFn: cacheKey },\n      );\n\n      // Fetches as expected\n\n      const keyA = { a: 123, b: 321 };\n      const keyB = { b: 321, a: 123 };\n\n      const [valueA, valueB] = await Promise.all([\n        identityLoader.load(keyA),\n        identityLoader.load(keyB),\n      ]);\n\n      expect(valueA).toBe(keyA);\n      expect(valueB).toBe(valueA);\n\n      expect(identityLoadCalls).toHaveLength(1);\n      expect(identityLoadCalls[0]).toHaveLength(1);\n      expect(identityLoadCalls[0][0]).toBe(keyA);\n    });\n\n    it('Allows priming the cache with an object key', async () => {\n      const [identityLoader, loadCalls] = idLoader<Obj, string>({\n        cacheKeyFn: cacheKey,\n      });\n\n      const key1 = { id: 123 };\n      const key2 = { id: 123 };\n\n      identityLoader.prime(key1, key1);\n\n      const value1 = await identityLoader.load(key1);\n      const value2 = await identityLoader.load(key2);\n\n      expect(loadCalls).toEqual([]);\n      expect(value1).toBe(key1);\n      expect(value2).toBe(key1);\n    });\n  });\n\n  describe('Accepts custom cacheMap instance', () => {\n    class SimpleMap {\n      stash: Object;\n\n      constructor() {\n        this.stash = {};\n      }\n      get(key) {\n        return this.stash[key];\n      }\n      set(key, value) {\n        this.stash[key] = value;\n      }\n      delete(key) {\n        delete this.stash[key];\n      }\n      clear() {\n        this.stash = {};\n      }\n    }\n\n    it('Accepts a custom cache map implementation', async () => {\n      const aCustomMap = new SimpleMap();\n      const identityLoadCalls = [];\n      const identityLoader = new DataLoader<string, string>(\n        keys => {\n          identityLoadCalls.push(keys);\n          return Promise.resolve(keys);\n        },\n        { cacheMap: aCustomMap },\n      );\n\n      // Fetches as expected\n\n      const [valueA, valueB1] = await Promise.all([\n        identityLoader.load('a'),\n        identityLoader.load('b'),\n      ]);\n\n      expect(valueA).toBe('a');\n      expect(valueB1).toBe('b');\n\n      expect(identityLoadCalls).toEqual([['a', 'b']]);\n      expect(Object.keys(aCustomMap.stash)).toEqual(['a', 'b']);\n\n      const [valueC, valueB2] = await Promise.all([\n        identityLoader.load('c'),\n        identityLoader.load('b'),\n      ]);\n\n      expect(valueC).toBe('c');\n      expect(valueB2).toBe('b');\n\n      expect(identityLoadCalls).toEqual([['a', 'b'], ['c']]);\n      expect(Object.keys(aCustomMap.stash)).toEqual(['a', 'b', 'c']);\n\n      // Supports clear\n\n      identityLoader.clear('b');\n      const valueB3 = await identityLoader.load('b');\n\n      expect(valueB3).toBe('b');\n      expect(identityLoadCalls).toEqual([['a', 'b'], ['c'], ['b']]);\n      expect(Object.keys(aCustomMap.stash)).toEqual(['a', 'c', 'b']);\n\n      // Supports clear all\n\n      identityLoader.clearAll();\n\n      expect(Object.keys(aCustomMap.stash)).toEqual([]);\n    });\n  });\n});\n\ndescribe('It allows custom schedulers', () => {\n  it('Supports manual dispatch', () => {\n    function createScheduler() {\n      let callbacks = [];\n      return {\n        schedule(callback) {\n          callbacks.push(callback);\n        },\n        dispatch() {\n          callbacks.forEach(callback => callback());\n          callbacks = [];\n        },\n      };\n    }\n\n    const { schedule, dispatch } = createScheduler();\n    const [identityLoader, loadCalls] = idLoader<string>({\n      batchScheduleFn: schedule,\n    });\n\n    identityLoader.load('A');\n    identityLoader.load('B');\n    dispatch();\n    identityLoader.load('A');\n    identityLoader.load('C');\n    dispatch();\n    // Note: never dispatched!\n    identityLoader.load('D');\n\n    expect(loadCalls).toEqual([['A', 'B'], ['C']]);\n  });\n\n  it('Custom batch scheduler is provided loader as this context', () => {\n    let that;\n    function batchScheduleFn(callback) {\n      that = this;\n      callback();\n    }\n\n    const [identityLoader] = idLoader<string>({ batchScheduleFn });\n\n    identityLoader.load('A');\n    expect(that).toBe(identityLoader);\n  });\n});\n\ndescribe('It is resilient to job queue ordering', () => {\n  it('batches loads occuring within promises', async () => {\n    const [identityLoader, loadCalls] = idLoader<string>();\n\n    await Promise.all([\n      identityLoader.load('A'),\n      Promise.resolve()\n        .then(() => Promise.resolve())\n        .then(() => {\n          identityLoader.load('B');\n          Promise.resolve()\n            .then(() => Promise.resolve())\n            .then(() => {\n              identityLoader.load('C');\n              Promise.resolve()\n                .then(() => Promise.resolve())\n                .then(() => {\n                  identityLoader.load('D');\n                });\n            });\n        }),\n    ]);\n\n    expect(loadCalls).toEqual([['A', 'B', 'C', 'D']]);\n  });\n\n  it('can call a loader from a loader', async () => {\n    const deepLoadCalls = [];\n    const deepLoader = new DataLoader<\n      $ReadOnlyArray<string>,\n      $ReadOnlyArray<string>,\n    >(keys => {\n      deepLoadCalls.push(keys);\n      return Promise.resolve(keys);\n    });\n\n    const aLoadCalls = [];\n    const aLoader = new DataLoader<string, string>(keys => {\n      aLoadCalls.push(keys);\n      return deepLoader.load(keys);\n    });\n\n    const bLoadCalls = [];\n    const bLoader = new DataLoader<string, string>(keys => {\n      bLoadCalls.push(keys);\n      return deepLoader.load(keys);\n    });\n\n    const [a1, b1, a2, b2] = await Promise.all([\n      aLoader.load('A1'),\n      bLoader.load('B1'),\n      aLoader.load('A2'),\n      bLoader.load('B2'),\n    ]);\n\n    expect(a1).toBe('A1');\n    expect(b1).toBe('B1');\n    expect(a2).toBe('A2');\n    expect(b2).toBe('B2');\n\n    expect(aLoadCalls).toEqual([['A1', 'A2']]);\n    expect(bLoadCalls).toEqual([['B1', 'B2']]);\n    expect(deepLoadCalls).toEqual([\n      [\n        ['A1', 'A2'],\n        ['B1', 'B2'],\n      ],\n    ]);\n  });\n});\n"
  },
  {
    "path": "src/__tests__/oldbrowser.test.js",
    "content": "/**\n * Copyright (c) 2019-present, GraphQL Foundation\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n * @flow\n */\n\n// Mock out process.nextTick and setImmediate as not existing for this test\n// before requiring.\nprocess.nextTick = (null: any);\nglobal.setImmediate = (null: any);\nconst DataLoader = require('..');\n\ndescribe('Old browser support', () => {\n  it('batches multiple requests without setImmediate', async () => {\n    const loadCalls = [];\n    const identityLoader = new DataLoader<number, number>(async keys => {\n      loadCalls.push(keys);\n      return keys;\n    });\n\n    const promise1 = identityLoader.load(1);\n    const promise2 = identityLoader.load(2);\n\n    const [value1, value2] = await Promise.all([promise1, promise2]);\n    expect(value1).toBe(1);\n    expect(value2).toBe(2);\n\n    expect(loadCalls).toEqual([[1, 2]]);\n  });\n});\n"
  },
  {
    "path": "src/__tests__/unhandled.test.js",
    "content": "/**\n * Copyright (c) 2019-present, GraphQL Foundation\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n * @flow\n */\n\nconst DataLoader = require('..');\n\ndescribe('Unhandled rejections', () => {\n  it('Not catching a primed error is an unhandled rejection', async () => {\n    let hadUnhandledRejection = false;\n    // Override Jest's unhandled detection\n    global.jasmine.process.removeAllListeners('unhandledRejection');\n    global.jasmine.process.on('unhandledRejection', () => {\n      hadUnhandledRejection = true;\n    });\n\n    const identityLoader = new DataLoader<number, number>(async keys => keys);\n\n    identityLoader.prime(1, new Error('Error: 1'));\n\n    // Ignore result.\n    identityLoader.load(1);\n\n    await new Promise(resolve => setTimeout(resolve, 10));\n    expect(hadUnhandledRejection).toBe(true);\n  });\n});\n"
  }
]