Skip to content

Instantly share code, notes, and snippets.

@0x77dev
Last active April 5, 2022 19:23
Show Gist options
  • Save 0x77dev/af60ef51e5405393c311f652ddd14289 to your computer and use it in GitHub Desktop.
Save 0x77dev/af60ef51e5405393c311f652ddd14289 to your computer and use it in GitHub Desktop.
mercurius-js/mercurius #370
import Fastify from 'fastify'
import cors from 'fastify-cors'
import mercurius from 'mercurius'
const app = Fastify()
const schema = `
extend type Query {
b: Boolean!
}
extend type Subscription {
counter: Float!
}
`
const resolvers = {
Subscription: {
counter: {
// @ts-ignore
resolve: (data) => {
return data.counter
},
// @ts-ignore
subscribe: async (root, args, { pubsub }) => {
if (pubsub) {
setInterval(() => pubsub.publish({ topic: "counter", payload: { counter: Math.random() } }), 1000)
return await pubsub.subscribe('counter')
}
},
},
},
Query: {
b: () => true
}
}
app.register(cors, { origin: '*' })
app.register(mercurius, {
schema,
resolvers,
federationMetadata: true,
graphiql: 'playground',
logLevel: 'trace',
path: '/',
subscription: {}
})
app.listen(3001)
import Fastify from 'fastify'
import cors from 'fastify-cors'
import mercurius from 'mercurius'
const rewriteHeaders = (headers: any) => {
if (headers.authorization) {
return {
authorization: headers.authorization,
}
}
return {}
}
const services = [
{
mandatory: true,
name: 'first',
rewriteHeaders,
url: process.env.FIRST_SERVICE || 'http://127.0.0.1:3001',
},
{
mandatory: true,
name: 'second',
rewriteHeaders,
url: process.env.SECOND_SERVICE || 'http://127.0.0.1:3002',
},
]
const start = async () => {
try {
const fastify = Fastify()
fastify.register(mercurius, {
gateway: {
services: services.map((service) => {
return {
...service,
wsUrl: service.url
.replace('http://', 'ws://')
.replace('https://', 'wss://'),
}
}),
},
graphiql: 'playground',
logLevel: 'trace',
path: '/',
routes: true,
subscription: true,
})
fastify.get('/health', async () => {
return {}
})
fastify.register(cors, { origin: '*' })
const url = await fastify.listen(process.env.PORT || 3030)
console.info('Listening on', url)
} catch (err) {
console.error(err)
}
}
start()
{
"dependencies": {
"fastify": "^3.9.2",
"fastify-cors": "^5.1.0",
"mercurius": "^6.9.0"
},
"devDependencies": {
"@types/node": "^14.14.20",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
}
import Fastify from 'fastify'
import cors from 'fastify-cors'
import mercurius from 'mercurius'
const app = Fastify()
const schema = `
extend type Query {
a: Boolean!
}
extend type Subscription {
random: String!
}
`
const resolvers = {
Subscription: {
random: {
// @ts-ignore
resolve: (data) => {
return data.random
},
// @ts-ignore
subscribe: async (root, args, { pubsub }) => {
if (pubsub) {
setInterval(() => pubsub.publish({ topic: "random", payload: { random: Math.random().toString(16) } }), 1000)
return await pubsub.subscribe('random')
}
},
},
},
Query: {
a: () => true
}
}
app.register(cors, { origin: '*' })
app.register(mercurius, {
schema,
resolvers,
federationMetadata: true,
graphiql: 'playground',
logLevel: 'trace',
path: '/',
subscription: {}
})
app.listen(3002)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment