Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resolver timeout #19

Open
wants to merge 6 commits into
base: joysteam
Choose a base branch
from
Open
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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ Almost all config in Warthog is driven by environment variables. The following i
| WARTHOG_RESOLVERS_PATH | Where should Warthog look for resolvers | src/\*\*\/\*.resolver.ts |
| WARTHOG_SUBSCRIPTIONS | Should we enable subscriptions and open a websocket port | false |
| WARTHOG_VALIDATE_RESOLVERS | TypeGraphQL validation enabled? | false |
| WARTHOG_RESOLVER_TIMEOUT_MS | Highest time a resolver should take before timing out | _none_ |
| WARTHOG_RELATION_CONCURRENCY | Max number of relations a resolver can query in parallel | 30 |

## Field/Column Decorators

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "@joystream/warthog",
"version": "2.41.9",
"version": "2.42.0",
"description": "Opinionated set of tools for setting up GraphQL backed by TypeORM",
"main": "dist/index.js",
"types": "dist/types/index.d.ts",
"engines": {
"node": ">=18"
},
"bin": {
"warthog": "bin/warthog"
},
Expand Down Expand Up @@ -107,7 +110,7 @@
"typedi": "^0.8.0",
"typeorm": "https://github.com/Joystream/typeorm/releases/download/0.3.5/typeorm-v0.3.5.tgz",
"typeorm-typedi-extensions": "^0.4.1",
"typescript": "^4.4"
"typescript": "^5.4.2"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
Expand Down
43 changes: 32 additions & 11 deletions src/middleware/DataLoaderMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import * as DataLoader from 'dataloader';
import { chunk } from 'lodash';
import { MiddlewareInterface, NextFn, ResolverData } from 'type-graphql';
import { Service } from 'typedi';

import { BaseContext } from '../core';

interface Deleteable {
deletedAt?: string;
}

@Service()
export class DataLoaderMiddleware implements MiddlewareInterface<BaseContext> {
async use({ context }: ResolverData<BaseContext>, next: NextFn) {
Expand All @@ -17,6 +14,11 @@ export class DataLoaderMiddleware implements MiddlewareInterface<BaseContext> {
loaders: {},
};

const reqTimeout = Number(process.env.WARTHOG_RESOLVER_TIMEOUT_MS);
const chunkSize = Number(process.env.WARTHOG_RELATION_CONCURRENCY) || 30;

const abortSignal = reqTimeout ? AbortSignal.timeout(reqTimeout) : undefined;

const loaders = context.dataLoader.loaders;

context.connection.entityMetadatas.forEach((entityMetadata) => {
Expand All @@ -36,18 +38,37 @@ export class DataLoaderMiddleware implements MiddlewareInterface<BaseContext> {
if (Array.isArray(entities) && entities[0] && Array.isArray(entities[0])) {
throw new Error('You must flatten arrays of arrays of entities');
}
return Promise.all(
entities.map((entity) => context.connection.relationLoader.load(relation, entity))
).then(function (results) {
return results.map(function (related) {
return relation.isManyToOne || relation.isOneToOne ? related[0] : related;
});
});

return chunk(entities, chunkSize).reduce<Promise<any[][]>>(
async (prev, entityChunk) => {
const next = await prev;

if (abortSignal?.aborted) {
throw new Error('Resolver timed out');
}

const results = await Promise.all(
entityChunk.map((entity) =>
context.connection.relationLoader.load(relation, entity)
)
);

next.push(
...results.map((related) =>
relation.isManyToOne || relation.isOneToOne ? related[0] : related
)
);

return next;
},
Promise.resolve([])
);
});
}
});
});
}

return next();
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"ignoreDeprecations": "5.0",
"keyofStringsOnly": true,
"lib": [
"es2016",
Expand Down Expand Up @@ -54,4 +55,4 @@
"src/**/*",
"typings"
]
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11459,10 +11459,10 @@ typescript-tuple@^2.2.1:
dependencies:
typescript-compare "^0.0.2"

typescript@^4.4:
version "4.5.4"
resolved "https://registry.npmjs.org/typescript/-/typescript-4.5.4.tgz#a17d3a0263bf5c8723b9c52f43c5084edf13c2e8"
integrity sha512-VgYs2A2QIRuGphtzFV7aQJduJ2gyfTljngLzjpfW9FoYZF6xuw1W0vW9ghCKLfcWrCFxK81CSGRAvS1pn4fIUg==
typescript@^5.4.2:
version "5.4.2"
resolved "https://registry.npmjs.org/typescript/-/typescript-5.4.2.tgz#0ae9cebcfae970718474fe0da2c090cad6577372"
integrity sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ==

uc.micro@^1.0.1, uc.micro@^1.0.5:
version "1.0.6"
Expand Down