Skip to content

Instantly share code, notes, and snippets.

@0x77dev
Last active July 25, 2021 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0x77dev/662309d334348758e19af329d2d9b0ec to your computer and use it in GitHub Desktop.
Save 0x77dev/662309d334348758e19af329d2d9b0ec to your computer and use it in GitHub Desktop.
mercurius-js/mercurius #424
import Fastify from "fastify";
import mercurius, { IResolvers, MercuriusContext } from "mercurius"
const example = Fastify()
const schema = `
extend type Query {
test: Boolean!
}
type TestEvent{
userId: Int!
}
extend type Subscription {
testEvent: TestEvent!
}
`
const resolvers: IResolvers<any, { userId?: string } & MercuriusContext> = {
Query: {
test: () => true
},
Subscription: {
testEvent: {
resolve: (data, args, { userId }) => {
console.log("resolve called with userId", userId)
return {userId}
},
// @ts-ignore
subscribe: async (root, args, { pubsub, userId }) => {
setInterval(() => pubsub.publish({topic: `testEvent-${userId}`, payload: {}}))
return await pubsub.subscribe(`testEvent-${userId}`)
}
}
}
}
example.register(mercurius, {
schema,
resolvers,
path: '/',
graphiql: 'playground',
subscription: {
onConnect: ({ payload }) => {
console.log("onConnect called with payload", payload)
return {
userId: payload.userId
}
},
onDisconnect: () => {
console.log("onDisconnect called")
}
},
federationMetadata: true,
})
example.listen(1026).then(() => {
const gateway = Fastify();
gateway.register(mercurius, {
gateway: {
services: [{ wsUrl: "ws://localhost:1026/", name: "example", url: "http://localhost:1026/" }]
},
path: '/',
graphiql: 'playground',
subscription: true
})
gateway.listen(1027)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment