Skip to content

Commit

Permalink
feat(subscriptions): @pgSubscription now supports filter (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo authored and benjie committed Jan 24, 2020
1 parent 9536750 commit b197408
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/pg-pubsub/src/PgSubscriptionResolverPlugin.ts
@@ -1,6 +1,6 @@
import debugFactory from "debug";
import { Plugin } from "graphile-build";
import { PubSub } from "graphql-subscriptions";
import { PubSub, withFilter } from "graphql-subscriptions";

const debug = debugFactory("pg-pubsub");

Expand Down Expand Up @@ -38,13 +38,14 @@ const PgSubscriptionResolverPlugin: Plugin = function(builder, { pubsub }) {
const {
topic: topicGen,
unsubscribeTopic: unsubscribeTopicGen,
filter,
} = pgSubscription;
if (!topicGen) {
return field;
}
return extend(field, {
subscribe: async (
_parent: any,
parent: any,
args: any,
resolveContext: any,
resolveInfo: any
Expand Down Expand Up @@ -94,6 +95,19 @@ const PgSubscriptionResolverPlugin: Plugin = function(builder, { pubsub }) {
});
}

if (filter) {
if (typeof filter !== "function") {
throw new Error(
"filter provided to pgSubscription must be a function"
);
}
return withFilter(() => asyncIterator, filter)(
parent,
args,
resolveContext,
resolveInfo
);
}
return asyncIterator;
},
...(field.resolve
Expand Down

0 comments on commit b197408

Please sign in to comment.