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

feat(modularity): allow inject log fn in handler #149

Merged
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
17 changes: 12 additions & 5 deletions packages/aws-lambda-graphql/src/DynamoDBEventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ import { isTTLExpired } from './helpers/isTTLExpired';
interface DynamoDBEventProcessorOptions {
onError?: (err: any) => void;
/**
* Enable console.log
* Enable log
*/
debug?: boolean;

/**
* Allow injecting a logging function
*/
log?: (message: any, ...optionalParams: any[]) => void;
}

/**
Expand All @@ -30,8 +35,11 @@ export class DynamoDBEventProcessor<TServer extends Server = Server>

private debug: boolean;

private log: (message: any, ...optionalParams: any[]) => void;

constructor(options: DynamoDBEventProcessorOptions = {}) {
this.onError = options.onError || ((err: any) => console.log(err));
this.log = options.log || console.log;
this.onError = options.onError || ((err: any) => this.log(err));
this.debug = options.debug || false;
}

Expand All @@ -54,7 +62,7 @@ export class DynamoDBEventProcessor<TServer extends Server = Server>

// skip if event is expired
if (isTTLExpired(event.ttl)) {
if (this.debug) console.log('Discarded event : TTL expired', event);
if (this.debug) this.log('Discarded event : TTL expired', event);
continue;
}

Expand Down Expand Up @@ -109,9 +117,8 @@ export class DynamoDBEventProcessor<TServer extends Server = Server>
const iterator = getAsyncIterator(iterable);
const result: IteratorResult<ExecutionResult> = await iterator.next();

if (this.debug) console.log('Send event ', result);

if (result.value != null) {
if (this.debug) this.log('Send event ', result);
return connectionManager.sendToConnection(
subscriber.connection,
formatMessage({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const query = parse(/* GraphQL */ `

describe('DynamoDBEventProcessor', () => {
it('supports payload as JSON', async () => {
const mockLog = jest.fn();
const connectionManager = {
sendToConnection: jest.fn(),
};
Expand Down Expand Up @@ -71,7 +72,10 @@ describe('DynamoDBEventProcessor', () => {
pubSub: new PubSub({ eventStore: {} as any }),
},
connectionManager: connectionManager as any,
eventProcessor: new DynamoDBEventProcessor(),
eventProcessor: new DynamoDBEventProcessor({
log: mockLog,
debug: true,
}),
schema: createSchema(),
subscriptionManager: subscriptionManager as any,
});
Expand Down Expand Up @@ -142,6 +146,14 @@ describe('DynamoDBEventProcessor', () => {
type: SERVER_EVENT_TYPES.GQL_DATA,
}),
);
expect(mockLog).toHaveBeenCalledWith('Send event ', {
done: false,
value: {
data: {
textFeed: 'test 1',
},
},
});
});

it('supports payload as object', async () => {
Expand Down