Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.

Commit 6283a29

Browse files
authored
fix: gatwayHandler types are now more narrow (#26)
1 parent 2165278 commit 6283a29

File tree

5 files changed

+53
-18
lines changed

5 files changed

+53
-18
lines changed

lib/gateway.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Handler } from 'aws-lambda'
21
import { GRAPHQL_TRANSPORT_WS_PROTOCOL, MessageType } from 'graphql-ws'
32
import {
3+
ApiGatewayHandler,
44
APIGatewayWebSocketEvent,
55
ServerClosure,
66
WebsocketResponse,
@@ -13,7 +13,7 @@ import { connection_init } from './messages/connection_init'
1313
import { pong } from './messages/pong'
1414

1515
export const handleGatewayEvent =
16-
(server: ServerClosure): Handler<APIGatewayWebSocketEvent, WebsocketResponse> =>
16+
(server: ServerClosure): ApiGatewayHandler<APIGatewayWebSocketEvent, WebsocketResponse> =>
1717
async (event) => {
1818
if (!event.requestContext) {
1919
return {

lib/index-test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { assert } from 'chai'
2+
import { Handler } from 'aws-lambda'
3+
import { tables } from '@architect/sandbox'
4+
import { createInstance } from '.'
5+
import { mockServerArgs } from './test/mockServer'
6+
import { APIGatewayWebSocketEvent, WebsocketResponse } from './types'
7+
8+
describe('createInstance', () => {
9+
describe('gatewayHandler', () => {
10+
before(async () => {
11+
await tables.start({ cwd: './mocks/arc-basic-events', quiet: true })
12+
})
13+
14+
after(async () => {
15+
tables.end()
16+
})
17+
18+
it('is type compatible with aws-lambda handler', async () => {
19+
const server = createInstance(await mockServerArgs())
20+
21+
const gatewayHandler: Handler<APIGatewayWebSocketEvent, WebsocketResponse> = server.gatewayHandler
22+
assert.ok(gatewayHandler)
23+
})
24+
})
25+
})

lib/messages/subscribe-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { assert } from 'chai'
22
import { tables } from '@architect/sandbox'
33
import { subscribe } from './subscribe'
4-
import { mockServerContext } from '../test/mockServerContext'
4+
import { mockServerContext } from '../test/mockServer'
55
import { connection_init } from './connection_init'
66
import { equals } from '@aws/dynamodb-expressions'
77
import { collect } from 'streaming-iterables'

lib/test/mockServerContext.ts renamed to lib/test/mockServer.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-empty-function */
12
import { makeExecutableSchema } from '@graphql-tools/schema'
23
import { tables as arcTables } from '@architect/functions'
34
import { makeServerClosure } from '../makeServerClosure'
@@ -33,26 +34,33 @@ const schema = makeExecutableSchema({
3334
resolvers,
3435
})
3536

37+
const ensureName = (tables: any, table: string) => {
38+
const actualTableName = tables.name(table)
39+
if (!actualTableName) {
40+
throw new Error(`No table found for ${table}`)
41+
}
42+
return actualTableName
43+
}
3644

37-
export const mockServerContext = async (args: Partial<ServerArgs>): Promise<ServerClosure> => {
45+
export const mockServerArgs = async (args: Partial<ServerArgs> = {}): Promise<ServerArgs> => {
3846
const tables = await arcTables()
3947

40-
const ensureName = (table) => {
41-
const actualTableName = tables.name(table)
42-
if (!actualTableName) {
43-
throw new Error(`No table found for ${table}`)
44-
}
45-
return actualTableName
46-
}
47-
48-
return makeServerClosure({
48+
return {
4949
dynamodb: arcTables.db,
5050
schema,
5151
tableNames: {
52-
connections: ensureName('Connection'),
53-
subscriptions: ensureName('Subscription'),
52+
connections: ensureName(tables, 'Connection'),
53+
subscriptions: ensureName(tables, 'Subscription'),
54+
},
55+
apiGatewayManagementApi: {
56+
postToConnection: () => ({ promise: async () => { } }),
57+
deleteConnection: () => ({ promise: async () => { } }),
5458
},
5559
onError: (err) => { console.log('onError'); throw err },
5660
...args,
57-
})
61+
}
62+
}
63+
64+
export const mockServerContext = async (args?: Partial<ServerArgs>): Promise<ServerClosure> => {
65+
return makeServerClosure(await mockServerArgs(args))
5866
}

lib/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable @typescript-eslint/ban-types */
33
import { ConnectionInitMessage, PingMessage, PongMessage } from 'graphql-ws'
44
import { DataMapper } from '@aws/dynamodb-data-mapper'
5-
import { APIGatewayEventRequestContext, APIGatewayProxyEvent, Handler } from 'aws-lambda'
5+
import { APIGatewayEventRequestContext, APIGatewayProxyEvent } from 'aws-lambda'
66
import { GraphQLResolveInfo, GraphQLSchema } from 'graphql'
77
import { DynamoDB } from 'aws-sdk'
88
import { Subscription } from './model/Subscription'
@@ -48,7 +48,7 @@ export type ServerClosure = {
4848
} & Omit<ServerArgs, 'tableNames'>
4949

5050
export interface ServerInstance {
51-
gatewayHandler: Handler<APIGatewayWebSocketEvent, WebsocketResponse>
51+
gatewayHandler: ApiGatewayHandler<APIGatewayWebSocketEvent, WebsocketResponse>
5252
stateMachineHandler: (input: StateFunctionInput) => Promise<StateFunctionInput>
5353
publish: (event: PubSubEvent) => Promise<void>
5454
complete: (event: PubSubEvent) => Promise<void>
@@ -123,3 +123,5 @@ export interface SubscribeOptions {
123123
onComplete?: (...args: SubscribeArgs) => void | Promise<void>
124124
onAfterSubscribe?: (...args: SubscribeArgs) => PubSubEvent | Promise<PubSubEvent> | undefined | Promise<undefined>
125125
}
126+
127+
export type ApiGatewayHandler<TEvent = any, TResult = any> = (event: TEvent) => void | Promise<TResult>

0 commit comments

Comments
 (0)