Skip to content

Instantly share code, notes, and snippets.

@samberic
Created December 1, 2022 12:55
Show Gist options
  • Save samberic/808e2fb94130c7122ab58ac25d6206a5 to your computer and use it in GitHub Desktop.
Save samberic/808e2fb94130c7122ab58ac25d6206a5 to your computer and use it in GitHub Desktop.
Minimum reproduce of mercurius issue
const Fastify = require('fastify');
const mercurius = require('mercurius');
const path = require('path');
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 }) => {
console.log('subscribe event, user is ' + userId);
return await pubsub.subscribe(`testEvent-${userId}`);
},
},
},
};
example.register(mercurius, {
schema,
resolvers,
path: '/',
graphiql: 'playground',
subscription: {
onConnect: ({ payload: { userId } }) => {
console.log('Sub connect for user: ' + 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: true,
subscription: true,
});
gateway.listen(1027);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment