Subscription Resolver Example
A subscription resolver commonly connects a GraphQL subscription field to an event source. The exact implementation depends on the GraphQL server and subscription transport being used.
Subscription Schema and Resolver
const typeDefs = `
type Message {
id: ID!
text: String!
}
type Subscription {
messageAdded: Message!
}
`;
const resolvers = {
Subscription: {
messageAdded: {
subscribe: () => pubsub.asyncIterator(["MESSAGE_ADDED"])
}
}
};
await pubsub.publish("MESSAGE_ADDED", {
messageAdded: newMessage
});💡 The subscription resolver connects clients to an event source, while application code publishes a payload when a new message is created. Production implementations may use a distributed event system rather than an in-memory publisher.