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

Add a(n optional) generic type map to PubSub. #245

Merged
merged 4 commits into from
Nov 25, 2021

Conversation

k-tten
Copy link
Contributor

@k-tten k-tten commented Jun 25, 2021

The class PubSub now has a generic type parameter so its methods publish and subscribe can be optionally type-checked by TypeScript.

  • has-reproduction
  • feature
  • blocking
  • good first review

/label typedefs

I was using apollo-server-express but the class PubSub did not have any compile time validation by TypeScript. So I extended the class and added a generic. I thought I would contribute to the project back since it will be useful to others.

New usage:

import { PubSub } from "apollo-server-express";

const pubsub = new PubSub<{
    EVENT_ONE: { data: number; };
    EVENT_TWO: { data: string; };
}>();

pubsub.publish("EVENT_ONE", { data: 42 });
pubsub.publish("EVENTONE", { data: 42 }); // ! ERROR
pubsub.publish("EVENT_ONE", { data: "42" }); // ! ERROR
pubsub.publish("EVENT_TWO", { data: "hello" });

pubsub.subscribe("EVENT_ONE", () => {});
pubsub.subscribe("EVENTONE", () => {}); // ! ERROR
pubsub.subscribe("EVENT_TWO", () => {});

const oldPubSub = new PubSub(); // => Still completely fine and should act as if the optional generic was never added.

Copy link

@cursedstuff cursedstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Updating the examples on the README.md would be good as well.

@k-tten
Copy link
Contributor Author

k-tten commented Sep 25, 2021

Yep! I updated the README as well to feature this if it ever gets merged.

@k-tten
Copy link
Contributor Author

k-tten commented Nov 9, 2021

Related: #223

@n1ru4l
Copy link
Contributor

n1ru4l commented Nov 23, 2021

@glasser This is something that should be added to the v3.0 release: https://github.com/apollographql/graphql-subscriptions/milestone/1

This has been out here for a while: https://github.com/contra/typed-graphql-subscriptions

@hwillson hwillson added this to the Release 3.0 milestone Nov 25, 2021
@hwillson hwillson changed the base branch from master to release-3.0 November 25, 2021 17:34
Copy link
Member

@hwillson hwillson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @cursorsdottsx!

k-tten and others added 3 commits November 25, 2021 12:36
The class PubSub now has a generic type parameter so its methods `publish` and `subscribe` can be **optionally** type-checked by TypeScript.
@hwillson hwillson merged commit 8e8d511 into apollographql:release-3.0 Nov 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants