Skip to content

Instantly share code, notes, and snippets.

@MuddyBootsCode
Last active September 23, 2020 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MuddyBootsCode/c77526894787e3325422e0c7ed0ea5cf to your computer and use it in GitHub Desktop.
Save MuddyBootsCode/c77526894787e3325422e0c7ed0ea5cf to your computer and use it in GitHub Desktop.
export class SubscribeDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
field.subscribe = () => pubsub.asyncIterator(field.name);
}
visitObject(object) {
return super.visitObject(object);
}
}
export class PublishDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
const { to } = this.args;
const { resolve } = field;
field.resolve = (...args) => {
const data = resolve.apply(this, args);
pubsub.publish(to, { [to]: data });
return data;
};
}
// This was my attempt to make it work with objects and not just field definitions
visitObject(object) {
const { to } = this.args;
const fields = object.getFields();
Object.keys(fields).forEach((fieldName) => {
const field = fields[fieldName];
const next = field.resolve;
field.resolve = (result, args, context, info) => {
pubsub.publish(to, {[to]: result})
return next(result, args, context, info);
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment