Last active
September 23, 2020 11:23
-
-
Save MuddyBootsCode/c77526894787e3325422e0c7ed0ea5cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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