Skip to content

Commit

Permalink
[CORL-1485] Revert #3241 (#3267)
Browse files Browse the repository at this point in the history
* fix: revert custom async iterator to resolve memory

* chore: bump 6.4.1
  • Loading branch information
wyattjoh authored Oct 30, 2020
1 parent 0a03d32 commit 4de2d60
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coralproject/talk",
"version": "6.4.0",
"version": "6.4.1",
"author": "The Coral Project",
"homepage": "https://coralproject.net/",
"sideEffects": [
Expand Down
47 changes: 4 additions & 43 deletions src/core/server/graph/resolvers/Subscription/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { GraphQLResolveInfo } from "graphql";
import { withFilter } from "graphql-subscriptions";

import GraphContext from "../../context";
import { SUBSCRIPTION_CHANNELS, SubscriptionPayload } from "./types";

type ResolverFn<TParent, TArgs, TContext> = (
parent: TParent,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo
) => AsyncIterable<any>;

type FilterFn<TParent, TArgs, TContext> = (
parent: TParent,
args: TArgs,
Expand All @@ -29,51 +23,18 @@ interface SubscriptionResolver<TParent, TArgs, TResult> {
resolve: Resolver<TParent, TArgs, TParent>;
}

/**
* withFilter applies a filter to a async iterator.
*
* This duplicates the functionality of the withFilter function provided by the
* `graphql-subscriptions` package without the memory leak as it uses native
* async iterators instead.
*
* Solution provided by @brettjashford.
*
* https://github.com/apollographql/graphql-subscriptions/pull/209#issuecomment-713906710
*
* @param asyncIteratorFn the async iterator to use that's provided by the transport
* @param filterFn the filter to apply for each iteration to check to see if we should sent it
*/
function withFilter<TParent, TArgs>(
asyncIteratorFn: ResolverFn<TParent, TArgs, GraphContext>,
filterFn: FilterFn<TParent, TArgs, GraphContext>
) {
return async function* (
source: TParent,
args: TArgs,
ctx: GraphContext,
info: GraphQLResolveInfo
) {
const asyncIterator = asyncIteratorFn(source, args, ctx, info);
for await (const payload of asyncIterator) {
if (await filterFn(payload, args, ctx, info)) {
yield payload;
}
}
};
}

function createTenantAsyncIterator<TParent, TArgs, TResult>(
channel: SUBSCRIPTION_CHANNELS
): Resolver<TParent, TArgs, AsyncIterable<TResult>> {
): Resolver<TParent, TArgs, AsyncIterator<TResult>> {
return (source, args, ctx) =>
// This is already technically returning an AsyncIterable, the Typescript
// types are in fact wrong:
//
// https://github.com/davidyaha/graphql-redis-subscriptions/pull/255
//
(ctx.pubsub.asyncIterator<TResult>(
ctx.pubsub.asyncIterator<TResult>(
createSubscriptionChannelName(ctx.tenant.id, channel)
) as unknown) as AsyncIterable<TResult>;
);
}

export function createSubscriptionChannelName(
Expand Down

0 comments on commit 4de2d60

Please sign in to comment.