Skip to content

Commit 97a73bb

Browse files
authored
Merge pull request #57 from Yelp/fix_error_handling_non_batch_resources
Add resource middleware to non batch resources
2 parents 5aa5db6 + 2ae5b98 commit 97a73bb

File tree

11 files changed

+403
-212
lines changed

11 files changed

+403
-212
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ and can make a more efficient set of HTTP requests to the underlying resource.
5858
```yaml
5959
resources:
6060
getPeople:
61-
docsLink: https://swapi.co/documentation#people
61+
docsLink: https://swapi.dev/documentation#people
6262
isBatchResource: true
6363
batchKey: people_ids
6464
newKey: person_id
6565
getPlanets:
66-
docsLink: https://swapi.co/documentation#planets
66+
docsLink: https://swapi.dev/documentation#planets
6767
isBatchResource: true
6868
batchKey: planet_ids
6969
newKey: planet_id
@@ -85,7 +85,7 @@ and can make a more efficient set of HTTP requests to the underlying resource.
8585
```js
8686
import getLoaders from './__codegen__/swapi-loaders';
8787
88-
// StarWarsAPI is a clientlib containing fetch calls to swapi.co
88+
// StarWarsAPI is a clientlib containing fetch calls to swapi.dev
8989
// getLoaders is the function that dataloader-codegen generates for us
9090
const swapiLoaders = getLoaders(StarWarsAPI);
9191

__tests__/implementation.test.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ test('middleware can transform the request args and the resource response', asyn
939939
});
940940
});
941941

942-
test('returning custom errors from error handler is supported', async () => {
942+
test('[isBatchResource: true] returning custom errors from error handler is supported', async () => {
943943
class MyCustomError extends Error {
944944
constructor(...args) {
945945
super(...args);
@@ -1020,6 +1020,73 @@ test('returning custom errors from error handler is supported', async () => {
10201020
});
10211021
});
10221022

1023+
test('[isBatchResource: false] returning custom errors from error handler is supported', async () => {
1024+
class MyCustomError extends Error {
1025+
constructor(...args) {
1026+
super(...args);
1027+
this.name = this.constructor.name;
1028+
this.foo = 'bar';
1029+
Error.captureStackTrace(this, MyCustomError);
1030+
}
1031+
}
1032+
1033+
function errorHandler(resourcePath, error) {
1034+
expect(resourcePath).toEqual(['foo']);
1035+
expect(error.message).toBe('yikes');
1036+
return new MyCustomError('hello from custom error object');
1037+
}
1038+
1039+
const config = {
1040+
resources: {
1041+
foo: {
1042+
isBatchResource: false,
1043+
docsLink: 'example.com/docs/bar',
1044+
},
1045+
},
1046+
};
1047+
1048+
const resources = {
1049+
foo: ({ foo_id, include_extra_info }) => {
1050+
if ([1, 3].includes(foo_id)) {
1051+
expect(include_extra_info).toBe(false);
1052+
throw new Error('yikes');
1053+
}
1054+
1055+
if ([2, 4, 5].includes(foo_id)) {
1056+
expect(include_extra_info).toBe(true);
1057+
return Promise.resolve({
1058+
foo_id,
1059+
foo_value: 'greetings',
1060+
extra_stuff: 'lorem ipsum',
1061+
});
1062+
}
1063+
},
1064+
};
1065+
1066+
await createDataLoaders(config, async getLoaders => {
1067+
const loaders = getLoaders(resources, { errorHandler });
1068+
1069+
const results = await loaders.foo.loadMany([
1070+
{ foo_id: 1, include_extra_info: false },
1071+
{ foo_id: 2, include_extra_info: true },
1072+
{ foo_id: 3, include_extra_info: false },
1073+
{ foo_id: 4, include_extra_info: true },
1074+
{ foo_id: 5, include_extra_info: true },
1075+
]);
1076+
1077+
expect(results).toMatchObject([
1078+
expect.toBeError(/hello from custom error object/, 'MyCustomError'),
1079+
{ foo_id: 2, foo_value: 'greetings', extra_stuff: 'lorem ipsum' },
1080+
expect.toBeError(/hello from custom error object/, 'MyCustomError'),
1081+
{ foo_id: 4, foo_value: 'greetings', extra_stuff: 'lorem ipsum' },
1082+
{ foo_id: 5, foo_value: 'greetings', extra_stuff: 'lorem ipsum' },
1083+
]);
1084+
1085+
expect(results[0]).toHaveProperty('foo', 'bar');
1086+
expect(results[2]).toHaveProperty('foo', 'bar');
1087+
});
1088+
});
1089+
10231090
test('bail if errorHandler does not return an error', async () => {
10241091
class MyCustomError extends Error {
10251092
constructor(...args) {

examples/swapi/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ swapi-loaders.js:
88
flow-typed: node_modules
99
yarn flow-typed install
1010

11+
.PHONY: build
1112
build: node_modules
1213
yarn babel *.js -d build

examples/swapi/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# dataloader-codegen Example: Star Wars API (SWAPI)
22

3-
Shows an example of a GraphQL Server using dataloader-codegen. Prints data from https://swapi.co/.
3+
Shows an example of a GraphQL Server using dataloader-codegen. Prints data from https://swapi.dev.
44

55
## Try it out locally!
66

@@ -28,5 +28,5 @@ $ node build/swapi-server.js
2828
## File Layout:
2929

3030
- `swapi-loaders.js`: An autogenerated file by dataloader-codegen. Contains the codegen'd dataloaders. (Checked in to git so folks can easily see an example of the generated code).
31-
- `swapi.js`: A set of functions to fetch data from https://swapi.co/. This is analogous to a library generated by openapi-generator/swagger-codegen.
31+
- `swapi.js`: A set of functions to fetch data from https://swapi.dev/. This is analogous to a library generated by openapi-generator/swagger-codegen.
3232
- `swapi-server.js`: The dummy GraphQL server! This imports the dataloaders from swapi-loaders.js. At present, it just prints the result of a query to stdout.

examples/swapi/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
"@babel/cli": "^7.8.4",
44
"@babel/node": "^7.7.0",
55
"@babel/preset-flow": "^7.0.0",
6-
"flow-bin": "0.122.0",
6+
"flow-bin": "0.123.0",
77
"flow-typed": "^2.6.2"
88
},
99
"dependencies": {
1010
"dataloader": "^2.0.0",
1111
"graphql": "15.0.0",
1212
"node-fetch": "^2.6.0"
13+
},
14+
"engines": {
15+
"node": ">=10"
1316
}
1417
}

0 commit comments

Comments
 (0)