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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ class RouteHandlerRegistry {
public register(
options: RouteHandlerOptions<Record<string, unknown>, boolean, boolean>
): void {
const { fieldName, handler, typeName, throwOnError, aggregate } = options;
const {
fieldName,
handler,
typeName,
throwOnError = false,
aggregate = true,
} = options;
this.#logger.debug(`Adding resolver for field ${typeName}.${fieldName}`);
const cacheKey = this.#makeKey(typeName, fieldName);
if (this.resolvers.has(cacheKey)) {
Expand Down
24 changes: 12 additions & 12 deletions packages/event-handler/src/appsync-graphql/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@ class Router {
fieldName,
handler: handler as BatchResolverHandler,
typeName,
aggregate: batchResolverOptions?.aggregate ?? true,
throwOnError: batchResolverOptions?.throwOnError ?? false,
aggregate: batchResolverOptions?.aggregate,
throwOnError: batchResolverOptions?.throwOnError,
});
return;
}
Expand All @@ -544,8 +544,8 @@ class Router {
fieldName,
handler: descriptor?.value,
typeName,
aggregate: batchResolverOptions?.aggregate ?? true,
throwOnError: batchResolverOptions?.throwOnError ?? false,
aggregate: batchResolverOptions?.aggregate,
throwOnError: batchResolverOptions?.throwOnError,
});
return descriptor;
};
Expand Down Expand Up @@ -730,8 +730,8 @@ class Router {
fieldName,
handler: handlerOrOptions as BatchResolverHandler,
typeName: 'Query',
aggregate: options?.aggregate ?? true,
throwOnError: options?.throwOnError ?? false,
aggregate: options?.aggregate,
throwOnError: options?.throwOnError,
});

return;
Expand All @@ -742,8 +742,8 @@ class Router {
fieldName,
handler: descriptor?.value,
typeName: 'Query',
aggregate: handlerOrOptions?.aggregate ?? true,
throwOnError: handlerOrOptions?.throwOnError ?? false,
aggregate: handlerOrOptions?.aggregate,
throwOnError: handlerOrOptions?.throwOnError,
});

return descriptor;
Expand Down Expand Up @@ -927,8 +927,8 @@ class Router {
fieldName,
handler: handlerOrOptions as BatchResolverHandler,
typeName: 'Mutation',
aggregate: options?.aggregate ?? true,
throwOnError: options?.throwOnError ?? false,
aggregate: options?.aggregate,
throwOnError: options?.throwOnError,
});

return;
Expand All @@ -939,8 +939,8 @@ class Router {
fieldName,
handler: descriptor?.value,
typeName: 'Mutation',
aggregate: handlerOrOptions?.aggregate ?? true,
throwOnError: handlerOrOptions?.throwOnError ?? false,
aggregate: handlerOrOptions?.aggregate,
throwOnError: handlerOrOptions?.throwOnError,
});

return descriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,15 +506,11 @@ describe('Class: AppSyncGraphQLResolver', () => {
setupHandler(handler);

if (aggregate) {
app.batchResolver(handler, {
fieldName: 'batchGet',
typeName: 'Query',
app.onBatchQuery('batchGet', handler, {
aggregate: true,
});
} else {
app.batchResolver(handler, {
fieldName: 'batchGet',
typeName: 'Query',
app.onBatchQuery('batchGet', handler, {
aggregate: false,
throwOnError: true,
});
Expand Down Expand Up @@ -596,16 +592,14 @@ describe('Class: AppSyncGraphQLResolver', () => {
.mockResolvedValueOnce({ id: '1', value: 'A' })
.mockRejectedValueOnce(new Error('fail'))
.mockResolvedValueOnce({ id: '3', value: 'C' });
app.batchResolver(handler, {
fieldName: 'batchGet',
typeName: 'Query',
app.onBatchMutation('batchPut', handler, {
aggregate: false,
throwOnError: true,
});
const events = [
onGraphqlEventFactory('batchGet', 'Query', { id: '1' }),
onGraphqlEventFactory('batchGet', 'Query', { id: '2' }),
onGraphqlEventFactory('batchGet', 'Query', { id: '3' }),
onGraphqlEventFactory('batchPut', 'Mutation', { id: '1' }),
onGraphqlEventFactory('batchPut', 'Mutation', { id: '2' }),
onGraphqlEventFactory('batchPut', 'Mutation', { id: '3' }),
];

// Act
Expand Down Expand Up @@ -640,4 +634,76 @@ describe('Class: AppSyncGraphQLResolver', () => {
)
);
});

it.each([
{
throwOnError: true,
description: 'throwOnError=true',
},
{
throwOnError: false,
description: 'throwOnError=false',
},
])(
'preserves the scope when using `onBatchQuery` & `onBatchMutation` decorators when aggregate=false and $description',
async ({ throwOnError }) => {
// Prepare
const app = new AppSyncGraphQLResolver({ logger: console });

class Lambda {
public readonly scope = 'scoped';

@app.onBatchQuery('batchGet', {
throwOnError,
})
public async handleBatchGet(
events: AppSyncResolverEvent<{ id: number }>[]
) {
const ids = events.map((event) => event.arguments.id);
return ids.map((id) => ({
id,
scope: this.scope,
}));
}

@app.onBatchMutation('batchPut', {
throwOnError,
})
public async handleBatchPut(
_events: AppSyncResolverEvent<{ id: number }>[]
) {
return [this.scope, this.scope];
}

public async handler(event: unknown, context: Context) {
return app.resolve(event, context, { scope: this });
}
}
const lambda = new Lambda();
const handler = lambda.handler.bind(lambda);

// Act
const resultQuery = await handler(
[
onGraphqlEventFactory('batchGet', 'Query', { id: 1 }),
onGraphqlEventFactory('batchGet', 'Query', { id: 2 }),
],
context
);
const resultMutation = await handler(
[
onGraphqlEventFactory('batchPut', 'Mutation', { id: 1 }),
onGraphqlEventFactory('batchPut', 'Mutation', { id: 2 }),
],
context
);

// Assess
expect(resultQuery).toEqual([
{ id: 1, scope: 'scoped' },
{ id: 2, scope: 'scoped' },
]);
expect(resultMutation).toEqual(['scoped', 'scoped']);
}
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ describe('Class: RouteHandlerRegistry', () => {
// Assess
expect(registry.resolvers.size).toBe(1);
expect(registry.resolvers.get('Query.getPost')).toEqual({
aggregate: true,
fieldName: 'getPost',
typeName: 'Query',
throwOnError: false,
handler: otherHandler,
});
expect(console.warn).toHaveBeenCalledWith(
"A resolver for field 'getPost' is already registered for 'Query'. The previous resolver will be replaced."
);
});

it('will not replace the resolver if the event type is different', () => {
it("doesn't replace the resolver if the event type is different", () => {
// Prepare
const registry = getRegistry();
const originalHandler = vi.fn();
Expand All @@ -89,13 +91,17 @@ describe('Class: RouteHandlerRegistry', () => {
// Assess
expect(registry.resolvers.size).toBe(2);
expect(registry.resolvers.get('Query.getPost')).toEqual({
aggregate: true,
fieldName: 'getPost',
typeName: 'Query',
throwOnError: false,
handler: originalHandler,
});
expect(registry.resolvers.get('Mutation.getPost')).toEqual({
aggregate: true,
fieldName: 'getPost',
typeName: 'Mutation',
throwOnError: false,
handler: otherHandler,
});
});
Expand Down
5 changes: 5 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default defineConfig({
'packages/testing/**',
],
},
projects: [
'packages/*/vitest.config.ts',
'examples/app/vitest.config.ts',
'layers/vitest.config.ts',
],
setupFiles: ['./packages/testing/src/setupEnv.ts'],
hookTimeout: 1_000 * 60 * 10, // 10 minutes
testTimeout: 1_000 * 60 * 3, // 3 minutes
Expand Down
5 changes: 0 additions & 5 deletions vitest.workspace.ts

This file was deleted.

Loading