This repository was archived by the owner on May 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcomplete.ts
More file actions
47 lines (41 loc) · 1.95 KB
/
complete.ts
File metadata and controls
47 lines (41 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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, SubscriptionServer } from '../types'
import { postToConnection } from '../utils/postToConnection'
import { buildContext } from '../utils/buildContext'
import { getResolverAndArgs } from '../utils/getResolverAndArgs'
import { isArray } from '../utils/isArray'
import { getFilteredSubs } from './getFilteredSubs'
export const complete = (serverPromise: Promise<ServerClosure> | ServerClosure): SubscriptionServer['complete'] => async (event, excludeKeys) => {
const server = await serverPromise
const subscriptions = await getFilteredSubs({ server, event, excludeKeys })
server.log('pubsub:complete', { event, subscriptions })
const iters = subscriptions.map(async (sub) => {
const message: CompleteMessage = {
id: sub.subscriptionId,
type: MessageType.Complete,
}
await postToConnection(server)({
...sub.requestContext,
message,
})
await server.models.subscription.delete({ id: sub.id })
const execContext = buildExecutionContext({
schema: server.schema,
document: parse(sub.subscription.query),
contextValue: await buildContext({ server, connectionInitPayload: sub.connectionInitPayload, connectionId: sub.connectionId }),
variableValues: sub.subscription.variables,
operationName: sub.subscription.operationName,
})
if (isArray(execContext)) {
throw new AggregateError(execContext)
}
const { field, root, args, context, info } = getResolverAndArgs({ execContext })
const onComplete = (field?.subscribe as SubscribePseudoIterable<PubSubEvent>)?.onComplete
server.log('pubsub:complete:onComplete', { onComplete: !!onComplete })
await onComplete?.(root, args, context, info)
})
await Promise.all(iters)
}