|
| 1 | +/** |
| 2 | + * Copyright (c) 2019-present, GraphQL Foundation |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * DataLoader creates a public API for loading data from a particular |
| 10 | + * data back-end with unique keys such as the id column of a SQL table |
| 11 | + * or document name in a MongoDB database, given a batch loading function. |
| 12 | + * |
| 13 | + * Each DataLoader instance contains a unique memoized cache. Use caution |
| 14 | + * when used in long-lived applications or those which serve many users |
| 15 | + * with different access permissions and consider creating a new instance |
| 16 | + * per web request. |
| 17 | + */ |
| 18 | +declare class DataLoader<K, V, C = K> { |
| 19 | + constructor( |
| 20 | + batchLoadFn: DataLoader.BatchLoadFn<K, V>, |
| 21 | + options?: DataLoader.Options<K, V, C>, |
| 22 | + ); |
| 23 | + |
| 24 | + /** |
| 25 | + * Loads a key, returning a `Promise` for the value represented by that key. |
| 26 | + */ |
| 27 | + load(key: K): Promise<V>; |
| 28 | + |
| 29 | + /** |
| 30 | + * Loads multiple keys, promising an array of values: |
| 31 | + * |
| 32 | + * var [ a, b ] = await myLoader.loadMany([ 'a', 'b' ]); |
| 33 | + * |
| 34 | + * This is equivalent to the more verbose: |
| 35 | + * |
| 36 | + * var [ a, b ] = await Promise.all([ |
| 37 | + * myLoader.load('a'), |
| 38 | + * myLoader.load('b') |
| 39 | + * ]); |
| 40 | + * |
| 41 | + */ |
| 42 | + loadMany(keys: ArrayLike<K>): Promise<Array<V | Error>>; |
| 43 | + |
| 44 | + /** |
| 45 | + * Clears the value at `key` from the cache, if it exists. Returns itself for |
| 46 | + * method chaining. |
| 47 | + */ |
| 48 | + clear(key: K): this; |
| 49 | + |
| 50 | + /** |
| 51 | + * Clears the entire cache. To be used when some event results in unknown |
| 52 | + * invalidations across this particular `DataLoader`. Returns itself for |
| 53 | + * method chaining. |
| 54 | + */ |
| 55 | + clearAll(): this; |
| 56 | + |
| 57 | + /** |
| 58 | + * Adds the provided key and value to the cache. If the key already exists, no |
| 59 | + * change is made. Returns itself for method chaining. |
| 60 | + */ |
| 61 | + prime(key: K, value: V | PromiseLike<V> | Error): this; |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * The name given to this `DataLoader` instance. Useful for APM tools. |
| 66 | + * |
| 67 | + * Is `null` if not set in the constructor. |
| 68 | + */ |
| 69 | + name: string | null; |
| 70 | +} |
| 71 | + |
| 72 | +declare namespace DataLoader { |
| 73 | + // If a custom cache is provided, it must be of this type (a subset of ES6 Map). |
| 74 | + export type CacheMap<K, V> = { |
| 75 | + get(key: K): V | void; |
| 76 | + set(key: K, value: V): any; |
| 77 | + delete(key: K): any; |
| 78 | + clear(): any; |
| 79 | + }; |
| 80 | + |
| 81 | + // A Function, which when given an Array of keys, returns a Promise of an Array |
| 82 | + // of values or Errors. |
| 83 | + export type BatchLoadFn<K, V> = ( |
| 84 | + keys: ReadonlyArray<K>, |
| 85 | + ) => PromiseLike<ArrayLike<V | Error>>; |
| 86 | + |
| 87 | + // Optionally turn off batching or caching or provide a cache key function or a |
| 88 | + // custom cache instance. |
| 89 | + export type Options<K, V, C = K> = { |
| 90 | + /** |
| 91 | + * Default `true`. Set to `false` to disable batching, invoking |
| 92 | + * `batchLoadFn` with a single load key. This is equivalent to setting |
| 93 | + * `maxBatchSize` to `1`. |
| 94 | + */ |
| 95 | + batch?: boolean; |
| 96 | + |
| 97 | + /** |
| 98 | + * Default `Infinity`. Limits the number of items that get passed in to the |
| 99 | + * `batchLoadFn`. May be set to `1` to disable batching. |
| 100 | + */ |
| 101 | + maxBatchSize?: number; |
| 102 | + |
| 103 | + /** |
| 104 | + * Default see https://github.com/graphql/dataloader#batch-scheduling. |
| 105 | + * A function to schedule the later execution of a batch. The function is |
| 106 | + * expected to call the provided callback in the immediate future. |
| 107 | + */ |
| 108 | + batchScheduleFn?: (callback: () => void) => void; |
| 109 | + |
| 110 | + /** |
| 111 | + * Default `true`. Set to `false` to disable memoization caching, creating a |
| 112 | + * new Promise and new key in the `batchLoadFn` for every load of the same |
| 113 | + * key. This is equivalent to setting `cacheMap` to `null`. |
| 114 | + */ |
| 115 | + cache?: boolean; |
| 116 | + |
| 117 | + /** |
| 118 | + * Default `key => key`. Produces cache key for a given load key. Useful |
| 119 | + * when keys are objects and two objects should be considered equivalent. |
| 120 | + */ |
| 121 | + cacheKeyFn?: (key: K) => C; |
| 122 | + |
| 123 | + /** |
| 124 | + * Default `new Map()`. Instance of `Map` (or an object with a similar API) |
| 125 | + * to be used as cache. May be set to `null` to disable caching. |
| 126 | + */ |
| 127 | + cacheMap?: CacheMap<C, Promise<V>> | null; |
| 128 | + |
| 129 | + /** |
| 130 | + * The name given to this `DataLoader` instance. Useful for APM tools. |
| 131 | + * |
| 132 | + * Is `null` if not set in the constructor. |
| 133 | + */ |
| 134 | + name?: string | null; |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +export = DataLoader; |
0 commit comments