diff --git a/README.md b/README.md index d5ecfc6..068d3d6 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # 🤖 dataloader-codegen [![npm](https://img.shields.io/npm/v/dataloader-codegen.svg)](https://yarn.pm/dataloader-codegen) -[![Build Status](https://travis-ci.org/Yelp/dataloader-codegen.svg?branch=master)](https://travis-ci.org/Yelp/dataloader-codegen) +[![Build Status](https://api.travis-ci.com/Yelp/dataloader-codegen.svg?branch=master)](https://travis-ci.com/github/Yelp/dataloader-codegen) dataloader-codegen is an opinionated JavaScript library for automagically generating [DataLoaders](https://github.com/graphql/dataloader) over a set of resources (e.g. HTTP endpoints), with a predictable interface, and maintains type safety. diff --git a/src/codegen.ts b/src/codegen.ts index c36d1ca..68adf2c 100644 --- a/src/codegen.ts +++ b/src/codegen.ts @@ -56,6 +56,7 @@ export default function codegen( import invariant from 'assert'; import DataLoader from 'dataloader'; import { + BatchItemNotFoundError, CaughtResourceError, cacheKeyOptions, partitionItems, diff --git a/src/implementation.ts b/src/implementation.ts index 442804e..70c10b0 100644 --- a/src/implementation.ts +++ b/src/implementation.ts @@ -1,10 +1,7 @@ import { ResourceConfig, BatchResourceConfig, NonBatchResourceConfig } from './config'; import assert from './assert'; import { getLoaderTypeKey, getLoaderTypeVal } from './genTypeFlow'; - -function errorPrefix(resourcePath: ReadonlyArray): string { - return `[dataloader-codegen :: ${resourcePath.join('.')}]`; -} +import { errorPrefix } from './runtimeHelpers'; function getLoaderComment(resourceConfig: ResourceConfig, resourcePath: ReadonlyArray): string { const configComment = JSON.stringify(resourceConfig, null, 2) @@ -246,7 +243,7 @@ function getBatchLoader(resourceConfig: BatchResourceConfig, resourcePath: Reado /** * We must return errors for all keys in this group :( */ - response = new Error([ + response = new BatchItemNotFoundError([ \`${errorPrefix( resourcePath, )} Resource returned \${response.length} items, but we requested \${requests.length} items.\`, diff --git a/src/runtimeHelpers.ts b/src/runtimeHelpers.ts index 35a5fe7..f4fcb0c 100644 --- a/src/runtimeHelpers.ts +++ b/src/runtimeHelpers.ts @@ -12,10 +12,20 @@ export function errorPrefix(resourcePath: ReadonlyArray): string { return `[dataloader-codegen :: ${resourcePath.join('.')}]`; } +/** + * An error reflects missing item in response. It follows similar structure to ApolloError that has an `extension` field. + * This makes it easier to link and integrate with apollo-server + * @see https://github.com/apollographql/apollo-server/blob/faba52c689c22472a19fcb65d78925df549077f7/packages/apollo-server-errors/src/index.ts#L3 + */ export class BatchItemNotFoundError extends Error { + readonly extensions: Record; + constructor(message: string) { super(message); this.name = this.constructor.name; + this.extensions = { + code: 'BATCH_ITEM_NOT_FOUND_ERROR', + }; Error.captureStackTrace(this, this.constructor); } }