Skip to content

Commit

Permalink
fix(ws): Subscriptions async iterator completes and better error hand…
Browse files Browse the repository at this point in the history
…ling (#1841)

Closes #1827

* fix: simple sub async iterator with better error handling
* fix: typings
* refactor: trim close event message for nicer display
* refactor: update and use @n1ru4l/push-pull-async-iterable-iterator
* chore: patch
* fix: gql error map in else
* refactor: update @n1ru4l/push-pull-async-iterable-iterator
* chore: clearer patch
  • Loading branch information
enisdenjo committed Apr 21, 2021
1 parent 65aa648 commit 94f1695
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-scissors-bow.md
@@ -0,0 +1,5 @@
---
'@graphiql/toolkit': patch
---

Subscriptions async iterator completes and better error handling
2 changes: 1 addition & 1 deletion packages/graphiql-toolkit/package.json
Expand Up @@ -20,7 +20,7 @@
"typings": "dist/index.d.ts",
"scripts": {},
"dependencies": {
"@n1ru4l/push-pull-async-iterable-iterator": "^2.0.1",
"@n1ru4l/push-pull-async-iterable-iterator": "^2.1.4",
"graphql-ws": "^4.3.2",
"meros": "^1.1.4"
},
Expand Down
23 changes: 21 additions & 2 deletions packages/graphiql-toolkit/src/create-fetcher/lib.ts
@@ -1,4 +1,4 @@
import { DocumentNode, visit } from 'graphql';
import { DocumentNode, visit, GraphQLError } from 'graphql';
import { meros } from 'meros';
import { createClient, Client, ClientOptions } from 'graphql-ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';
Expand Down Expand Up @@ -94,7 +94,26 @@ export const createWebsocketsFetcherFromClient = (wsClient: Client) => (
graphQLParams: FetcherParams,
) =>
makeAsyncIterableIteratorFromSink<FetcherResult>(sink =>
wsClient!.subscribe(graphQLParams, sink),
wsClient!.subscribe(graphQLParams, {
...sink,
error: err => {
if (err instanceof Error) {
sink.error(err);
} else if (err instanceof CloseEvent) {
sink.error(
new Error(
`Socket closed with event ${err.code} ${err.reason || ''}`.trim(),
),
);
} else {
sink.error(
new Error(
(err as GraphQLError[]).map(({ message }) => message).join(', '),
),
);
}
},
}),
);

export const createLegacyWebsocketsFetcher = (
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -2683,10 +2683,10 @@
call-me-maybe "^1.0.1"
glob-to-regexp "^0.3.0"

"@n1ru4l/push-pull-async-iterable-iterator@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@n1ru4l/push-pull-async-iterable-iterator/-/push-pull-async-iterable-iterator-2.0.1.tgz#8fc68e0e9e8bd3826d727b96ddd2afba2a7f26c7"
integrity sha512-3nDfpI/4WZgdocw75cxG4Qwmi3yp5zOleaT23EpVUhwVwlYzb4kDojl0nCZKB5lCGcTqT5wFlKepShs4Q+hymw==
"@n1ru4l/push-pull-async-iterable-iterator@^2.1.4":
version "2.1.4"
resolved "https://registry.yarnpkg.com/@n1ru4l/push-pull-async-iterable-iterator/-/push-pull-async-iterable-iterator-2.1.4.tgz#a90225474352f9f159bff979905f707b9c6bcf04"
integrity sha512-qLIvoOUJ+zritv+BlzcBMePKNjKQzH9Rb2i9W98YXxf/M62Lye8qH0peyiU8yJ1tL0kfulWi31BoK10E6BKJeA==

"@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents":
version "2.1.8-no-fsevents"
Expand Down

0 comments on commit 94f1695

Please sign in to comment.