-
-
Save simoneb/f1a61a1424f8ea7a38cc0606bfb33dcb to your computer and use it in GitHub Desktop.
mercurius-js/mercurius #424
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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