Repository: graphql/dataloader
Branch: main
Commit: 468257a47a8f
Files: 31
Total size: 125.0 KB
Directory structure:
gitextract_vqab7clx/
├── .changeset/
│ ├── README.md
│ └── config.json
├── .eslintignore
├── .eslintrc
├── .flowconfig
├── .github/
│ ├── ISSUE_TEMPLATE/
│ │ ├── bug-report.md
│ │ ├── feature-request.md
│ │ └── question.md
│ └── workflows/
│ └── validation.yml
├── .gitignore
├── CHANGELOG.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── babel.config.js
├── examples/
│ ├── CouchDB.md
│ ├── GoogleDatastore.md
│ ├── Knex.md
│ ├── Redis.md
│ ├── RethinkDB.md
│ └── SQL.md
├── flow-typed/
│ └── npm/
│ └── jest_v24.x.x.js
├── package.json
├── renovate.json
├── resources/
│ ├── prepublish.sh
│ └── watch.js
└── src/
└── __tests__/
├── abuse.test.js
├── browser.test.js
├── dataloader.test.js
├── oldbrowser.test.js
└── unhandled.test.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .changeset/README.md
================================================
# Changesets
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
================================================
FILE: .changeset/config.json
================================================
{
"$schema": "https://unpkg.com/@changesets/config@2.1.0/schema.json",
"changelog": [
"@changesets/changelog-github",
{ "repo": "graphql/dataloader" }
],
"commit": false,
"fixed": [],
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
================================================
FILE: .eslintignore
================================================
dist/
flow-typed/
================================================
FILE: .eslintrc
================================================
{
"globals": {
"$ReadOnlyArray": true
},
"parser": "babel-eslint",
"plugins": ["prettier"],
"env": {
"es6": true,
"node": true,
"jest": true
},
"rules": {
"block-scoped-var": 0,
"callback-return": 2,
"camelcase": [2, {"properties": "always"}],
"comma-dangle": 0,
"comma-spacing": 0,
"complexity": 0,
"computed-property-spacing": [2, "never"],
"consistent-return": 0,
"consistent-this": 0,
"default-case": 0,
"dot-location": [2, "property"],
"dot-notation": 0,
"eol-last": 2,
"eqeqeq": 2,
"func-names": 0,
"func-style": 0,
"generator-star-spacing": [0, {"before": true, "after": false}],
"guard-for-in": 2,
"handle-callback-err": [2, "error"],
"id-length": 0,
"id-match": [2, "^(?:_?[a-zA-Z0-9]*)|[_A-Z0-9]+$"],
"init-declarations": 0,
"key-spacing": [2, {"beforeColon": false, "afterColon": true}],
"keyword-spacing": 2,
"linebreak-style": 2,
"lines-around-comment": 0,
"max-depth": 0,
"max-nested-callbacks": 0,
"max-params": 0,
"max-statements": 0,
"new-cap": 0,
"new-parens": 2,
"newline-after-var": 0,
"no-alert": 2,
"no-array-constructor": 2,
"no-bitwise": 0,
"no-caller": 2,
"no-catch-shadow": 0,
"no-class-assign": 2,
"no-cond-assign": 2,
"no-console": 1,
"no-const-assign": 2,
"no-constant-condition": 2,
"no-continue": 0,
"no-control-regex": 0,
"no-debugger": 1,
"no-delete-var": 2,
"no-div-regex": 2,
"no-dupe-args": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-else-return": 2,
"no-empty": 2,
"no-empty-character-class": 2,
"no-eq-null": 0,
"no-eval": 2,
"no-ex-assign": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-extra-boolean-cast": 2,
"no-extra-parens": 0,
"no-extra-semi": 2,
"no-fallthrough": 2,
"no-floating-decimal": 2,
"no-func-assign": 2,
"no-implicit-coercion": 2,
"no-implied-eval": 2,
"no-inline-comments": 0,
"no-inner-declarations": [2, "functions"],
"no-invalid-regexp": 2,
"no-invalid-this": 0,
"no-irregular-whitespace": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 0,
"no-lone-blocks": 2,
"no-lonely-if": 2,
"no-loop-func": 0,
"no-mixed-requires": [2, true],
"no-mixed-spaces-and-tabs": 2,
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-multiple-empty-lines": 0,
"no-native-reassign": 0,
"no-negated-in-lhs": 2,
"no-nested-ternary": 0,
"no-new": 2,
"no-new-func": 0,
"no-new-object": 2,
"no-new-require": 2,
"no-new-wrappers": 2,
"no-obj-calls": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-param-reassign": 2,
"no-path-concat": 2,
"no-plusplus": 0,
"no-process-env": 0,
"no-process-exit": 0,
"no-proto": 2,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-restricted-modules": 0,
"no-return-assign": 2,
"no-script-url": 2,
"no-self-compare": 0,
"no-sequences": 2,
"no-shadow": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-sparse-arrays": 2,
"no-sync": 2,
"no-ternary": 0,
"no-this-before-super": 2,
"no-throw-literal": 2,
"no-trailing-spaces": 2,
"no-undef": 2,
"no-undef-init": 2,
"no-undefined": 0,
"no-underscore-dangle": 0,
"no-unexpected-multiline": 2,
"no-unneeded-ternary": 2,
"no-unreachable": 2,
"no-unused-expressions": 2,
"no-unused-vars": [2, {"vars": "all", "args": "after-used"}],
"no-use-before-define": 0,
"no-useless-call": 2,
"no-var": 0,
"no-void": 2,
"no-warning-comments": 0,
"no-with": 2,
"object-curly-spacing": [0, "always"],
"object-shorthand": [2, "always"],
"one-var": [2, "never"],
"operator-assignment": [2, "always"],
"prefer-const": 0,
"prefer-reflect": 0,
"prefer-spread": 0,
"quote-props": [2, "as-needed"],
"radix": 2,
"require-yield": 2,
"semi-spacing": [2, {"before": false, "after": true}],
"sort-vars": 0,
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
"space-in-parens": 0,
"space-infix-ops": [2, {"int32Hint": false}],
"space-unary-ops": [2, {"words": true, "nonwords": false}],
"spaced-comment": [2, "always"],
"strict": 0,
"use-isnan": 2,
"valid-jsdoc": 0,
"valid-typeof": 2,
"vars-on-top": 0,
"wrap-iife": 2,
"wrap-regex": 0,
"yoda": [2, "never", {"exceptRange": true}],
"prettier/prettier": 2
}
}
================================================
FILE: .flowconfig
================================================
[ignore]
.*/lib/.*
.*/dist/.*
.*/coverage/.*
.*/resources/.*
.*/node_modules/y18n/test/.*
[include]
[libs]
[options]
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectError
include_warnings=true
================================================
FILE: .github/ISSUE_TEMPLATE/bug-report.md
================================================
---
name: Bug report
about: Create a report to help us improve
title: "[BUG]"
labels: bug
---
## Expected Behavior
## Current Behavior
## Possible Solution
## Steps to Reproduce
## Context
================================================
FILE: .github/ISSUE_TEMPLATE/feature-request.md
================================================
---
name: Feature request
about: Suggest an idea for this project
title: "[REQUEST]"
labels: enhancement
---
## What problem are you trying to solve?
## Describe the solution you'd like
## Describe alternatives you've considered
## Additional context
================================================
FILE: .github/ISSUE_TEMPLATE/question.md
================================================
---
name: Question
about: Not a bug or feature request?
title: "[QUESTION]"
labels: help wanted
---
================================================
FILE: .github/workflows/validation.yml
================================================
name: Flow check, Lint and Tests
on: push
jobs:
validation:
name: Testing on Node ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20, 22]
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Use Node ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- name: Install Dependencies using Yarn
run: yarn --ignore-engines
- name: Tests
run: yarn test:ci
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
fail_ci_if_error: true
================================================
FILE: .gitignore
================================================
*.swp
*~
*.iml
.*.haste_cache.*
.DS_Store
.idea
npm-debug.log
node_modules
coverage
dist
# Generated with release scripts
index.d.ts
index.js
index.js.flow
================================================
FILE: CHANGELOG.md
================================================
# dataloader
## 2.2.3
### Patch Changes
- [#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.
## 2.2.2
### Patch Changes
- [#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
## 2.2.1
### Patch Changes
- [#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
## 2.2.0
### Minor Changes
- [#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.
### Patch Changes
- [#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.
* [#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
- [#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.
## 2.1.0
### Minor Changes
- 28cf959: - Do not return void results from arrow functions https://github.com/graphql/dataloader/commit/3b0bae94e91453d9a432c02628745252abc5e011
- Fix typo in `loader.load()` error message https://github.com/graphql/dataloader/commit/249b2b966a8807c50e07746ff04acb8c48fa4357
- Fix typo in SQL example https://github.com/graphql/dataloader/commit/cae1a3d9bfa48e181a49fd443f43813b335dc120
- Fix typo in TypeScript declaration https://github.com/graphql/dataloader/commit/ef6d32f97cde16aba84d96dc806c4439eaf8efae
- 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
### Patch Changes
- 3135e9a: Fix typo in jsdoc comment; flip "objects are keys" to "keys are objects"
================================================
FILE: CONTRIBUTING.md
================================================
# Contributing to DataLoader
We want to make contributing to this project as easy and transparent as
possible.
## Code of Conduct
This 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)
## Pull Requests
We actively welcome your pull requests for documentation and code.
1. Fork the repo and create your branch from `master`.
2. If you've added code that should be tested, add tests with 100% coverage.
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes.
5. Make sure your code lints.
6. If you haven't already, complete the Contributor License Agreement ("CLA").
7. 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)
8. Open a Pull Request so we can review and incorporate your change.
## Releases
To release a new version:
1. Run `yarn changeset version` to bump the version of the package.
2. Run `yarn release` this will create a new release on GitHub and publish the package to NPM.
## Issues
We use GitHub issues to track public bugs. Please ensure your description is
clear and has sufficient instructions to be able to reproduce the issue.
## Coding Style
- 2 spaces for indentation rather than tabs
- 80 character line length
- See .eslintrc for the gory details.
## License
By contributing to DataLoader, you agree that your contributions will be
licensed under its MIT license.
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) GraphQL Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# DataLoader
DataLoader is a generic utility to be used as part of your application's data
fetching layer to provide a simplified and consistent API over various remote
data sources such as databases or web services via batching and caching.
[](https://github.com/graphql/dataloader/actions/workflows/validation.yml)
[](https://coveralls.io/github/graphql/dataloader?branch=main)
A port of the "Loader" API originally developed by [@schrockn][] at Facebook in
2010 as a simplifying force to coalesce the sundry key-value store back-end
APIs which existed at the time. At Facebook, "Loader" became one of the
implementation details of the "Ent" framework, a privacy-aware data entity
loading and caching layer within web server product code. This ultimately became
the underpinning for Facebook's GraphQL server implementation and type
definitions.
DataLoader is a simplified version of this original idea implemented in
JavaScript for Node.js services. DataLoader is often used when implementing a
[graphql-js][] service, though it is also broadly useful in other situations.
This mechanism of batching and caching data requests is certainly not unique to
Node.js or JavaScript, it is also the primary motivation for
[Haxl](https://github.com/facebook/Haxl), Facebook's data loading library
for 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/).
DataLoader is provided so that it may be useful not just to build GraphQL
services for Node.js but also as a publicly available reference implementation
of this concept in the hopes that it can be ported to other languages. If you
port DataLoader to another language, please open an issue to include a link from
this repository.
## Getting Started
First, install DataLoader using npm.
```sh
npm install --save dataloader
```
To get started, create a `DataLoader`. Each `DataLoader` instance represents a
unique cache. Typically instances are created per request when used within a
web-server like [express][] if different users can see different things.
> Note: DataLoader assumes a JavaScript environment with global ES6 `Promise`
> and `Map` classes, available in all supported versions of Node.js.
## Batching
Batching is not an advanced feature, it's DataLoader's primary feature.
Create loaders by providing a batch loading function.
```js
const DataLoader = require('dataloader');
const userLoader = new DataLoader(keys => myBatchGetUsers(keys));
```
A batch loading function accepts an Array of keys, and returns a Promise which
resolves to an Array of values[\*](#batch-function).
Then load individual values from the loader. DataLoader will coalesce all
individual loads which occur within a single frame of execution (a single tick
of the event loop) and then call your batch function with all requested keys.
```js
const user = await userLoader.load(1);
const invitedBy = await userLoader.load(user.invitedByID);
console.log(`User 1 was invited by ${invitedBy}`);
// Elsewhere in your application
const user = await userLoader.load(2);
const lastInvited = await userLoader.load(user.lastInvitedID);
console.log(`User 2 last invited ${lastInvited}`);
```
A naive application may have issued four round-trips to a backend for the
required information, but with DataLoader this application will make at most
two.
DataLoader allows you to decouple unrelated parts of your application without
sacrificing the performance of batch data-loading. While the loader presents an
API that loads individual values, all concurrent requests will be coalesced and
presented to your batch loading function. This allows your application to safely
distribute data fetching requirements throughout your application and maintain
minimal outgoing data requests.
#### Batch Function
A batch loading function accepts an Array of keys, and returns a Promise which
resolves to an Array of values or Error instances. The loader itself is provided
as the `this` context.
```js
async function batchFunction(keys) {
const results = await db.fetchAllKeys(keys);
return keys.map(key => results[key] || new Error(`No result for ${key}`));
}
const loader = new DataLoader(batchFunction);
```
There are a few constraints this function must uphold:
- The Array of values must be the same length as the Array of keys.
- Each index in the Array of values must correspond to the same index in the Array of keys.
For example, if your batch function was provided the Array of keys: `[ 2, 9, 6, 1 ]`,
and loading from a back-end service returned the values:
```js
{ id: 9, name: 'Chicago' }
{ id: 1, name: 'New York' }
{ id: 2, name: 'San Francisco' }
```
Our back-end service returned results in a different order than we requested, likely
because it was more efficient for it to do so. Also, it omitted a result for key `6`,
which we can interpret as no value existing for that key.
To uphold the constraints of the batch function, it must return an Array of values
the same length as the Array of keys, and re-order them to ensure each index aligns
with the original keys `[ 2, 9, 6, 1 ]`:
```js
[
{ id: 2, name: 'San Francisco' },
{ id: 9, name: 'Chicago' },
null, // or perhaps `new Error()`
{ id: 1, name: 'New York' },
];
```
#### Batch Scheduling
By default DataLoader will coalesce all individual loads which occur within a
single frame of execution before calling your batch function with all requested
keys. This ensures no additional latency while capturing many related requests
into a single batch. In fact, this is the same behavior used in Facebook's
original PHP implementation in 2010. See `enqueuePostPromiseJob` in the
[source code][] for more details about how this works.
However sometimes this behavior is not desirable or optimal. Perhaps you expect
requests to be spread out over a few subsequent ticks because of an existing use
of `setTimeout`, or you just want manual control over dispatching regardless of
the run loop. DataLoader allows providing a custom batch scheduler to provide
these or any other behaviors.
A custom scheduler is provided as `batchScheduleFn` in options. It must be a
function which is passed a callback and is expected to call that callback in the
immediate future to execute the batch request.
As an example, here is a batch scheduler which collects all requests over a
100ms window of time (and as a consequence, adds 100ms of latency):
```js
const myLoader = new DataLoader(myBatchFn, {
batchScheduleFn: callback => setTimeout(callback, 100),
});
```
As another example, here is a manually dispatched batch scheduler:
```js
function createScheduler() {
let callbacks = [];
return {
schedule(callback) {
callbacks.push(callback);
},
dispatch() {
callbacks.forEach(callback => callback());
callbacks = [];
},
};
}
const { schedule, dispatch } = createScheduler();
const myLoader = new DataLoader(myBatchFn, { batchScheduleFn: schedule });
myLoader.load(1);
myLoader.load(2);
dispatch();
```
## Caching
DataLoader provides a memoization cache for all loads which occur in a single
request to your application. After `.load()` is called once with a given key,
the resulting value is cached to eliminate redundant loads.
#### Caching Per-Request
DataLoader caching _does not_ replace Redis, Memcache, or any other shared
application-level cache. DataLoader is first and foremost a data loading mechanism,
and its cache only serves the purpose of not repeatedly loading the same data in
the context of a single request to your Application. To do this, it maintains a
simple in-memory memoization cache (more accurately: `.load()` is a memoized function).
Avoid multiple requests from different users using the DataLoader instance, which
could result in cached data incorrectly appearing in each request. Typically,
DataLoader instances are created when a Request begins, and are not used once the
Request ends.
For example, when using with [express][]:
```js
function createLoaders(authToken) {
return {
users: new DataLoader(ids => genUsers(authToken, ids)),
};
}
const app = express();
app.get('/', function (req, res) {
const authToken = authenticateUser(req);
const loaders = createLoaders(authToken);
res.send(renderPage(req, loaders));
});
app.listen();
```
#### Caching and Batching
Subsequent calls to `.load()` with the same key will result in that key not
appearing in the keys provided to your batch function. _However_, the resulting
Promise will still wait on the current batch to complete. This way both cached
and uncached requests will resolve at the same time, allowing DataLoader
optimizations for subsequent dependent loads.
In the example below, User `1` happens to be cached. However, because User `1`
and `2` are loaded in the same tick, they will resolve at the same time. This
means both `user.bestFriendID` loads will also happen in the same tick which
results in two total requests (the same as if User `1` had not been cached).
```js
userLoader.prime(1, { bestFriend: 3 });
async function getBestFriend(userID) {
const user = await userLoader.load(userID);
return await userLoader.load(user.bestFriendID);
}
// In one part of your application
getBestFriend(1);
// Elsewhere
getBestFriend(2);
```
Without this optimization, if the cached User `1` resolved immediately, this
could result in three total requests since each `user.bestFriendID` load would
happen at different times.
#### Clearing Cache
In certain uncommon cases, clearing the request cache may be necessary.
The most common example when clearing the loader's cache is necessary is after
a mutation or update within the same request, when a cached value could be out of
date and future loads should not use any possibly cached value.
Here's a simple example using SQL UPDATE to illustrate.
```js
// Request begins...
const userLoader = new DataLoader(...);
// And a value happens to be loaded (and cached).
const user = await userLoader.load(4);
// A mutation occurs, invalidating what might be in cache.
await sqlRun('UPDATE users WHERE id=4 SET username="zuck"');
userLoader.clear(4);
// Later the value load is loaded again so the mutated data appears.
const user = await userLoader.load(4);
// Request completes.
```
#### Caching Errors
If a batch load fails (that is, a batch function throws or returns a rejected
Promise), then the requested values will not be cached. However if a batch
function returns an `Error` instance for an individual value, that `Error` will
be cached to avoid frequently loading the same `Error`.
In some circumstances you may wish to clear the cache for these individual Errors:
```js
try {
const user = await userLoader.load(1);
} catch (error) {
if (/* determine if the error should not be cached */) {
userLoader.clear(1);
}
throw error
}
```
#### Disabling Cache
In certain uncommon cases, a DataLoader which _does not_ cache may be desirable.
Calling `new DataLoader(myBatchFn, { cache: false })` will ensure that every
call to `.load()` will produce a _new_ Promise, and requested keys will not be
saved in memory.
However, when the memoization cache is disabled, your batch function will
receive an array of keys which may contain duplicates! Each key will be
associated with each call to `.load()`. Your batch loader should provide a value
for each instance of the requested key.
For example:
```js
const myLoader = new DataLoader(
keys => {
console.log(keys);
return someBatchLoadFn(keys);
},
{ cache: false },
);
myLoader.load('A');
myLoader.load('B');
myLoader.load('A');
// > [ 'A', 'B', 'A' ]
```
More complex cache behavior can be achieved by calling `.clear()` or `.clearAll()`
rather than disabling the cache completely. For example, this DataLoader will
provide unique keys to a batch function due to the memoization cache being
enabled, but will immediately clear its cache when the batch function is called
so later requests will load new values.
```js
const myLoader = new DataLoader(keys => {
myLoader.clearAll();
return someBatchLoadFn(keys);
});
```
#### Custom Cache
As mentioned above, DataLoader is intended to be used as a per-request cache.
Since requests are short-lived, DataLoader uses an infinitely growing [Map][] as
a memoization cache. This should not pose a problem as most requests are
short-lived and the entire cache can be discarded after the request completes.
However this memoization caching strategy isn't safe when using a long-lived
DataLoader, since it could consume too much memory. If using DataLoader in this
way, you can provide a custom Cache instance with whatever behavior you prefer,
as long as it follows the same API as [Map][].
The example below uses an LRU (least recently used) cache to limit total memory
to hold at most 100 cached values via the [lru_map][] npm package.
```js
import { LRUMap } from 'lru_map';
const myLoader = new DataLoader(someBatchLoadFn, {
cacheMap: new LRUMap(100),
});
```
More specifically, any object that implements the methods `get()`, `set()`,
`delete()` and `clear()` methods can be provided. This allows for custom Maps
which implement various [cache algorithms][] to be provided.
## API
#### class DataLoader
DataLoader creates a public API for loading data from a particular
data back-end with unique keys such as the `id` column of a SQL table or
document name in a MongoDB database, given a batch loading function.
Each `DataLoader` instance contains a unique memoized cache. Use caution when
used in long-lived applications or those which serve many users with different
access permissions and consider creating a new instance per web request.
##### `new DataLoader(batchLoadFn [, options])`
Create a new `DataLoader` given a batch loading function and options.
- _batchLoadFn_: A function which accepts an Array of keys, and returns a
Promise which resolves to an Array of values.
- _options_: An optional object of options:
| Option Key | Type | Default | Description |
| ----------------- | -------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `batch` | Boolean | `true` | Set to `false` to disable batching, invoking `batchLoadFn` with a single load key. This is equivalent to setting `maxBatchSize` to `1`. |
| `maxBatchSize` | Number | `Infinity` | Limits the number of items that get passed in to the `batchLoadFn`. May be set to `1` to disable batching. |
| `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. |
| `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`. |
| `cacheKeyFn` | Function | `key => key` | Produces cache key for a given load key. Useful when objects are keys and two objects should be considered equivalent. |
| `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. |
| `name` | String | `null` | The name given to this `DataLoader` instance. Useful for APM tools. |
##### `load(key)`
Loads a key, returning a `Promise` for the value represented by that key.
- _key_: A key value to load.
##### `loadMany(keys)`
Loads multiple keys, promising an array of values:
```js
const [a, b] = await myLoader.loadMany(['a', 'b']);
```
This is similar to the more verbose:
```js
const [a, b] = await Promise.all([myLoader.load('a'), myLoader.load('b')]);
```
However it is different in the case where any load fails. Where
Promise.all() would reject, loadMany() always resolves, however each result
is either a value or an Error instance.
```js
var [a, b, c] = await myLoader.loadMany(['a', 'b', 'badkey']);
// c instanceof Error
```
- _keys_: An array of key values to load.
##### `clear(key)`
Clears the value at `key` from the cache, if it exists. Returns itself for
method chaining.
- _key_: A key value to clear.
##### `clearAll()`
Clears the entire cache. To be used when some event results in unknown
invalidations across this particular `DataLoader`. Returns itself for
method chaining.
##### `prime(key, value)`
Primes the cache with the provided key and value. If the key already exists, no
change is made. (To forcefully prime the cache, clear the key first with
`loader.clear(key).prime(key, value)`.) Returns itself for method chaining.
To prime the cache with an error at a key, provide an Error instance.
## Using with GraphQL
DataLoader pairs nicely well with [GraphQL][graphql-js]. GraphQL fields are
designed to be stand-alone functions. Without a caching or batching mechanism,
it's easy for a naive GraphQL server to issue new database requests each time a
field is resolved.
Consider the following GraphQL request:
```
{
me {
name
bestFriend {
name
}
friends(first: 5) {
name
bestFriend {
name
}
}
}
}
```
Naively, if `me`, `bestFriend` and `friends` each need to request the backend,
there could be at most 13 database requests!
When using DataLoader, we could define the `User` type using the
[SQLite](examples/SQL.md) example with clearer code and at most 4 database requests,
and possibly fewer if there are cache hits.
```js
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
name: { type: GraphQLString },
bestFriend: {
type: UserType,
resolve: user => userLoader.load(user.bestFriendID),
},
friends: {
args: {
first: { type: GraphQLInt },
},
type: new GraphQLList(UserType),
resolve: async (user, { first }) => {
const rows = await queryLoader.load([
'SELECT toID FROM friends WHERE fromID=? LIMIT ?',
user.id,
first,
]);
return rows.map(row => userLoader.load(row.toID));
},
},
}),
});
```
## Common Patterns
### Creating a new DataLoader per request.
In many applications, a web server using DataLoader serves requests to many
different users with different access permissions. It may be dangerous to use
one cache across many users, and is encouraged to create a new DataLoader
per request:
```js
function createLoaders(authToken) {
return {
users: new DataLoader(ids => genUsers(authToken, ids)),
cdnUrls: new DataLoader(rawUrls => genCdnUrls(authToken, rawUrls)),
stories: new DataLoader(keys => genStories(authToken, keys)),
};
}
// When handling an incoming web request:
const loaders = createLoaders(request.query.authToken);
// Then, within application logic:
const user = await loaders.users.load(4);
const pic = await loaders.cdnUrls.load(user.rawPicUrl);
```
Creating an object where each key is a `DataLoader` is one common pattern which
provides a single value to pass around to code which needs to perform
data loading, such as part of the `rootValue` in a [graphql-js][] request.
### Loading by alternative keys.
Occasionally, some kind of value can be accessed in multiple ways. For example,
perhaps a "User" type can be loaded not only by an "id" but also by a "username"
value. If the same user is loaded by both keys, then it may be useful to fill
both caches when a user is loaded from either source:
```js
const userByIDLoader = new DataLoader(async ids => {
const users = await genUsersByID(ids);
for (let user of users) {
usernameLoader.prime(user.username, user);
}
return users;
});
const usernameLoader = new DataLoader(async names => {
const users = await genUsernames(names);
for (let user of users) {
userByIDLoader.prime(user.id, user);
}
return users;
});
```
### Freezing results to enforce immutability
Since DataLoader caches values, it's typically assumed these values will be
treated as if they were immutable. While DataLoader itself doesn't enforce
this, you can create a higher-order function to enforce immutability
with Object.freeze():
```js
function freezeResults(batchLoader) {
return keys => batchLoader(keys).then(values => values.map(Object.freeze));
}
const myLoader = new DataLoader(freezeResults(myBatchLoader));
```
### Batch functions which return Objects instead of Arrays
DataLoader expects batch functions which return an Array of the same length as
the provided keys. However this is not always a common return format from other
libraries. A DataLoader higher-order function can convert from one format to another. The example below converts a `{ key: value }` result to the format
DataLoader expects.
```js
function objResults(batchLoader) {
return keys =>
batchLoader(keys).then(objValues =>
keys.map(key => objValues[key] || new Error(`No value for ${key}`)),
);
}
const myLoader = new DataLoader(objResults(myBatchLoader));
```
## Common Back-ends
Looking to get started with a specific back-end? Try the [loaders in the examples directory](/examples).
## Other Implementations
Listed in alphabetical order
- Elixir
- [dataloader](https://github.com/absinthe-graphql/dataloader)
- Golang
- [Dataloader](https://github.com/nicksrandall/dataloader)
- Java
- [java-dataloader](https://github.com/graphql-java/java-dataloader)
- .Net
- [GraphQL .NET DataLoader](https://graphql-dotnet.github.io/docs/guides/dataloader/)
- [Green Donut](https://github.com/ChilliCream/graphql-platform?tab=readme-ov-file#green-donut)
- Perl
- [perl-DataLoader](https://github.com/richardjharris/perl-DataLoader)
- PHP
- [DataLoaderPHP](https://github.com/overblog/dataloader-php)
- Python
- [aiodataloader](https://github.com/syrusakbary/aiodataloader)
- ReasonML
- [bs-dataloader](https://github.com/ulrikstrid/bs-dataloader)
- Ruby
- [BatchLoader](https://github.com/exaspark/batch-loader)
- [Dataloader](https://github.com/sheerun/dataloader)
- [GraphQL Batch](https://github.com/Shopify/graphql-batch)
- Rust
- [Dataloader](https://github.com/cksac/dataloader-rs)
- Swift
- [SwiftDataLoader](https://github.com/kimdv/SwiftDataLoader)
- C++
- [cppdataloader](https://github.com/jafarlihi/cppdataloader)
## Video Source Code Walkthrough
**DataLoader Source Code Walkthrough (YouTube):**
A walkthrough of the DataLoader v1 source code. While the source has changed
since this video was made, it is still a good overview of the rationale of
DataLoader and how it works.
[@schrockn]: https://github.com/schrockn
[Map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
[graphql-js]: https://github.com/graphql/graphql-js
[cache algorithms]: https://en.wikipedia.org/wiki/Cache_algorithms
[express]: http://expressjs.com/
[babel/polyfill]: https://babeljs.io/docs/usage/polyfill/
[lru_map]: https://github.com/rsms/js-lru
[source code]: https://github.com/graphql/dataloader/blob/main/src/index.js
# Contributing to this repo
This 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/).
To 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.
You 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).
If 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).
================================================
FILE: babel.config.js
================================================
module.exports = api => ({
presets: api.env('test')
? ['@babel/preset-flow']
: [['@babel/preset-env', { loose: true }], '@babel/preset-flow'],
});
================================================
FILE: examples/CouchDB.md
================================================
# Using DataLoader with CouchDB
CouchDB is a "NoSQL" document database which supports batch loading via the
[HTTP Bulk Document API](http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API),
making it well suited for use with DataLoader.
This example uses the [nano][] CouchDB client which offers a `fetch` method
supporting the bulk document API.
```js
const DataLoader = require('dataloader');
const nano = require('nano');
const couch = nano('http://localhost:5984');
const userDB = couch.use('users');
const userLoader = new DataLoader(
keys =>
new Promise((resolve, reject) => {
userDB.fetch({ keys: keys }, (error, docs) => {
if (error) {
return reject(error);
}
resolve(
docs.rows.map(row => (row.error ? new Error(row.error) : row.doc)),
);
});
}),
);
// Usage
const promise1 = userLoader.load('8fce1902834ac6458e9886fa7f89c0ef');
const promise2 = userLoader.load('00a271787f89c0ef2e10e88a0c00048b');
const [user1, user2] = await Promise.all([promise1, promise2]);
console.log(user1, user2);
```
[nano]: https://github.com/dscape/nano
================================================
FILE: examples/GoogleDatastore.md
================================================
# Using DataLoader with Google Datastore
Google Datastore is a "NoSQL" document database which supports [batch operations](https://cloud.google.com/datastore/docs/concepts/entities#batch_operations),
making it well suited for use with DataLoader.
Here we build an example Google Datastore DataLoader using [@google-cloud/datastore](https://cloud.google.com/nodejs/docs/reference/datastore/1.3.x/Datastore).
```js
const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
const datastoreLoader = new DataLoader(
async keys => {
const results = await datastore.get(keys);
// Sort resulting entities by the keys they were requested with.
const entities = results[0];
const entitiesByKey = {};
entities.forEach(entity => {
entitiesByKey[JSON.stringify(entity[datastore.KEY])] = entity;
});
return keys.map(key => entitiesByKey[JSON.stringify(key)] || null);
},
{
// Datastore complex keys need to be converted to a string for use as cache keys
cacheKeyFn: key => JSON.stringify(key),
},
);
```
================================================
FILE: examples/Knex.md
================================================
# Using DataLoader with Knex.js
This example demonstrates how to use **DataLoader** with SQL databases via
[Knex.js][knex], which is a SQL query builder and a client for popular
databases such as **PostgreSQL**, **MySQL**, **MariaDB** etc.
Similarly to the [SQL](./SQL.md) example, you can use "where in" clause to
fetch multiple records by the list of IDs with the only difference that you
don't have to write any SQL code by hand.
```js
const DataLoader = require('dataloader');
const db = require('./db'); // an instance of Knex client
// The list of data loaders
const loaders = {
user: new DataLoader(ids =>
db
.table('users')
.whereIn('id', ids)
.select()
.then(rows => ids.map(id => rows.find(x => x.id === id))),
),
story: new DataLoader(ids =>
db
.table('stories')
.whereIn('id', ids)
.select()
.then(rows => ids.map(id => rows.find(x => x.id === id))),
),
storiesByUserId: new DataLoader(ids =>
db
.table('stories')
.whereIn('author_id', ids)
.select()
.then(rows => ids.map(id => rows.filter(x => x.author_id === id))),
),
};
// Usage
const [user, stories] = await Promise.all([
loaders.user.load('1234'),
loaders.storiesByUserId.load('1234'),
]);
```
For a complete example visit [kriasoft/nodejs-api-starter][nsk].
[knex]: http://knexjs.org/
[nsk]: https://github.com/kriasoft/nodejs-api-starter#readme
================================================
FILE: examples/Redis.md
================================================
# Using DataLoader with Redis
Redis is a very simple key-value store which provides the batch load method
[MGET](http://redis.io/commands/mget) which makes it very well suited for use
with DataLoader.
Here we build an example Redis DataLoader using [node_redis][].
```js
const DataLoader = require('dataloader');
const redis = require('redis');
const client = redis.createClient();
const redisLoader = new DataLoader(
keys =>
new Promise((resolve, reject) => {
client.mget(keys, (error, results) => {
if (error) {
return reject(error);
}
resolve(
results.map((result, index) =>
result !== null ? result : new Error(`No key: ${keys[index]}`),
),
);
});
}),
);
```
[node_redis]: https://github.com/NodeRedis/node_redis
================================================
FILE: examples/RethinkDB.md
================================================
# RethinkDb
RethinkDb offers a batching method called `getAll` but there are a few caveats :
- Order of results is not guaranteed ([rethinkdb/rethinkdb#5187](https://github.com/rethinkdb/rethinkdb/issues/5187))
- Non-existent keys will not return an empty record
For example, against a table `example_table` with these records:
```js
[
{ id: 1, name: 'Document 1' },
{ id: 2, name: 'Document 2' },
];
```
A query `r.getAll(1, 2, 3)` could return:
```js
[
{ id: 2, name: 'Document 2' },
{ id: 1, name: 'Document 1' },
];
```
Because query keys and values are associated by position in the dataloader
cache, this naive implementation won't work (with the same table as above):
```js
const r = require('rethinkdb');
const db = await r.connect();
const exampleLoader = new DataLoader(async keys => {
const result = await db.table('example_table').getAll(...keys);
return result.toArray();
});
await exampleLoader.loadMany([1, 2, 3]); // Throws (values length !== keys length)
await exampleLoader.loadMany([1, 2]);
await exampleLoader.load(1); // {"id": 2, "name": "Document 2"}
```
A solution is to normalize results returned by `getAll` to match the structure
of supplied `keys`.
To achieve this efficiently, we first write an indexing function. This function
will return a `Map` indexing results.
Parameters:
- `results`: Array of RethinkDb results
- `indexField`: String indicating which field was used as index for this batch query
- `cacheKeyFn`: Optional function used to serialize non-scalar index field values
```js
function indexResults(results, indexField, cacheKeyFn = key => key) {
const indexedResults = new Map();
results.forEach(res => {
indexedResults.set(cacheKeyFn(res[indexField]), res);
});
return indexedResults;
}
```
Then, we can leverage our Map to normalize RethinkDb results with another
utility function which will produce a normalizing function.
```js
function normalizeRethinkDbResults(keys, indexField, cacheKeyFn = key => key) {
return results => {
const indexedResults = indexResults(results, indexField, cacheKeyFn);
return keys.map(
val =>
indexedResults.get(cacheKeyFn(val)) ||
new Error(`Key not found : ${val}`),
);
};
}
```
Full dataloader implementation:
```js
const r = require('rethinkdb');
const db = await r.connect();
const exampleLoader = new DataLoader(async keys => {
const results = await db.table('example_table').getAll(...keys);
return normalizeRethinkDbResults(res.toArray(), 'id');
});
// [{"id": 1, "name": "Document 1"}, {"id": 2, "name": "Document 2"}, Error];
await exampleLoader.loadMany([1, 2, 3]);
// {"id": 1, "name": "Document 1"}
await exampleLoader.load(1);
```
================================================
FILE: examples/SQL.md
================================================
# Using DataLoader with SQLite
While not a key-value store, SQL offers a natural batch mechanism with
`SELECT * WHERE IN` statements. While `DataLoader` is best suited for key-value
stores, it is still suited for SQL when queries remain simple. This example
requests the entire row at a given `id`, however your usage may differ.
```js
const DataLoader = require('dataloader');
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('./to/your/db.sql');
// Dispatch a WHERE-IN query, ensuring response has rows in correct order.
const userLoader = new DataLoader(
ids =>
new Promise((resolve, reject) => {
db.all(
'SELECT * FROM users WHERE id IN $ids',
{ $ids: ids },
(error, rows) => {
if (error) {
reject(error);
} else {
resolve(
ids.map(
id =>
rows.find(row => row.id === id) ||
new Error(`Row not found: ${id}`),
),
);
}
},
);
}),
);
// Usage
const promise1 = userLoader.load('1234');
const promise2 = userLoader.load('5678');
const [user1, user2] = await Promise.all([promise1, promise2]);
console.log(user1, user2);
```
[sqlite3]: https://github.com/mapbox/node-sqlite3
================================================
FILE: flow-typed/npm/jest_v24.x.x.js
================================================
// flow-typed signature: 27f8467378a99b6130bd20f54f31a644
// flow-typed version: 6cb9e99836/jest_v24.x.x/flow_>=v0.104.x
type JestMockFn, TReturn> = {
(...args: TArguments): TReturn,
/**
* An object for introspecting mock calls
*/
mock: {
/**
* An array that represents all calls that have been made into this mock
* function. Each call is represented by an array of arguments that were
* passed during the call.
*/
calls: Array,
/**
* An array that contains all the object instances that have been
* instantiated from this mock function.
*/
instances: Array,
/**
* An array that contains all the object results that have been
* returned by this mock function call
*/
results: Array<{
isThrow: boolean,
value: TReturn,
...
}>,
...
},
/**
* Resets all information stored in the mockFn.mock.calls and
* mockFn.mock.instances arrays. Often this is useful when you want to clean
* up a mock's usage data between two assertions.
*/
mockClear(): void,
/**
* Resets all information stored in the mock. This is useful when you want to
* completely restore a mock back to its initial state.
*/
mockReset(): void,
/**
* Removes the mock and restores the initial implementation. This is useful
* when you want to mock functions in certain test cases and restore the
* original implementation in others. Beware that mockFn.mockRestore only
* works when mock was created with jest.spyOn. Thus you have to take care of
* restoration yourself when manually assigning jest.fn().
*/
mockRestore(): void,
/**
* Accepts a function that should be used as the implementation of the mock.
* The mock itself will still record all calls that go into and instances
* that come from itself -- the only difference is that the implementation
* will also be executed when the mock is called.
*/
mockImplementation(
fn: (...args: TArguments) => TReturn
): JestMockFn,
/**
* Accepts a function that will be used as an implementation of the mock for
* one call to the mocked function. Can be chained so that multiple function
* calls produce different results.
*/
mockImplementationOnce(
fn: (...args: TArguments) => TReturn
): JestMockFn,
/**
* Accepts a string to use in test result output in place of "jest.fn()" to
* indicate which mock function is being referenced.
*/
mockName(name: string): JestMockFn,
/**
* Just a simple sugar function for returning `this`
*/
mockReturnThis(): void,
/**
* Accepts a value that will be returned whenever the mock function is called.
*/
mockReturnValue(value: TReturn): JestMockFn,
/**
* Sugar for only returning a value once inside your mock
*/
mockReturnValueOnce(value: TReturn): JestMockFn,
/**
* Sugar for jest.fn().mockImplementation(() => Promise.resolve(value))
*/
mockResolvedValue(value: TReturn): JestMockFn>,
/**
* Sugar for jest.fn().mockImplementationOnce(() => Promise.resolve(value))
*/
mockResolvedValueOnce(
value: TReturn
): JestMockFn>,
/**
* Sugar for jest.fn().mockImplementation(() => Promise.reject(value))
*/
mockRejectedValue(value: TReturn): JestMockFn>,
/**
* Sugar for jest.fn().mockImplementationOnce(() => Promise.reject(value))
*/
mockRejectedValueOnce(value: TReturn): JestMockFn>,
...
};
type JestAsymmetricEqualityType = { /**
* A custom Jasmine equality tester
*/
asymmetricMatch(value: mixed): boolean, ... };
type JestCallsType = {
allArgs(): mixed,
all(): mixed,
any(): boolean,
count(): number,
first(): mixed,
mostRecent(): mixed,
reset(): void,
...
};
type JestClockType = {
install(): void,
mockDate(date: Date): void,
tick(milliseconds?: number): void,
uninstall(): void,
...
};
type JestMatcherResult = {
message?: string | (() => string),
pass: boolean,
...
};
type JestMatcher = (
received: any,
...actual: Array
) => JestMatcherResult | Promise;
type JestPromiseType = {
/**
* Use rejects to unwrap the reason of a rejected promise so any other
* matcher can be chained. If the promise is fulfilled the assertion fails.
*/
rejects: JestExpectType,
/**
* Use resolves to unwrap the value of a fulfilled promise so any other
* matcher can be chained. If the promise is rejected the assertion fails.
*/
resolves: JestExpectType,
...
};
/**
* Jest allows functions and classes to be used as test names in test() and
* describe()
*/
type JestTestName = string | Function;
/**
* Plugin: jest-styled-components
*/
type JestStyledComponentsMatcherValue =
| string
| JestAsymmetricEqualityType
| RegExp
| typeof undefined;
type JestStyledComponentsMatcherOptions = {
media?: string,
modifier?: string,
supports?: string,
...
};
type JestStyledComponentsMatchersType = { toHaveStyleRule(
property: string,
value: JestStyledComponentsMatcherValue,
options?: JestStyledComponentsMatcherOptions
): void, ... };
/**
* Plugin: jest-enzyme
*/
type EnzymeMatchersType = {
// 5.x
toBeEmpty(): void,
toBePresent(): void,
// 6.x
toBeChecked(): void,
toBeDisabled(): void,
toBeEmptyRender(): void,
toContainMatchingElement(selector: string): void,
toContainMatchingElements(n: number, selector: string): void,
toContainExactlyOneMatchingElement(selector: string): void,
toContainReact(element: React$Element): void,
toExist(): void,
toHaveClassName(className: string): void,
toHaveHTML(html: string): void,
toHaveProp: ((propKey: string, propValue?: any) => void) &
((props: {...}) => void),
toHaveRef(refName: string): void,
toHaveState: ((stateKey: string, stateValue?: any) => void) &
((state: {...}) => void),
toHaveStyle: ((styleKey: string, styleValue?: any) => void) &
((style: {...}) => void),
toHaveTagName(tagName: string): void,
toHaveText(text: string): void,
toHaveValue(value: any): void,
toIncludeText(text: string): void,
toMatchElement(
element: React$Element,
options?: {| ignoreProps?: boolean, verbose?: boolean |}
): void,
toMatchSelector(selector: string): void,
// 7.x
toHaveDisplayName(name: string): void,
...
};
// DOM testing library extensions (jest-dom)
// https://github.com/testing-library/jest-dom
type DomTestingLibraryType = {
/**
* @deprecated
*/
toBeInTheDOM(container?: HTMLElement): void,
toBeInTheDocument(): void,
toBeVisible(): void,
toBeEmpty(): void,
toBeDisabled(): void,
toBeEnabled(): void,
toBeInvalid(): void,
toBeRequired(): void,
toBeValid(): void,
toContainElement(element: HTMLElement | null): void,
toContainHTML(htmlText: string): void,
toHaveAttribute(attr: string, value?: any): void,
toHaveClass(...classNames: string[]): void,
toHaveFocus(): void,
toHaveFormValues(expectedValues: { [name: string]: any, ... }): void,
toHaveStyle(css: string): void,
toHaveTextContent(
text: string | RegExp,
options?: { normalizeWhitespace: boolean, ... }
): void,
toHaveValue(value?: string | string[] | number): void,
...
};
// Jest JQuery Matchers: https://github.com/unindented/custom-jquery-matchers
type JestJQueryMatchersType = {
toExist(): void,
toHaveLength(len: number): void,
toHaveId(id: string): void,
toHaveClass(className: string): void,
toHaveTag(tag: string): void,
toHaveAttr(key: string, val?: any): void,
toHaveProp(key: string, val?: any): void,
toHaveText(text: string | RegExp): void,
toHaveData(key: string, val?: any): void,
toHaveValue(val: any): void,
toHaveCss(css: { [key: string]: any, ... }): void,
toBeChecked(): void,
toBeDisabled(): void,
toBeEmpty(): void,
toBeHidden(): void,
toBeSelected(): void,
toBeVisible(): void,
toBeFocused(): void,
toBeInDom(): void,
toBeMatchedBy(sel: string): void,
toHaveDescendant(sel: string): void,
toHaveDescendantWithText(sel: string, text: string | RegExp): void,
...
};
// Jest Extended Matchers: https://github.com/jest-community/jest-extended
type JestExtendedMatchersType = {
/**
* Note: Currently unimplemented
* Passing assertion
*
* @param {String} message
*/
// pass(message: string): void;
/**
* Note: Currently unimplemented
* Failing assertion
*
* @param {String} message
*/
// fail(message: string): void;
/**
* Use .toBeEmpty when checking if a String '', Array [] or Object {} is empty.
*/
toBeEmpty(): void,
/**
* Use .toBeOneOf when checking if a value is a member of a given Array.
* @param {Array.<*>} members
*/
toBeOneOf(members: any[]): void,
/**
* Use `.toBeNil` when checking a value is `null` or `undefined`.
*/
toBeNil(): void,
/**
* Use `.toSatisfy` when you want to use a custom matcher by supplying a predicate function that returns a `Boolean`.
* @param {Function} predicate
*/
toSatisfy(predicate: (n: any) => boolean): void,
/**
* Use `.toBeArray` when checking if a value is an `Array`.
*/
toBeArray(): void,
/**
* Use `.toBeArrayOfSize` when checking if a value is an `Array` of size x.
* @param {Number} x
*/
toBeArrayOfSize(x: number): void,
/**
* Use `.toIncludeAllMembers` when checking if an `Array` contains all of the same members of a given set.
* @param {Array.<*>} members
*/
toIncludeAllMembers(members: any[]): void,
/**
* Use `.toIncludeAnyMembers` when checking if an `Array` contains any of the members of a given set.
* @param {Array.<*>} members
*/
toIncludeAnyMembers(members: any[]): void,
/**
* 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.
* @param {Function} predicate
*/
toSatisfyAll(predicate: (n: any) => boolean): void,
/**
* Use `.toBeBoolean` when checking if a value is a `Boolean`.
*/
toBeBoolean(): void,
/**
* Use `.toBeTrue` when checking a value is equal (===) to `true`.
*/
toBeTrue(): void,
/**
* Use `.toBeFalse` when checking a value is equal (===) to `false`.
*/
toBeFalse(): void,
/**
* Use .toBeDate when checking if a value is a Date.
*/
toBeDate(): void,
/**
* Use `.toBeFunction` when checking if a value is a `Function`.
*/
toBeFunction(): void,
/**
* Use `.toHaveBeenCalledBefore` when checking if a `Mock` was called before another `Mock`.
*
* Note: Required Jest version >22
* 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
*
* @param {Mock} mock
*/
toHaveBeenCalledBefore(mock: JestMockFn): void,
/**
* Use `.toBeNumber` when checking if a value is a `Number`.
*/
toBeNumber(): void,
/**
* Use `.toBeNaN` when checking a value is `NaN`.
*/
toBeNaN(): void,
/**
* Use `.toBeFinite` when checking if a value is a `Number`, not `NaN` or `Infinity`.
*/
toBeFinite(): void,
/**
* Use `.toBePositive` when checking if a value is a positive `Number`.
*/
toBePositive(): void,
/**
* Use `.toBeNegative` when checking if a value is a negative `Number`.
*/
toBeNegative(): void,
/**
* Use `.toBeEven` when checking if a value is an even `Number`.
*/
toBeEven(): void,
/**
* Use `.toBeOdd` when checking if a value is an odd `Number`.
*/
toBeOdd(): void,
/**
* Use `.toBeWithin` when checking if a number is in between the given bounds of: start (inclusive) and end (exclusive).
*
* @param {Number} start
* @param {Number} end
*/
toBeWithin(start: number, end: number): void,
/**
* Use `.toBeObject` when checking if a value is an `Object`.
*/
toBeObject(): void,
/**
* Use `.toContainKey` when checking if an object contains the provided key.
*
* @param {String} key
*/
toContainKey(key: string): void,
/**
* Use `.toContainKeys` when checking if an object has all of the provided keys.
*
* @param {Array.} keys
*/
toContainKeys(keys: string[]): void,
/**
* Use `.toContainAllKeys` when checking if an object only contains all of the provided keys.
*
* @param {Array.} keys
*/
toContainAllKeys(keys: string[]): void,
/**
* Use `.toContainAnyKeys` when checking if an object contains at least one of the provided keys.
*
* @param {Array.} keys
*/
toContainAnyKeys(keys: string[]): void,
/**
* Use `.toContainValue` when checking if an object contains the provided value.
*
* @param {*} value
*/
toContainValue(value: any): void,
/**
* Use `.toContainValues` when checking if an object contains all of the provided values.
*
* @param {Array.<*>} values
*/
toContainValues(values: any[]): void,
/**
* Use `.toContainAllValues` when checking if an object only contains all of the provided values.
*
* @param {Array.<*>} values
*/
toContainAllValues(values: any[]): void,
/**
* Use `.toContainAnyValues` when checking if an object contains at least one of the provided values.
*
* @param {Array.<*>} values
*/
toContainAnyValues(values: any[]): void,
/**
* Use `.toContainEntry` when checking if an object contains the provided entry.
*
* @param {Array.} entry
*/
toContainEntry(entry: [string, string]): void,
/**
* Use `.toContainEntries` when checking if an object contains all of the provided entries.
*
* @param {Array.>} entries
*/
toContainEntries(entries: [string, string][]): void,
/**
* Use `.toContainAllEntries` when checking if an object only contains all of the provided entries.
*
* @param {Array.>} entries
*/
toContainAllEntries(entries: [string, string][]): void,
/**
* Use `.toContainAnyEntries` when checking if an object contains at least one of the provided entries.
*
* @param {Array.>} entries
*/
toContainAnyEntries(entries: [string, string][]): void,
/**
* Use `.toBeExtensible` when checking if an object is extensible.
*/
toBeExtensible(): void,
/**
* Use `.toBeFrozen` when checking if an object is frozen.
*/
toBeFrozen(): void,
/**
* Use `.toBeSealed` when checking if an object is sealed.
*/
toBeSealed(): void,
/**
* Use `.toBeString` when checking if a value is a `String`.
*/
toBeString(): void,
/**
* Use `.toEqualCaseInsensitive` when checking if a string is equal (===) to another ignoring the casing of both strings.
*
* @param {String} string
*/
toEqualCaseInsensitive(string: string): void,
/**
* Use `.toStartWith` when checking if a `String` starts with a given `String` prefix.
*
* @param {String} prefix
*/
toStartWith(prefix: string): void,
/**
* Use `.toEndWith` when checking if a `String` ends with a given `String` suffix.
*
* @param {String} suffix
*/
toEndWith(suffix: string): void,
/**
* Use `.toInclude` when checking if a `String` includes the given `String` substring.
*
* @param {String} substring
*/
toInclude(substring: string): void,
/**
* Use `.toIncludeRepeated` when checking if a `String` includes the given `String` substring the correct number of times.
*
* @param {String} substring
* @param {Number} times
*/
toIncludeRepeated(substring: string, times: number): void,
/**
* Use `.toIncludeMultiple` when checking if a `String` includes all of the given substrings.
*
* @param {Array.} substring
*/
toIncludeMultiple(substring: string[]): void,
...
};
interface JestExpectType {
not: JestExpectType &
EnzymeMatchersType &
DomTestingLibraryType &
JestJQueryMatchersType &
JestStyledComponentsMatchersType &
JestExtendedMatchersType;
/**
* If you have a mock function, you can use .lastCalledWith to test what
* arguments it was last called with.
*/
lastCalledWith(...args: Array): void;
/**
* toBe just checks that a value is what you expect. It uses === to check
* strict equality.
*/
toBe(value: any): void;
/**
* Use .toBeCalledWith to ensure that a mock function was called with
* specific arguments.
*/
toBeCalledWith(...args: Array): void;
/**
* Using exact equality with floating point numbers is a bad idea. Rounding
* means that intuitive things fail.
*/
toBeCloseTo(num: number, delta: any): void;
/**
* Use .toBeDefined to check that a variable is not undefined.
*/
toBeDefined(): void;
/**
* Use .toBeFalsy when you don't care what a value is, you just want to
* ensure a value is false in a boolean context.
*/
toBeFalsy(): void;
/**
* To compare floating point numbers, you can use toBeGreaterThan.
*/
toBeGreaterThan(number: number): void;
/**
* To compare floating point numbers, you can use toBeGreaterThanOrEqual.
*/
toBeGreaterThanOrEqual(number: number): void;
/**
* To compare floating point numbers, you can use toBeLessThan.
*/
toBeLessThan(number: number): void;
/**
* To compare floating point numbers, you can use toBeLessThanOrEqual.
*/
toBeLessThanOrEqual(number: number): void;
/**
* Use .toBeInstanceOf(Class) to check that an object is an instance of a
* class.
*/
toBeInstanceOf(cls: Class<*>): void;
/**
* .toBeNull() is the same as .toBe(null) but the error messages are a bit
* nicer.
*/
toBeNull(): void;
/**
* Use .toBeTruthy when you don't care what a value is, you just want to
* ensure a value is true in a boolean context.
*/
toBeTruthy(): void;
/**
* Use .toBeUndefined to check that a variable is undefined.
*/
toBeUndefined(): void;
/**
* Use .toContain when you want to check that an item is in a list. For
* testing the items in the list, this uses ===, a strict equality check.
*/
toContain(item: any): void;
/**
* Use .toContainEqual when you want to check that an item is in a list. For
* testing the items in the list, this matcher recursively checks the
* equality of all fields, rather than checking for object identity.
*/
toContainEqual(item: any): void;
/**
* Use .toEqual when you want to check that two objects have the same value.
* This matcher recursively checks the equality of all fields, rather than
* checking for object identity.
*/
toEqual(value: any): void;
/**
* Use .toHaveBeenCalled to ensure that a mock function got called.
*/
toHaveBeenCalled(): void;
toBeCalled(): void;
/**
* Use .toHaveBeenCalledTimes to ensure that a mock function got called exact
* number of times.
*/
toHaveBeenCalledTimes(number: number): void;
toBeCalledTimes(number: number): void;
/**
*
*/
toHaveBeenNthCalledWith(nthCall: number, ...args: Array): void;
nthCalledWith(nthCall: number, ...args: Array): void;
/**
*
*/
toHaveReturned(): void;
toReturn(): void;
/**
*
*/
toHaveReturnedTimes(number: number): void;
toReturnTimes(number: number): void;
/**
*
*/
toHaveReturnedWith(value: any): void;
toReturnWith(value: any): void;
/**
*
*/
toHaveLastReturnedWith(value: any): void;
lastReturnedWith(value: any): void;
/**
*
*/
toHaveNthReturnedWith(nthCall: number, value: any): void;
nthReturnedWith(nthCall: number, value: any): void;
/**
* Use .toHaveBeenCalledWith to ensure that a mock function was called with
* specific arguments.
*/
toHaveBeenCalledWith(...args: Array): void;
toBeCalledWith(...args: Array): void;
/**
* Use .toHaveBeenLastCalledWith to ensure that a mock function was last called
* with specific arguments.
*/
toHaveBeenLastCalledWith(...args: Array): void;
lastCalledWith(...args: Array): void;
/**
* Check that an object has a .length property and it is set to a certain
* numeric value.
*/
toHaveLength(number: number): void;
/**
*
*/
toHaveProperty(propPath: string | $ReadOnlyArray, value?: any): void;
/**
* Use .toMatch to check that a string matches a regular expression or string.
*/
toMatch(regexpOrString: RegExp | string): void;
/**
* Use .toMatchObject to check that a javascript object matches a subset of the properties of an object.
*/
toMatchObject(object: Object | Array