Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CORL-1485] Revert #3241 #3267

Merged
merged 2 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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