|
| 1 | +import AggregateError from 'aggregate-error' |
1 | 2 | import { parse } from 'graphql' |
2 | 3 | import { equals } from '@aws/dynamodb-expressions' |
3 | 4 | import { buildExecutionContext } from 'graphql/execution/execute' |
4 | | -import { MessageHandler } from './types' |
5 | 5 | import { constructContext, getResolverAndArgs } from '../utils/graphql' |
6 | | -import { SubscribePsuedoIterable } from '../types' |
| 6 | +import { SubscribePsuedoIterable, MessageHandler } from '../types' |
| 7 | +import { isArray } from '../utils/isArray' |
7 | 8 |
|
8 | 9 | /** Handler function for 'disconnect' message. */ |
9 | 10 | export const disconnect: MessageHandler<null> = |
10 | | - async ({ c, event }) => { |
| 11 | + async ({ server, event }) => { |
11 | 12 | try { |
12 | | - await c.onDisconnect?.({ event }) |
| 13 | + await server.onDisconnect?.({ event }) |
13 | 14 |
|
14 | | - const entities = await c.mapper.query( |
15 | | - c.model.Subscription, |
| 15 | + const entities = server.mapper.query( |
| 16 | + server.model.Subscription, |
16 | 17 | { |
17 | 18 | connectionId: equals(event.requestContext.connectionId), |
18 | 19 | }, |
19 | 20 | { indexName: 'ConnectionIndex' }, |
20 | 21 | ) |
21 | 22 |
|
22 | 23 | const completed = {} as Record<string, boolean> |
23 | | - let deletions = [] as Promise<any>[] |
| 24 | + const deletions = [] as Promise<any>[] |
24 | 25 | for await (const entity of entities) { |
25 | | - deletions = [ |
26 | | - ...deletions, |
| 26 | + deletions.push( |
27 | 27 | (async () => { |
28 | 28 | // only call onComplete per subscription |
29 | 29 | if (!completed[entity.subscriptionId]) { |
30 | 30 | completed[entity.subscriptionId] = true |
31 | 31 |
|
32 | 32 | const execContext = buildExecutionContext( |
33 | | - c.schema, |
| 33 | + server.schema, |
34 | 34 | parse(entity.subscription.query), |
35 | 35 | undefined, |
36 | | - await constructContext(c)(entity), |
| 36 | + await constructContext(server)(entity), |
37 | 37 | entity.subscription.variables, |
38 | 38 | entity.subscription.operationName, |
39 | 39 | undefined, |
40 | 40 | ) |
41 | 41 |
|
42 | | - if (!('operation' in execContext)) { |
43 | | - throw execContext |
| 42 | + if (isArray(execContext)) { |
| 43 | + throw new AggregateError(execContext) |
44 | 44 | } |
45 | 45 |
|
46 | | - const [field, root, args, context, info] = |
47 | | - getResolverAndArgs(c)(execContext) |
| 46 | + |
| 47 | + const [field, root, args, context, info] = getResolverAndArgs(server)(execContext) |
48 | 48 |
|
49 | 49 | const onComplete = (field?.subscribe as SubscribePsuedoIterable)?.onComplete |
50 | 50 | if (onComplete) { |
51 | 51 | await onComplete(root, args, context, info) |
52 | 52 | } |
53 | 53 | } |
54 | 54 |
|
55 | | - await c.mapper.delete(entity) |
| 55 | + await server.mapper.delete(entity) |
56 | 56 | })(), |
57 | | - ] |
| 57 | + ) |
58 | 58 | } |
59 | 59 |
|
60 | 60 | await Promise.all([ |
61 | 61 | // Delete subscriptions |
62 | 62 | ...deletions, |
63 | 63 | // Delete connection |
64 | | - c.mapper.delete( |
65 | | - Object.assign(new c.model.Connection(), { |
| 64 | + server.mapper.delete( |
| 65 | + Object.assign(new server.model.Connection(), { |
66 | 66 | id: event.requestContext.connectionId!, |
67 | 67 | }), |
68 | 68 | ), |
69 | 69 | ]) |
70 | 70 | } catch (err) { |
71 | | - await c.onError?.(err, { event }) |
| 71 | + await server.onError?.(err, { event }) |
72 | 72 | } |
73 | 73 | } |
0 commit comments