apollographql / apollographql/graphql-subscriptions

Can the pubsub-async-iterator properties and methods be protected instead of private?

Open
#271 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.6k
Forks
129
PR merge metrics
No merged PRs in 30d

Description

I am trying to make a version of the pubsub async iterator that throws an error if the pushQueue goes over a certain size. Right now it is difficult to extend the existing pubsub async iterator to add this because most things are set to private and cannot be accessed by an overriding class.

Right now I am having to do this:

```typescript
class MaxQueuePubSubAsyncIterator extends PubSubAsyncIterator {
constructor(pubsub: PubSubEngine, eventNames: string | string[]) {
super(pubsub, eventNames)
// pushValue and pushQueue are private so this is a hack to access/override them.
const super_pushValue = this["pushValue"] as ((event:T) => Promise)
this["pushValue"] = async (event: T) => {
if ((this["pushQueue"] as T[]).length >= MAX_QUEUE) {
throw new MaxQueueError('Maximum Queue Size Reached')
}
return await super_pushValue(event)
}
}
}
```

However if `pushValue` and `pushQueue` were marked as protected instead of private, they would still be inaccessible in normal usage but could be accessed from extending classes and simplify this code quite a bit:

```typescript
class MaxQueuePubSubAsyncIterator extends PubSubAsyncIterator {
override async pushValue(event: T) {
if (this.pushQueue.length >= MAX_QUEUE) {
throw new MaxQueueError('Maximum Queue Size Reached')
}
return super.pushValue(event)
}
}
```

Contributor guide

Open the contributing guide

Research direction

Search the TypeScript source for the PubSubAsyncIterator class and the pushValue and pushQueue declarations. Update the access level so extending classes can use them while normal usage remains restricted, then verify that the provided MaxQueuePubSubAsyncIterator example type-checks and the existing tests still pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api, backend-api-design
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.