Last active
November 24, 2020 18:08
-
-
Save blocka/03087e7f0729047fbfff70c1bd45aaab to your computer and use it in GitHub Desktop.
waitForSubscription helper function for testing graphql subscriptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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