-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add suppoort for graphql-transport-ws with duplex ping-pong
- Loading branch information
Showing
11 changed files
with
697 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
### chat app | ||
# Chat App | ||
|
||
Example app using subscriptions to build a chat room. | ||
|
||
to run this server | ||
### Server | ||
```bash | ||
go run ./example/chat/server/server.go | ||
``` | ||
|
||
to run the react app | ||
### Client | ||
The react app uses two different implementation for the websocket link | ||
- [apollo-link-ws](https://www.apollographql.com/docs/react/api/link/apollo-link-ws) which uses the deprecated [subscriptions-transport-ws](https://github.com/apollographql/subscriptions-transport-ws) library | ||
- [graphql-ws](https://github.com/enisdenjo/graphql-ws) | ||
|
||
First you need to install the dependencies | ||
```bash | ||
cd ./example/chat | ||
npm install | ||
``` | ||
|
||
Then to run the app with the `apollo-link-ws` implementation do | ||
```bash | ||
npm run start | ||
``` | ||
|
||
or to run the app with the `graphql-ws` implementation do | ||
```bash | ||
npm run start:graphql-transport-ws | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createClient } from 'graphql-ws'; | ||
import { print } from 'graphql'; | ||
import { ApolloLink, Observable } from '@apollo/client'; | ||
|
||
export class WebSocketLink extends ApolloLink { | ||
client; | ||
|
||
constructor(options) { | ||
super(); | ||
this.client = createClient(options); | ||
} | ||
|
||
request(operation) { | ||
return new Observable((sink) => { | ||
return this.client.subscribe( | ||
{ ...operation, query: print(operation.query) }, | ||
{ | ||
next: sink.next.bind(sink), | ||
complete: sink.complete.bind(sink), | ||
error: (err) => { | ||
if (err instanceof Error) { | ||
return sink.error(err); | ||
} | ||
|
||
if (err instanceof CloseEvent) { | ||
return sink.error( | ||
// reason will be available on clean closes | ||
new Error( | ||
`Socket closed with event ${err.code} ${err.reason || ''}`, | ||
), | ||
); | ||
} | ||
|
||
return sink.error( | ||
new Error( | ||
err | ||
.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
Oops, something went wrong.