Skip to content

Commit 9e3a12e

Browse files
committed
refactor: resolve PR feedbacks and replace onQuery and onMutation event factories with a unified onGraphqlEventFactory in tests
1 parent 0b12a2b commit 9e3a12e

File tree

2 files changed

+81
-15
lines changed

2 files changed

+81
-15
lines changed

packages/event-handler/src/appsync-graphql/AppSyncGraphQLResolver.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ export class AppSyncGraphQLResolver extends Router {
125125
fieldName
126126
);
127127
if (resolverHandlerOptions) {
128-
return await resolverHandlerOptions.handler.apply(this, [
129-
event.arguments,
130-
]);
128+
return resolverHandlerOptions.handler.apply(this, [event.arguments]);
131129
}
132130

133131
throw new ResolverNotFoundException(

packages/event-handler/tests/unit/appsync-graphql/AppSyncGraphQLResolver.test.ts

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import context from '@aws-lambda-powertools/testing-utils/context';
2-
import {
3-
onMutationEventFactory,
4-
onQueryEventFactory,
5-
} from 'tests/helpers/factories.js';
2+
import { onGraphqlEventFactory } from 'tests/helpers/factories.js';
63
import { beforeEach, describe, expect, it, vi } from 'vitest';
74
import { AppSyncGraphQLResolver } from '../../../src/appsync-graphql/AppSyncGraphQLResolver.js';
85
import { ResolverNotFoundException } from '../../../src/appsync-graphql/errors.js';
@@ -17,7 +14,10 @@ describe('Class: AppSyncGraphQLResolver', () => {
1714
const app = new AppSyncGraphQLResolver({ logger: console });
1815

1916
// Act
20-
const result = await app.resolve([onQueryEventFactory()], context);
17+
const result = await app.resolve(
18+
[onGraphqlEventFactory('getPost', 'Query')],
19+
context
20+
);
2121

2222
// Assess
2323
expect(console.warn).toHaveBeenCalledWith(
@@ -40,26 +40,26 @@ describe('Class: AppSyncGraphQLResolver', () => {
4040
expect(result).toBeUndefined();
4141
});
4242

43-
it('throw error if there are no onQuery handlers', async () => {
43+
it('throws error if there are no onQuery handlers', async () => {
4444
// Prepare
4545
const app = new AppSyncGraphQLResolver({ logger: console });
4646

4747
// Act && Assess
4848
await expect(
49-
app.resolve(onQueryEventFactory('getPost'), context)
49+
app.resolve(onGraphqlEventFactory('getPost', 'Query'), context)
5050
).rejects.toThrow(
5151
new ResolverNotFoundException('No resolver found for Query-getPost')
5252
);
5353
expect(console.error).toHaveBeenCalled();
5454
});
5555

56-
it('throw error if there are no onMutation handlers', async () => {
56+
it('throws error if there are no onMutation handlers', async () => {
5757
// Prepare
5858
const app = new AppSyncGraphQLResolver({ logger: console });
5959

6060
// Act && Assess
6161
await expect(
62-
app.resolve(onMutationEventFactory('addPost'), context)
62+
app.resolve(onGraphqlEventFactory('addPost', 'Mutation'), context)
6363
).rejects.toThrow(
6464
new ResolverNotFoundException('No resolver found for Mutation-addPost')
6565
);
@@ -84,7 +84,7 @@ describe('Class: AppSyncGraphQLResolver', () => {
8484

8585
// Act
8686
const result = await app.resolve(
87-
onQueryEventFactory('getPost', { id: '123' }),
87+
onGraphqlEventFactory('getPost', 'Query', { id: '123' }),
8888
context
8989
);
9090

@@ -115,7 +115,7 @@ describe('Class: AppSyncGraphQLResolver', () => {
115115

116116
// Act
117117
const result = await app.resolve(
118-
onMutationEventFactory('addPost', {
118+
onGraphqlEventFactory('addPost', 'Mutation', {
119119
title: 'Post Title',
120120
content: 'Post Content',
121121
}),
@@ -138,6 +138,74 @@ describe('Class: AppSyncGraphQLResolver', () => {
138138
});
139139
});
140140

141+
it('logs only warnings and errors using global console object if no logger supplied', async () => {
142+
// Prepare
143+
const app = new AppSyncGraphQLResolver();
144+
app.resolver<{ title: string; content: string }>(
145+
async ({ title, content }) => {
146+
return {
147+
id: '123',
148+
title,
149+
content,
150+
};
151+
},
152+
{
153+
fieldName: 'addPost',
154+
typeName: 'Mutation',
155+
}
156+
);
157+
158+
// Act
159+
const result = await app.resolve(
160+
onGraphqlEventFactory('addPost', 'Mutation', {
161+
title: 'Post Title',
162+
content: 'Post Content',
163+
}),
164+
context
165+
);
166+
167+
// Assess
168+
expect(console.debug).not.toHaveBeenCalledWith();
169+
expect(console.debug).not.toHaveBeenCalledWith();
170+
expect(result).toEqual({
171+
id: '123',
172+
title: 'Post Title',
173+
content: 'Post Content',
174+
});
175+
});
176+
177+
it('emits debug message when AWS_LAMBDA_LOG_LEVEL is set to DEBUG', async () => {
178+
// Prepare
179+
vi.stubEnv('AWS_LAMBDA_LOG_LEVEL', 'DEBUG');
180+
const app = new AppSyncGraphQLResolver();
181+
182+
app.resolver<{ title: string; content: string }>(
183+
async ({ title, content }) => {
184+
return {
185+
id: '123',
186+
title,
187+
content,
188+
};
189+
},
190+
{
191+
fieldName: 'addPost',
192+
typeName: 'Mutation',
193+
}
194+
);
195+
196+
// Act
197+
await app.resolve(
198+
onGraphqlEventFactory('addPost', 'Mutation', {
199+
title: 'Post Title',
200+
content: 'Post Content',
201+
}),
202+
context
203+
);
204+
205+
// Assess
206+
expect(console.debug).toHaveBeenCalled();
207+
});
208+
141209
it.each([
142210
{
143211
type: 'base error',
@@ -171,7 +239,7 @@ describe('Class: AppSyncGraphQLResolver', () => {
171239

172240
// Act
173241
const result = await app.resolve(
174-
onMutationEventFactory('addPost', {
242+
onGraphqlEventFactory('addPost', 'Mutation', {
175243
title: 'Post Title',
176244
content: 'Post Content',
177245
}),

0 commit comments

Comments
 (0)