Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
groupby-typename
Advanced tools
Type-safe and tested utility to easily group an array of GraphQL objects by their typename
Type-safe and tested utility to easily group an array of GraphQL objects by their typename
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.
npm install groupby-typename
[!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 targetESNext
or the upcomingES2024
), 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.
groupby-typename is MIT licensed.
FAQs
Type-safe and tested utility to easily group an array of GraphQL objects by their typename
The npm package groupby-typename receives a total of 237 weekly downloads. As such, groupby-typename popularity was classified as not popular.
We found that groupby-typename demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.