Skip to content

Instantly share code, notes, and snippets.

@nigelhanlon
Created January 14, 2021 16:31
Show Gist options
  • Save nigelhanlon/defbf4f34e6993bbcad623df6ca5be98 to your computer and use it in GitHub Desktop.
Save nigelhanlon/defbf4f34e6993bbcad623df6ca5be98 to your computer and use it in GitHub Desktop.
import Fastify from 'fastify'
import cors from 'fastify-cors'
import mercurius from 'mercurius'
const app = Fastify()
const schema = `
type Counter {
count: Float!
}
extend type Query {
b: Boolean!
}
extend type Subscription {
counter: Counter!
}
`
const resolvers = {
Subscription: {
counter: {
subscribe: async (_, __, { pubsub }: any) => {
if(pubsub) {
setInterval(() => pubsub.publish({
topic: "counter",
payload: { counter: { count: Math.random() }}
}), 1000)
return await pubsub.subscribe('counter')
}
}
}
// counter: {
// // @ts-ignore
// resolver: (data) => {
// console.log(data)
// return data.counter
// },
// subscribe: async (root, args, { pubsub }) => {
// console.log({root, args, pubsub})
// if (pubsub) {
// console.log('calling subscribe')
// 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',
},
// Commented out second service for easy debugging.
// {
// 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"
}
}
//
// Added for completeness but has not been changed for this example gist.
//
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