Skip to content

Commit

Permalink
chore(batchDelegate): fix property names (#1868)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Aug 2, 2020
1 parent 7c55fbe commit c2c81a3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 43 deletions.
24 changes: 9 additions & 15 deletions packages/batch-delegate/src/createBatchDelegateFn.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import DataLoader from 'dataloader';

import {
CreateBatchDelegateFnOptions,
BatchDelegateOptionsFn,
BatchDelegateFn,
BatchDelegateMapKeysFn,
BatchDelegateMapResultsFn,
} from './types';
import { CreateBatchDelegateFnOptions, BatchDelegateOptionsFn, BatchDelegateFn } from './types';

import { getLoader } from './getLoader';

export function createBatchDelegateFn<K = any, V = any, C = K>(
optionsOrMapKeysFn: CreateBatchDelegateFnOptions | BatchDelegateMapKeysFn,
optionsFn?: BatchDelegateOptionsFn,
optionsOrArgsFromKeys: CreateBatchDelegateFnOptions | ((keys: ReadonlyArray<K>) => Record<string, any>),
lazyOptionsFn?: BatchDelegateOptionsFn,
dataLoaderOptions?: DataLoader.Options<K, V, C>,
mapResultsFn?: BatchDelegateMapResultsFn
valuesFromResults?: (results: any, keys: ReadonlyArray<K>) => Array<V>
): BatchDelegateFn<K> {
return typeof optionsOrMapKeysFn === 'function'
return typeof optionsOrArgsFromKeys === 'function'
? createBatchDelegateFnImpl({
mapKeysFn: optionsOrMapKeysFn,
optionsFn,
argsFromKeys: optionsOrArgsFromKeys,
lazyOptionsFn,
dataLoaderOptions,
mapResultsFn,
valuesFromResults,
})
: createBatchDelegateFnImpl(optionsOrMapKeysFn);
: createBatchDelegateFnImpl(optionsOrArgsFromKeys);
}

function createBatchDelegateFnImpl<K = any>(options: CreateBatchDelegateFnOptions): BatchDelegateFn<K> {
Expand Down
16 changes: 7 additions & 9 deletions packages/batch-delegate/src/getLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ import { BatchDelegateOptions, DataLoaderCache } from './types';
const cache: DataLoaderCache = new WeakMap();

function createBatchFn<K = any>(options: BatchDelegateOptions) {
const mapKeysFn = options.mapKeysFn ?? ((keys: ReadonlyArray<K>) => ({ ids: keys }));
const { mapResultsFn, optionsFn } = options;
const argsFromKeys = options.argsFromKeys ?? ((keys: ReadonlyArray<K>) => ({ ids: keys }));
const { valuesFromResults, lazyOptionsFn } = options;

return async (keys: ReadonlyArray<K>) => {
let results = await delegateToSchema({
const results = await delegateToSchema({
returnType: new GraphQLList(getNamedType(options.info.returnType) as GraphQLOutputType),
args: mapKeysFn(keys),
...(optionsFn != null ? optionsFn(options) : options),
args: argsFromKeys(keys),
...(lazyOptionsFn == null ? options : lazyOptionsFn(options)),
});

if (mapResultsFn != null) {
results = mapResultsFn(results, keys);
}
const values = valuesFromResults == null ? results : valuesFromResults(results, keys);

return Array.isArray(results) ? results : keys.map(() => results);
return Array.isArray(values) ? values : keys.map(() => values);
};
}

Expand Down
16 changes: 6 additions & 10 deletions packages/batch-delegate/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,19 @@ export type BatchDelegateOptionsFn<TContext = Record<string, any>, K = any> = (
batchDelegateOptions: BatchDelegateOptions<TContext, K>
) => IDelegateToSchemaOptions<TContext>;

export type BatchDelegateMapKeysFn<K = any> = (keys: ReadonlyArray<K>) => Record<string, any>;

export type BatchDelegateMapResultsFn<K = any, V = any> = (results: any, keys: ReadonlyArray<K>) => Array<V>;

export interface BatchDelegateOptions<TContext = Record<string, any>, K = any, V = any, C = K>
extends Omit<IDelegateToSchemaOptions<TContext>, 'args'> {
dataLoaderOptions?: DataLoader.Options<K, V, C>;
key: K;
mapKeysFn?: BatchDelegateMapKeysFn;
mapResultsFn?: BatchDelegateMapResultsFn;
optionsFn?: BatchDelegateOptionsFn;
argsFromKeys?: (keys: ReadonlyArray<K>) => Record<string, any>;
valuesFromResults?: (results: any, keys: ReadonlyArray<K>) => Array<V>;
lazyOptionsFn?: BatchDelegateOptionsFn;
}

export interface CreateBatchDelegateFnOptions<TContext = Record<string, any>, K = any, V = any, C = K>
extends Partial<Omit<IDelegateToSchemaOptions<TContext>, 'args' | 'info'>> {
dataLoaderOptions?: DataLoader.Options<K, V, C>;
mapKeysFn?: BatchDelegateMapKeysFn;
mapResultsFn?: BatchDelegateMapResultsFn;
optionsFn?: BatchDelegateOptionsFn;
argsFromKeys?: (keys: ReadonlyArray<K>) => Record<string, any>;
valuesFromResults?: (results: any, keys: ReadonlyArray<K>) => Array<V>;
lazyOptionsFn?: (batchDelegateOptions: BatchDelegateOptions<TContext, K>) => IDelegateToSchemaOptions<TContext>;
}
2 changes: 1 addition & 1 deletion packages/batch-delegate/tests/basic.example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('batch delegation within basic stitching example', () => {
operation: 'query',
fieldName: 'usersByIds',
key: chirp.chirpedAtUserId,
mapKeysFn: (ids) => ({ ids }),
argsFromKeys: (ids) => ({ ids }),
context,
info,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/delegate/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ export interface MergedTypeConfig<K = any, V = any> {
fieldName?: string;
args?: (originalResult: any) => Record<string, any>;
key?: (originalResult: any) => K;
mapKeysFn?: (keys: ReadonlyArray<K>) => Record<string, any>;
mapResultsFn?: (results: any, keys: ReadonlyArray<K>) => Array<V>;
argsFromKeys?: (keys: ReadonlyArray<K>) => Record<string, any>;
valuesFromResults?: (results: any, keys: ReadonlyArray<K>) => Array<V>;
}

export type MergedTypeResolver = (
Expand Down
4 changes: 2 additions & 2 deletions packages/stitch/src/stitchingInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ function createMergedTypes(
operation: 'query',
fieldName: mergedTypeConfig.fieldName,
key: mergedTypeConfig.key(originalResult),
mapKeysFn: mergedTypeConfig.mapKeysFn ?? mergedTypeConfig.args,
mapResultsFn: mergedTypeConfig.mapResultsFn,
argsFromKeys: mergedTypeConfig.argsFromKeys ?? mergedTypeConfig.args,
valuesFromResults: mergedTypeConfig.valuesFromResults,
selectionSet,
context,
info,
Expand Down
8 changes: 4 additions & 4 deletions website/docs/schema-stitching.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ const schema = stitchSchemas({
operation: 'query',
fieldName: 'usersByIds',
key: chirp.chirpedAtUserId,
mapKeysFn: (ids) => ({ ids }),
argsFromKeys: (ids) => ({ ids }),
context,
info,
});
Expand Down Expand Up @@ -513,8 +513,8 @@ export interface MergedTypeConfig {
fieldName?: string;
args?: (originalResult: any) => Record<string, any>;
key?: (originalResult: any) => K;
mapKeysFn?: (keys: ReadonlyArray<K>) => Record<string, any>;
mapResultsFn?: (results: any, keys: ReadonlyArray<K>) => Array<V>;
argsFromKeys?: (keys: ReadonlyArray<K>) => Record<string, any>;
valuesFromResults?: (results: any, keys: ReadonlyArray<K>) => Array<V>;
}

export type MergedTypeResolver = (
Expand All @@ -530,7 +530,7 @@ Type merging simply merges types with the same name, but is smart enough to appl

All merged types returned by any subschema will delegate as necessary to subschemas also implementing the type, using the provided `resolve` function of type `MergedTypeResolver`.

You can also use batch delegation instead of simple delegation by delegating to a root field returning a list and using the `key`, `mapKeysFn`, and `mapResultsFn` properties. See the [batch delegation](#batch-delegation) for more details.
You can also use batch delegation instead of simple delegation by delegating to a root field returning a list and using the `key`, `argsFromKeys`, and `valuesFromResults` properties. See the [batch delegation](#batch-delegation) for more details.

The simplified magic above happens because if left unspecified, we provide a default type-merging resolver for you, which uses the other `MergedTypeConfig` options (for simple delegation), as follows:

Expand Down

0 comments on commit c2c81a3

Please sign in to comment.