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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
1 change: 1 addition & 0 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default function codegen(
import invariant from 'assert';
import DataLoader from 'dataloader';
import {
BatchItemNotFoundError,
CaughtResourceError,
cacheKeyOptions,
partitionItems,
Expand Down
7 changes: 2 additions & 5 deletions src/implementation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { ResourceConfig, BatchResourceConfig, NonBatchResourceConfig } from './config';
import assert from './assert';
import { getLoaderTypeKey, getLoaderTypeVal } from './genTypeFlow';

function errorPrefix(resourcePath: ReadonlyArray<string>): string {
return `[dataloader-codegen :: ${resourcePath.join('.')}]`;
}
import { errorPrefix } from './runtimeHelpers';

function getLoaderComment(resourceConfig: ResourceConfig, resourcePath: ReadonlyArray<string>): string {
const configComment = JSON.stringify(resourceConfig, null, 2)
Expand Down Expand Up @@ -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.\`,
Expand Down
10 changes: 10 additions & 0 deletions src/runtimeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ export function errorPrefix(resourcePath: ReadonlyArray<string>): 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<string, any>;

constructor(message: string) {
super(message);
this.name = this.constructor.name;
this.extensions = {
code: 'BATCH_ITEM_NOT_FOUND_ERROR',
};
Error.captureStackTrace(this, this.constructor);
}
}
Expand Down