-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for websocket endpoint with graphql-ws
- Loading branch information
Showing
7 changed files
with
150 additions
and
16 deletions.
There are no files selected for viewing
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
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
43 changes: 43 additions & 0 deletions
43
packages/graphql-playground-react/src/state/sessions/WebSocketLink.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ApolloLink, Operation, FetchResult, Observable } from 'apollo-link'; | ||
import { print, GraphQLError } from 'graphql'; | ||
import { Client } from 'graphql-ws'; | ||
|
||
export class WebSocketLink extends ApolloLink { | ||
|
||
constructor(private client: Client) { | ||
super(); | ||
} | ||
|
||
public request(operation: Operation): Observable<FetchResult> { | ||
return new Observable((sink) => { | ||
return this.client.subscribe<FetchResult>( | ||
{ ...operation, query: print(operation.query) }, | ||
{ | ||
next: sink.next.bind(sink), | ||
complete: sink.complete.bind(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 | ||
? `: ${err.reason}` // reason will be available on clean closes | ||
: '', | ||
), | ||
); | ||
} else { | ||
sink.error( | ||
new Error( | ||
(err as GraphQLError[]) | ||
.map(({ message }) => message) | ||
.join(', '), | ||
), | ||
); | ||
} | ||
}, | ||
}, | ||
); | ||
}); | ||
} | ||
} |
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
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
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
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