Skip to content

Commit d024757

Browse files
authored
feat: RedisEventTarget (#1299)
* feat: add redis event target draft * run tests on all EventTarget implementations * remove comments * fix: fallback error message * issa module * fix: polyfill event target on older platforms * chore: remove unused dependency * chore: changeset and documentation * chore: add redis pub sub example * chore: skip SvelteKit tests
1 parent 70c98bb commit d024757

File tree

24 files changed

+759
-163
lines changed

24 files changed

+759
-163
lines changed

.changeset/lazy-countries-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-yoga/typed-event-target': minor
3+
---
4+
5+
Initial release of this package. It contains an EventTarget implementation with generic typings.

.changeset/new-lobsters-scream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-yoga/subscription': minor
3+
---
4+
5+
Use `@graphql-yoga/typed-event-target` as a dependency for the EventTarget implementation.

.changeset/silly-lions-rest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-yoga/redis-event-target': minor
3+
---
4+
5+
Initial release of this package. It contains an EventTarget implementation based upon Redis Pub/Sub using ioredis.

examples/redis-pub-sub/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Redis Pub/Sub Example
2+
3+
## Usage instructions
4+
5+
Start Redis with Docker
6+
7+
```bash
8+
docker run -p "6379:6379" redis:7.0.2
9+
```
10+
11+
Start two server instances running on different ports
12+
13+
```bash
14+
PORT=4000 yarn workspace example-redis-pub-sub start
15+
PORT=4001 yarn workspace example-redis-pub-sub start
16+
```
17+
18+
Visit and set up the subscription by pressing the Play button.
19+
20+
```bash
21+
http://127.0.0.1:4000/graphql?query=subscription+%7B%0A++message%0A%7D
22+
```
23+
24+
Visit and execute the mutation by pressing the Play button.
25+
26+
```bash
27+
http://127.0.0.1:4001/graphql?query=mutation+%7B%0A++sendMessage%28message%3A+%22Yo+we+share+a+redis+instance.%22%29%0A%7D
28+
```
29+
30+
See your subscription update appear on `127.0.0.1:4000`, even though you executed the mutation on a different Node.js server instance running on `127.0.0.1:4001`.
31+
32+
The magic of Redis. 🪄✨
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "example-redis-pub-sub",
3+
"version": "0.0.0",
4+
"private": true,
5+
"description": "",
6+
"scripts": {
7+
"dev": "cross-env NODE_ENV=development ts-node-dev --exit-child --respawn src/main.ts",
8+
"start": "ts-node src/main.ts",
9+
"check": "tsc --pretty --noEmit"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"devDependencies": {
15+
"@types/node": "16.11.7",
16+
"cross-env": "7.0.3",
17+
"ts-node": "10.8.1",
18+
"ts-node-dev": "1.1.8",
19+
"typescript": "4.7.4"
20+
},
21+
"dependencies": {
22+
"@graphql-yoga/node": "2.12.0",
23+
"@graphql-yoga/redis-event-target": "0.0.0",
24+
"graphql": "16.5.0",
25+
"ioredis": "5.0.6"
26+
},
27+
"module": "commonjs"
28+
}

examples/redis-pub-sub/src/main.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { createServer, createPubSub } from '@graphql-yoga/node'
2+
import { createRedisEventTarget } from '@graphql-yoga/redis-event-target'
3+
import Redis from 'ioredis'
4+
5+
const publishClient = new Redis()
6+
const subscribeClient = new Redis()
7+
8+
const pubSub = createPubSub<{
9+
message: [string]
10+
}>({
11+
eventTarget: createRedisEventTarget({
12+
publishClient,
13+
subscribeClient,
14+
}),
15+
})
16+
17+
const server = createServer<{ pubSub: typeof pubSub }>({
18+
context: () => ({ pubSub }),
19+
port: parseInt(process.env.PORT || '4000', 10),
20+
schema: {
21+
typeDefs: /* GraphQL */ `
22+
type Query {
23+
_: Boolean
24+
}
25+
26+
type Subscription {
27+
message: String!
28+
}
29+
30+
type Mutation {
31+
sendMessage(message: String!): Boolean
32+
}
33+
`,
34+
resolvers: {
35+
Subscription: {
36+
message: {
37+
subscribe: (_, __, context) => context.pubSub.subscribe('message'),
38+
resolve: (message) => message,
39+
},
40+
},
41+
Mutation: {
42+
sendMessage(_, { message }, context) {
43+
context.pubSub.publish('message', message)
44+
},
45+
},
46+
},
47+
},
48+
})
49+
50+
server.start()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
4+
"module": "commonjs" /* Specify what module code is generated. */,
5+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
6+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
7+
"strict": true /* Enable all strict type-checking options. */,
8+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
9+
}
10+
}

examples/sveltekit/__tests__/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import puppeteer from 'puppeteer';
33
let browser: puppeteer.Browser;
44
let page: puppeteer.Page;
55

6-
describe('SvelteKit integration', () => {
6+
describe.skip('SvelteKit integration', () => {
77
beforeAll(async () => {
88
browser = await puppeteer.launch({
99
// If you wanna run tests with open browser

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"workspaces": [
4040
"packages/*",
4141
"packages/plugins/*",
42+
"packages/event-target/*",
4243
"examples/**/*",
4344
"benchmark/*",
4445
"website",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# @graphql-yoga/redis-event-target
2+
3+
Do distributed GraphQL subscriptions over Redis.
4+
5+
[Learn more about GraphQL Subscriptions.](https://www.graphql-yoga.com/docs/features/subscriptions)

0 commit comments

Comments
 (0)