Skip to content

Instantly share code, notes, and snippets.

@simoneb
Forked from 0x77dev/mercurius-issue-424.ts
Last active June 7, 2021 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simoneb/f1a61a1424f8ea7a38cc0606bfb33dcb to your computer and use it in GitHub Desktop.
Save simoneb/f1a61a1424f8ea7a38cc0606bfb33dcb to your computer and use it in GitHub Desktop.
mercurius-js/mercurius #424
const Fastify = require('fastify')
const mercurius = require('mercurius')
const example = Fastify()
const schema = `
extend type Query {
test: Boolean!
}
extend type Mutation {
addTestEvent(userId: Int!): Int!
}
type TestEvent {
userId: Int!
}
extend type Subscription {
testEvent: TestEvent!
}
`
const resolvers = {
Query: {
test: () => true,
},
Mutation: {
addTestEvent: async (_, { userId }, { pubsub }) => {
await pubsub.publish({
topic: `testEvent-${userId}`,
payload: { testEvent: { userId } },
})
return userId
},
},
Subscription: {
testEvent: {
subscribe: async (_, args, { pubsub, userId }) => {
return await pubsub.subscribe(`testEvent-${userId}`)
},
},
},
}
example.register(mercurius, {
schema,
resolvers,
path: '/',
graphiql: 'playground',
subscription: {
onConnect: ({ payload: { userId } }) => {
if (userId > 2) {
return null
}
return {
userId,
}
},
},
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