Skip to content

Instantly share code, notes, and snippets.

@blocka
Last active November 24, 2020 18:08
Show Gist options
  • Save blocka/03087e7f0729047fbfff70c1bd45aaab to your computer and use it in GitHub Desktop.
Save blocka/03087e7f0729047fbfff70c1bd45aaab to your computer and use it in GitHub Desktop.
waitForSubscription helper function for testing graphql subscriptions
export function waitForSubscription<TResult, TContext>(schema: GraphQLSchema, subscription: DocumentNode, context: TContext) {
return new Promise<TResult | ReadonlyArray<GraphQLError>>(async resolve => {
const iterator = await subscribe(schema, subscription, {}, context)
if (isAsyncIterable(iterator)) {
for await (const next of iterator) {
const val: ExecutionResult<TResult> = next
if (val.errors) resolve(val.errors)
const result = val.data
if (result) resolve(result)
}
}
if ('errors' in iterator) {
resolve(iterator.errors)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment