npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

groupby-typename

v1.0.0

Published

Type-safe and tested utility to easily group an array of GraphQL objects by their typename

Downloads

1,459

Readme

GraphQL can return a list of objects of different types (nodes or union of types). This utility allows you to group them by their __typename field, but also ensure that they are typed accordingly.

By default, using a naive group by doesn't offer a type-safe way to access the grouped objects. Let's say we have an array that contains User and Post: Array<User | Post>, we would expect the returned type to be something like:

{
  User?: User[];
  Post?: Post[];
}

Unfortunately, it won't be the case, it will either be Record<string, (User | Post)[]> or Record<"User" | "Post", (User | Post)[]>.

Leading to confusing usages where accessing a key for a given __typename won't narrow down the type:

const grouped = Object.groupBy(data, (item) => item.__typename);
//    ^? Partial<Record<"User" | "Post", (User | Post)[]>>

const users = grouped.User;
//    ^? const users: (User | Post)[] | undefined

See this example in TypeScript Playground.

This utility aims to solve this issue by providing a type-safe way to access the grouped objects.

Installation

npm install groupby-typename

Usage

[!NOTE] This utility is built with Object.groupBy—which is part of the new baseline for 2024. Currently supported by all major browsers, but requires TypeScript 5.4 and above (you will need to target ESNext or the upcoming ES2024), or Node 21 and above.

If you do not meet those requirements, don't worry, the utility will fallback to a legacy implementation based on a reducer.

import { groupByTypename } from 'groupby-typename';

type User = { __typename: 'User'; id: number; name: string };
type Post = { __typename: 'Post'; id: number; title: string };

const data: Array<User | Post> = [
  { __typename: 'User', id: 1, name: 'Alice' },
  { __typename: 'User', id: 2, name: 'Bob' },
  { __typename: 'Post', id: 1, title: 'Hello, world!' },
  { __typename: 'Post', id: 2, title: 'GraphQL is awesome!' },
];

const grouped = groupByTypename(data);
//    ^? const grouped: { User?: User[]; Post?: Post[]; }

const users = grouped.User;
//    ^? const users: User[] | undefined

[!NOTE] Because they are not runtime types, the union of types is necessarily a superset of the types in the array at runtime. Therefore, the returned keys are always optional, as we can't know if at least one object of a given type is present in the array.

License

groupby-typename is MIT licensed.