Skip to content

Commit 754487d

Browse files
authored
feat: Client (enisdenjo#3)
1 parent 8381796 commit 754487d

File tree

6 files changed

+1191
-0
lines changed

6 files changed

+1191
-0
lines changed

README.md

+55
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,61 @@ fastify.listen(4000);
122122
console.log('Listening to port 4000');
123123
```
124124

125+
#### Use the client
126+
127+
```ts
128+
import { createClient } from 'graphql-sse';
129+
130+
const client = createClient({
131+
url: 'http://welcomer.com:4000/graphql/stream',
132+
});
133+
134+
// query
135+
(async () => {
136+
const result = await new Promise((resolve, reject) => {
137+
let result;
138+
client.subscribe(
139+
{
140+
query: '{ hello }',
141+
},
142+
{
143+
next: (data) => (result = data),
144+
error: reject,
145+
complete: () => resolve(result),
146+
},
147+
);
148+
});
149+
150+
expect(result).toEqual({ hello: 'world' });
151+
})();
152+
153+
// subscription
154+
(async () => {
155+
const onNext = () => {
156+
/* handle incoming values */
157+
};
158+
159+
let unsubscribe = () => {
160+
/* complete the subscription */
161+
};
162+
163+
await new Promise((resolve, reject) => {
164+
unsubscribe = client.subscribe(
165+
{
166+
query: 'subscription { greetings }',
167+
},
168+
{
169+
next: onNext,
170+
error: reject,
171+
complete: resolve,
172+
},
173+
);
174+
});
175+
176+
expect(onNext).toBeCalledTimes(5); // we say "Hi" in 5 languages
177+
})();
178+
```
179+
125180
## [How does it work?](PROTOCOL.md)
126181

127182
Read about the exact transport intricacies used by the library in the [GraphQL over Server-Sent Events Protocol document](PROTOCOL.md).

0 commit comments

Comments
 (0)