From 1b8b04734c11df282b804eebbde380312fe757de Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Wed, 18 Aug 2021 10:03:46 -0400 Subject: [PATCH] fix: server instance event handler types to match their actual functions - dried up those type definitions so they don't drift - renamed some files to match function names --- lib/{gateway.ts => handleGatewayEvent.ts} | 9 ++------- ...stepFunctionHandler.ts => handleStateMachineEvent.ts} | 4 ++-- lib/index.ts | 4 ++-- lib/pubsub/complete.ts | 4 ++-- lib/pubsub/publish.ts | 4 ++-- lib/types.ts | 2 +- 6 files changed, 11 insertions(+), 16 deletions(-) rename lib/{gateway.ts => handleGatewayEvent.ts} (90%) rename lib/{stepFunctionHandler.ts => handleStateMachineEvent.ts} (83%) diff --git a/lib/gateway.ts b/lib/handleGatewayEvent.ts similarity index 90% rename from lib/gateway.ts rename to lib/handleGatewayEvent.ts index 1ff55462..6c64b28a 100644 --- a/lib/gateway.ts +++ b/lib/handleGatewayEvent.ts @@ -1,10 +1,5 @@ import { GRAPHQL_TRANSPORT_WS_PROTOCOL, MessageType } from 'graphql-ws' -import { - ApiGatewayHandler, - APIGatewayWebSocketEvent, - ServerClosure, - WebsocketResponse, -} from './types' +import { ServerClosure, ServerInstance } from './types' import { disconnect } from './messages/disconnect' import { ping } from './messages/ping' import { complete } from './messages/complete' @@ -12,7 +7,7 @@ import { subscribe } from './messages/subscribe' import { connection_init } from './messages/connection_init' import { pong } from './messages/pong' -export const handleGatewayEvent = (server: ServerClosure): ApiGatewayHandler => async (event) => { +export const handleGatewayEvent = (server: ServerClosure): ServerInstance['gatewayHandler'] => async (event) => { if (!event.requestContext) { server.log('handleGatewayEvent unknown') return { diff --git a/lib/stepFunctionHandler.ts b/lib/handleStateMachineEvent.ts similarity index 83% rename from lib/stepFunctionHandler.ts rename to lib/handleStateMachineEvent.ts index 6b26f159..61ab3039 100644 --- a/lib/stepFunctionHandler.ts +++ b/lib/handleStateMachineEvent.ts @@ -1,9 +1,9 @@ import { MessageType } from 'graphql-ws' -import { ServerClosure, StateFunctionInput } from './types' +import { ServerClosure, ServerInstance } from './types' import { sendMessage } from './utils/sendMessage' import { deleteConnection } from './utils/deleteConnection' -export const handleStateMachineEvent = (c: ServerClosure) => async (input: StateFunctionInput): Promise => { +export const handleStateMachineEvent = (c: ServerClosure): ServerInstance['stateMachineHandler'] => async (input) => { if (!c.pingpong) { throw new Error('Invalid pingpong settings') } diff --git a/lib/index.ts b/lib/index.ts index 248905c5..c875c209 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,8 +1,8 @@ import { ServerArgs, ServerClosure, ServerInstance } from './types' import { publish } from './pubsub/publish' import { complete } from './pubsub/complete' -import { handleGatewayEvent } from './gateway' -import { handleStateMachineEvent } from './stepFunctionHandler' +import { handleGatewayEvent } from './handleGatewayEvent' +import { handleStateMachineEvent } from './handleStateMachineEvent' import { makeServerClosure } from './makeServerClosure' export const createInstance = (opts: ServerArgs): ServerInstance => { diff --git a/lib/pubsub/complete.ts b/lib/pubsub/complete.ts index 91bd99fb..eb3a0842 100644 --- a/lib/pubsub/complete.ts +++ b/lib/pubsub/complete.ts @@ -2,14 +2,14 @@ import AggregateError from 'aggregate-error' import { parse } from 'graphql' import { CompleteMessage, MessageType } from 'graphql-ws' import { buildExecutionContext } from 'graphql/execution/execute' -import { ServerClosure, PubSubEvent, SubscribePseudoIterable, PartialBy } from '../types' +import { ServerClosure, PubSubEvent, SubscribePseudoIterable, ServerInstance } from '../types' import { sendMessage } from '../utils/sendMessage' import { constructContext } from '../utils/constructContext' import { getResolverAndArgs } from '../utils/getResolverAndArgs' import { isArray } from '../utils/isArray' import { getFilteredSubs } from './getFilteredSubs' -export const complete = (server: ServerClosure) => async (event: PartialBy): Promise => { +export const complete = (server: ServerClosure): ServerInstance['complete'] => async event => { const subscriptions = await getFilteredSubs({ server, event }) server.log('pubsub:complete %j', { event, subscriptions }) diff --git a/lib/pubsub/publish.ts b/lib/pubsub/publish.ts index 0b578b76..2772602f 100644 --- a/lib/pubsub/publish.ts +++ b/lib/pubsub/publish.ts @@ -1,11 +1,11 @@ import { parse, execute } from 'graphql' import { MessageType, NextMessage } from 'graphql-ws' -import { PubSubEvent, ServerClosure } from '../types' +import { ServerClosure, ServerInstance } from '../types' import { sendMessage } from '../utils/sendMessage' import { constructContext } from '../utils/constructContext' import { getFilteredSubs } from './getFilteredSubs' -export const publish = (server: ServerClosure) => async (event: T): Promise => { +export const publish = (server: ServerClosure): ServerInstance['publish'] => async event => { server.log('pubsub:publish %j', { event }) const subscriptions = await getFilteredSubs({ server, event }) server.log('pubsub:publish %j', { subscriptions: subscriptions.map(({ connectionId, filter, subscription }) => ({ connectionId, filter, subscription }) ) }) diff --git a/lib/types.ts b/lib/types.ts index b67bcb94..55d76b53 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -53,7 +53,7 @@ export interface ServerInstance { gatewayHandler: ApiGatewayHandler stateMachineHandler: (input: StateFunctionInput) => Promise publish: (event: PubSubEvent) => Promise - complete: (event: PubSubEvent) => Promise + complete: (event: PartialBy) => Promise } export type TableNames = {