Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.
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
4 changes: 2 additions & 2 deletions lib/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Handler } from 'aws-lambda'
import { GRAPHQL_TRANSPORT_WS_PROTOCOL, MessageType } from 'graphql-ws'
import {
ApiGatewayHandler,
APIGatewayWebSocketEvent,
ServerClosure,
WebsocketResponse,
Expand All @@ -13,7 +13,7 @@ import { connection_init } from './messages/connection_init'
import { pong } from './messages/pong'

export const handleGatewayEvent =
(server: ServerClosure): Handler<APIGatewayWebSocketEvent, WebsocketResponse> =>
(server: ServerClosure): ApiGatewayHandler<APIGatewayWebSocketEvent, WebsocketResponse> =>
async (event) => {
if (!event.requestContext) {
return {
Expand Down
25 changes: 25 additions & 0 deletions lib/index-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { assert } from 'chai'
import { Handler } from 'aws-lambda'
import { tables } from '@architect/sandbox'
import { createInstance } from '.'
import { mockServerArgs } from './test/mockServer'
import { APIGatewayWebSocketEvent, WebsocketResponse } from './types'

describe('createInstance', () => {
describe('gatewayHandler', () => {
before(async () => {
await tables.start({ cwd: './mocks/arc-basic-events', quiet: true })
})

after(async () => {
tables.end()
})

it('is type compatible with aws-lambda handler', async () => {
const server = createInstance(await mockServerArgs())

const gatewayHandler: Handler<APIGatewayWebSocketEvent, WebsocketResponse> = server.gatewayHandler
assert.ok(gatewayHandler)
})
})
})
2 changes: 1 addition & 1 deletion lib/messages/subscribe-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from 'chai'
import { tables } from '@architect/sandbox'
import { subscribe } from './subscribe'
import { mockServerContext } from '../test/mockServerContext'
import { mockServerContext } from '../test/mockServer'
import { connection_init } from './connection_init'
import { equals } from '@aws/dynamodb-expressions'
import { collect } from 'streaming-iterables'
Expand Down
34 changes: 21 additions & 13 deletions lib/test/mockServerContext.ts → lib/test/mockServer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { makeExecutableSchema } from '@graphql-tools/schema'
import { tables as arcTables } from '@architect/functions'
import { makeServerClosure } from '../makeServerClosure'
Expand Down Expand Up @@ -33,26 +34,33 @@ const schema = makeExecutableSchema({
resolvers,
})

const ensureName = (tables: any, table: string) => {
const actualTableName = tables.name(table)
if (!actualTableName) {
throw new Error(`No table found for ${table}`)
}
return actualTableName
}

export const mockServerContext = async (args: Partial<ServerArgs>): Promise<ServerClosure> => {
export const mockServerArgs = async (args: Partial<ServerArgs> = {}): Promise<ServerArgs> => {
const tables = await arcTables()

const ensureName = (table) => {
const actualTableName = tables.name(table)
if (!actualTableName) {
throw new Error(`No table found for ${table}`)
}
return actualTableName
}

return makeServerClosure({
return {
dynamodb: arcTables.db,
schema,
tableNames: {
connections: ensureName('Connection'),
subscriptions: ensureName('Subscription'),
connections: ensureName(tables, 'Connection'),
subscriptions: ensureName(tables, 'Subscription'),
},
apiGatewayManagementApi: {
postToConnection: () => ({ promise: async () => { } }),
deleteConnection: () => ({ promise: async () => { } }),
},
onError: (err) => { console.log('onError'); throw err },
...args,
})
}
}

export const mockServerContext = async (args?: Partial<ServerArgs>): Promise<ServerClosure> => {
return makeServerClosure(await mockServerArgs(args))
}
6 changes: 4 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/ban-types */
import { ConnectionInitMessage, PingMessage, PongMessage } from 'graphql-ws'
import { DataMapper } from '@aws/dynamodb-data-mapper'
import { APIGatewayEventRequestContext, APIGatewayProxyEvent, Handler } from 'aws-lambda'
import { APIGatewayEventRequestContext, APIGatewayProxyEvent } from 'aws-lambda'
import { GraphQLResolveInfo, GraphQLSchema } from 'graphql'
import { DynamoDB } from 'aws-sdk'
import { Subscription } from './model/Subscription'
Expand Down Expand Up @@ -48,7 +48,7 @@ export type ServerClosure = {
} & Omit<ServerArgs, 'tableNames'>

export interface ServerInstance {
gatewayHandler: Handler<APIGatewayWebSocketEvent, WebsocketResponse>
gatewayHandler: ApiGatewayHandler<APIGatewayWebSocketEvent, WebsocketResponse>
stateMachineHandler: (input: StateFunctionInput) => Promise<StateFunctionInput>
publish: (event: PubSubEvent) => Promise<void>
complete: (event: PubSubEvent) => Promise<void>
Expand Down Expand Up @@ -123,3 +123,5 @@ export interface SubscribeOptions {
onComplete?: (...args: SubscribeArgs) => void | Promise<void>
onAfterSubscribe?: (...args: SubscribeArgs) => PubSubEvent | Promise<PubSubEvent> | undefined | Promise<undefined>
}

export type ApiGatewayHandler<TEvent = any, TResult = any> = (event: TEvent) => void | Promise<TResult>