-
-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add key option within type merging config to enable batch loading for lists. This minimal version of batching uses cached dataLoaders to create a separate batch for each list rather than for every similar query within the resolution tree. This is because a new dataloader is created for every new info.fieldNodes object, which is memoized upstream by graphql-js within resolution of a given list to allow the loader to be used for items of that list. A future version could provide an option to batch by similar target fieldName/selectionSet, but this version may hit the sweet spot in terms of code complexity and batching behavior. see: #1710
- Loading branch information
Showing
7 changed files
with
162 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@graphql-tools/batchDelegate", | ||
"version": "6.0.12", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
"repository": "git@github.com:ardatan/graphql-tools.git", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.esm.js", | ||
"typings": "dist/index.d.ts", | ||
"typescript": { | ||
"definition": "dist/index.d.ts" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^14.0.0 || ^15.0.0" | ||
}, | ||
"buildOptions": { | ||
"input": "./src/index.ts" | ||
}, | ||
"dependencies": { | ||
"@graphql-tools/delegate": "6.0.12", | ||
"dataloader": "2.0.0", | ||
"tslib": "~2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@graphql-tools/schema": "6.0.12", | ||
"@graphql-tools/stitch": "6.0.12", | ||
"@graphql-tools/utils": "6.0.12" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"directory": "dist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { FieldNode, getNamedType, GraphQLOutputType, GraphQLList } from 'graphql'; | ||
|
||
import DataLoader from 'dataloader'; | ||
|
||
import { delegateToSchema } from '@graphql-tools/delegate'; | ||
|
||
import { BatchDelegateOptionsFn, BatchDelegateFn, BatchDelegateOptions } from './types'; | ||
|
||
export function createBatchDelegateFn<K = any, V = any, C = K>( | ||
argFn: (args: ReadonlyArray<K>) => Record<string, any>, | ||
batchDelegateOptionsFn: BatchDelegateOptionsFn, | ||
dataLoaderOptions?: DataLoader.Options<K, V, C> | ||
): BatchDelegateFn<K> { | ||
let cache: WeakMap<ReadonlyArray<FieldNode>, DataLoader<K, V, C>>; | ||
|
||
function createBatchFn(options: BatchDelegateOptions) { | ||
return async (keys: ReadonlyArray<K>) => { | ||
const results = await delegateToSchema({ | ||
returnType: new GraphQLList(getNamedType(options.info.returnType) as GraphQLOutputType), | ||
args: argFn(keys), | ||
...batchDelegateOptionsFn(options), | ||
}); | ||
return Array.isArray(results) ? results : keys.map(() => results); | ||
}; | ||
} | ||
|
||
function getLoader(options: BatchDelegateOptions) { | ||
if (!cache) { | ||
cache = new WeakMap(); | ||
const batchFn = createBatchFn(options); | ||
const newValue = new DataLoader<K, V, C>(keys => batchFn(keys), dataLoaderOptions); | ||
cache.set(options.info.fieldNodes, newValue); | ||
return newValue; | ||
} | ||
|
||
const cachedValue = cache.get(options.info.fieldNodes); | ||
if (cachedValue === undefined) { | ||
const batchFn = createBatchFn(options); | ||
const newValue = new DataLoader<K, V, C>(keys => batchFn(keys), dataLoaderOptions); | ||
cache.set(options.info.fieldNodes, newValue); | ||
return newValue; | ||
} | ||
|
||
return cachedValue; | ||
} | ||
|
||
return options => { | ||
const loader = getLoader(options); | ||
return loader.load(options.key); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { createBatchDelegateFn } from './createBatchDelegateFn'; | ||
|
||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { IDelegateToSchemaOptions } from '@graphql-tools/delegate'; | ||
|
||
export type BatchDelegateFn<TContext = Record<string, any>, K = any> = ( | ||
batchDelegateOptions: BatchDelegateOptions<TContext, K> | ||
) => any; | ||
|
||
export type BatchDelegateOptionsFn<TContext = Record<string, any>, K = any> = ( | ||
batchDelegateOptions: BatchDelegateOptions<TContext, K> | ||
) => IDelegateToSchemaOptions<TContext>; | ||
|
||
export interface BatchDelegateOptions<TContext = Record<string, any>, K = any> | ||
extends Omit<IDelegateToSchemaOptions<TContext>, 'args'> { | ||
key: K; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters