Skip to content

Commit 0aaa906

Browse files
authored
Merge pull request Yelp#265 from kkellyy/unique_set_batchkeys
Add configuration option for batch keys that are a Set rather than $ReadOnlyArray
2 parents 4cb4dd0 + 0b23092 commit 0aaa906

File tree

10 files changed

+521
-18
lines changed

10 files changed

+521
-18
lines changed

API_DOCS.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ resources:
9393
nestedPath: ?string (can only use if isBatchResource=true)
9494
commaSeparatedBatchKey: ?string (can only use if isBatchResource=true)
9595
isResponseDictionary: ?boolean (can only use if isBatchResource=true)
96+
isBatchKeyASet: ?boolean (can only use if isBatchResource=true)
9697
9798
typings:
9899
language: flow
@@ -113,16 +114,17 @@ Describes the shape and behaviour of the resources object you will pass to `getL
113114

114115
#### `resources` Parameters
115116

116-
| Key | Value Description |
117-
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
118-
| `isBatchResource` | Is this a batch resource? (Can you pass it a list of keys and get a list of results back?) |
119-
| `docsLink` | The URL for the documentation of the resource. Useful for others to verify information is correct, and may be used in stack traces. |
120-
| `batchKey` | The argument to the resource that represents the list of entities we want to fetch. (e.g. 'user_ids') |
121-
| `newKey` | The argument we'll replace the batchKey with - should be a singular version of the `batchKey` (e.g. 'user_id') |
122-
| `reorderResultsByKey` | (Optional) If the resource itself does not guarantee ordering, use this to specify which key in the response objects corresponds to an element in `batchKey`. Transforms and re-order the response to the same order as requested from the DataLoaders. |
123-
| `nestedPath` | (Optional) If the resource returns the list of results in a nested path (e.g. `{ results: [ 1, 2, 3 ] }`), this tells the DataLoader where in the response to find the results. (e.g. 'results'). |
124-
| `commaSeparatedBatchKey` | (Optional) Set to true if the interface of the resource takes the batch key as a comma separated list (rather than an array of IDs, as is more common). Default: false |
125-
| `isResponseDictionary` | (Optional) Set to true if the batch resource returns the results as a dictionary with key mapped to values (instead of a list of items). If this option is supplied `reorderResultsByKey` should not be. Default: false |
117+
| Key | Value Description |
118+
| ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
119+
| `isBatchResource` | Is this a batch resource? (Can you pass it a list of keys and get a list of results back?) |
120+
| `docsLink` | The URL for the documentation of the resource. Useful for others to verify information is correct, and may be used in stack traces. |
121+
| `batchKey` | The argument to the resource that represents the list of entities we want to fetch. (e.g. 'user_ids') |
122+
| `newKey` | The argument we'll replace the batchKey with - should be a singular version of the `batchKey` (e.g. 'user_id') |
123+
| `reorderResultsByKey` | (Optional) If the resource itself does not guarantee ordering, use this to specify which key in the response objects corresponds to an element in `batchKey`. Transforms and re-order the response to the same order as requested from the DataLoaders. |
124+
| `nestedPath` | (Optional) If the resource returns the list of results in a nested path (e.g. `{ results: [ 1, 2, 3 ] }`), this tells the DataLoader where in the response to find the results. (e.g. 'results'). |
125+
| `commaSeparatedBatchKey` | (Optional) Set to true if the interface of the resource takes the batch key as a comma separated list (rather than an array of IDs, as is more common). Default: false |
126+
| `isResponseDictionary` | (Optional) Set to true if the batch resource returns the results as a dictionary with key mapped to values (instead of a list of items). If this option is supplied `reorderResultsByKey` should not be. Default: false |
127+
| `isBatchKeyASet` | (Optional) Set to true if the interface of the resource takes the batch key as a set (rather than an array). For example, when using a generated clientlib based on swagger where `uniqueItems: true` is set for the batchKey parameter. Default: false. |
126128
127129
### `typings`
128130

__tests__/genTypeFlow.test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
import { getResourceTypeReference } from '../src/genTypeFlow';
1+
import { getResourceTypeReference, getNewKeyTypeFromBatchKeySetType } from '../src/genTypeFlow';
22

33
it('getResourceTypeReference converts a resource path to a valid reference', () => {
44
expect(getResourceTypeReference(null, ['foo', 'bar', 'baz'])).toBe(
55
"$PropertyType<$PropertyType<$PropertyType<ResourcesType, 'foo'>, 'bar'>, 'baz'>",
66
);
77
});
8+
9+
it('getNewKeyTypeFromBatchKeySetType returns a newKey type with a valid value', () => {
10+
expect(
11+
getNewKeyTypeFromBatchKeySetType(
12+
'bKey',
13+
"$PropertyType<$PropertyType<$PropertyType<ResourcesType, 'foo'>, 'bar'>, 'baz'>",
14+
),
15+
).toBe(`\
16+
$Call<
17+
ExtractArg,
18+
[$PropertyType<$PropertyType<$PropertyType<$PropertyType<$PropertyType<ResourcesType, 'foo'>, 'bar'>, 'baz'>, 'bKey'>, 'has'>]
19+
>`);
20+
});

__tests__/implementation.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,59 @@ test('batch endpoint with isResponseDictionary handles a response that returns a
843843
});
844844
});
845845

846+
test('batch endpoint with isBatchKeyASet handles a response', async () => {
847+
const config = {
848+
resources: {
849+
foo: {
850+
isBatchResource: true,
851+
docsLink: 'example.com/docs/bar',
852+
batchKey: 'foo_ids',
853+
newKey: 'foo_id',
854+
isBatchKeyASet: true,
855+
},
856+
},
857+
};
858+
859+
const resources = {
860+
foo: ({ foo_ids, include_extra_info }) => {
861+
if (_.isEqual(foo_ids, [1, 2])) {
862+
expect(include_extra_info).toBe(false);
863+
return Promise.resolve([
864+
{ foo_id: 1, foo_value: 'hello' },
865+
{ foo_id: 2, foo_value: 'world' },
866+
]);
867+
}
868+
869+
if (_.isEqual(foo_ids, [3])) {
870+
expect(include_extra_info).toBe(true);
871+
return Promise.resolve([
872+
{
873+
foo_id: 3,
874+
foo_value: 'greetings',
875+
extra_stuff: 'lorem ipsum',
876+
},
877+
]);
878+
}
879+
},
880+
};
881+
882+
await createDataLoaders(config, async (getLoaders) => {
883+
const loaders = getLoaders(resources);
884+
885+
const results = await loaders.foo.loadMany([
886+
{ foo_id: 1, include_extra_info: false },
887+
{ foo_id: 2, include_extra_info: false },
888+
{ foo_id: 3, include_extra_info: true },
889+
]);
890+
891+
expect(results).toEqual([
892+
{ foo_id: 1, foo_value: 'hello' },
893+
{ foo_id: 2, foo_value: 'world' },
894+
{ foo_id: 3, foo_value: 'greetings', extra_stuff: 'lorem ipsum' },
895+
]);
896+
});
897+
});
898+
846899
test('batch endpoint with isResponseDictionary handles a response that returns a dictionary, with a missing item', async () => {
847900
const config = {
848901
resources: {

0 commit comments

Comments
 (0)