Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ and can make a more efficient set of HTTP requests to the underlying resource.
```yaml
resources:
getPeople:
docsLink: https://swapi.co/documentation#people
docsLink: https://swapi.dev/documentation#people
isBatchResource: true
batchKey: people_ids
newKey: person_id
getPlanets:
docsLink: https://swapi.co/documentation#planets
docsLink: https://swapi.dev/documentation#planets
isBatchResource: true
batchKey: planet_ids
newKey: planet_id
Expand All @@ -85,7 +85,7 @@ and can make a more efficient set of HTTP requests to the underlying resource.
```js
import getLoaders from './__codegen__/swapi-loaders';

// StarWarsAPI is a clientlib containing fetch calls to swapi.co
// StarWarsAPI is a clientlib containing fetch calls to swapi.dev
// getLoaders is the function that dataloader-codegen generates for us
const swapiLoaders = getLoaders(StarWarsAPI);

Expand Down
69 changes: 68 additions & 1 deletion __tests__/implementation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ test('middleware can transform the request args and the resource response', asyn
});
});

test('returning custom errors from error handler is supported', async () => {
test('[isBatchResource: true] returning custom errors from error handler is supported', async () => {
class MyCustomError extends Error {
constructor(...args) {
super(...args);
Expand Down Expand Up @@ -1020,6 +1020,73 @@ test('returning custom errors from error handler is supported', async () => {
});
});

test('[isBatchResource: false] returning custom errors from error handler is supported', async () => {
class MyCustomError extends Error {
constructor(...args) {
super(...args);
this.name = this.constructor.name;
this.foo = 'bar';
Error.captureStackTrace(this, MyCustomError);
}
}

function errorHandler(resourcePath, error) {
expect(resourcePath).toEqual(['foo']);
expect(error.message).toBe('yikes');
return new MyCustomError('hello from custom error object');
}

const config = {
resources: {
foo: {
isBatchResource: false,
docsLink: 'example.com/docs/bar',
},
},
};

const resources = {
foo: ({ foo_id, include_extra_info }) => {
if ([1, 3].includes(foo_id)) {
expect(include_extra_info).toBe(false);
throw new Error('yikes');
}

if ([2, 4, 5].includes(foo_id)) {
expect(include_extra_info).toBe(true);
return Promise.resolve({
foo_id,
foo_value: 'greetings',
extra_stuff: 'lorem ipsum',
});
}
},
};

await createDataLoaders(config, async getLoaders => {
const loaders = getLoaders(resources, { errorHandler });

const results = await loaders.foo.loadMany([
{ foo_id: 1, include_extra_info: false },
{ foo_id: 2, include_extra_info: true },
{ foo_id: 3, include_extra_info: false },
{ foo_id: 4, include_extra_info: true },
{ foo_id: 5, include_extra_info: true },
]);

expect(results).toMatchObject([
expect.toBeError(/hello from custom error object/, 'MyCustomError'),
{ foo_id: 2, foo_value: 'greetings', extra_stuff: 'lorem ipsum' },
expect.toBeError(/hello from custom error object/, 'MyCustomError'),
{ foo_id: 4, foo_value: 'greetings', extra_stuff: 'lorem ipsum' },
{ foo_id: 5, foo_value: 'greetings', extra_stuff: 'lorem ipsum' },
]);

expect(results[0]).toHaveProperty('foo', 'bar');
expect(results[2]).toHaveProperty('foo', 'bar');
});
});

test('bail if errorHandler does not return an error', async () => {
class MyCustomError extends Error {
constructor(...args) {
Expand Down
1 change: 1 addition & 0 deletions examples/swapi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ swapi-loaders.js:
flow-typed: node_modules
yarn flow-typed install

.PHONY: build
build: node_modules
yarn babel *.js -d build
4 changes: 2 additions & 2 deletions examples/swapi/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# dataloader-codegen Example: Star Wars API (SWAPI)

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

## Try it out locally!

Expand Down Expand Up @@ -28,5 +28,5 @@ $ node build/swapi-server.js
## File Layout:

- `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).
- `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.
- `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.
- `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.
5 changes: 4 additions & 1 deletion examples/swapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
"@babel/cli": "^7.8.4",
"@babel/node": "^7.7.0",
"@babel/preset-flow": "^7.0.0",
"flow-bin": "0.122.0",
"flow-bin": "0.123.0",
"flow-typed": "^2.6.2"
},
"dependencies": {
"dataloader": "^2.0.0",
"graphql": "15.0.0",
"node-fetch": "^2.6.0"
},
"engines": {
"node": ">=10"
}
}
Loading