-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsubscriptions.ts
40 lines (36 loc) · 1.1 KB
/
subscriptions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { g, InferResolvers, buildSchema, Infer } from './../src/index'
import { createYoga } from 'graphql-yoga'
import { createServer } from 'http'
const queryType = g.type('Query', {
greet: g.string()
})
const subscriptionType = g.type('Subscription', {
counter: g.int(),
winner: g.string()
})
const resolvers: InferResolvers<{ Subscription: typeof subscriptionType }, {}> = {
Subscription: {
counter: {
subscribe: async function* (parent, args, context, info) {
for (let i = 100; i >= 0; i--) {
await new Promise((resolve) => setTimeout(resolve, 1000))
yield { counter: i }
}
}
},
winner: {
subscribe: async function* (parent, args, context, info) {
for (let i = 100; i >= 0; i--) {
await new Promise((resolve) => setTimeout(resolve, 1000))
yield { winner: `User ${i}` }
}
}
}
}
}
const schema = buildSchema({ g, resolvers })
const yoga = createYoga({ schema })
const server = createServer(yoga)
server.listen(4000, () => {
console.info('Server is running on http://localhost:4000/graphql')
})