
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
graphql-code-generator-plugin-typescript-typename-typeguards
Advanced tools
A plugin for graphql code generator that generate user defined type guards with __typename
This is a plugin for GraphQL Code Generator.
Generate user defined type guard functions from user defined type of schema.
What is a user defined type guard function? https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
npm install -D graphql-code-generator-plugin-typescript-typename-typeguards
import type { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
generates: {
"src/generated/graphql.ts": {
plugins: [
"typescript",
"graphql-code-generator-plugin-typescript-typename-typeguards",
],
},
},
};
export default config;
For GraphQL Code Generator v2
generates:
src/generated/graphql.ts:
plugins:
- typescript
- graphql-code-generator-plugin-typescript-typename-typeguards
type User {
id: ID!
name: String!
age: Int!
email: String!
address: Address!
friends: [User!]!
}
type Address {
country: String!
prefecture: String!
city: String!
street: String!
}
export const isUser = (field: { __typename?: string }): field is User =>
field.__typename === "User";
export const isAddress = (field: { __typename?: string }): field is Address =>
field.__typename === "Address";
If true, the type guard function's args will be generated as a string literal union.
const config: CodegenConfig = {
generates: {
"src/generated/graphql.ts": {
plugins: [
"typescript",
"graphql-code-generator-plugin-typescript-typename-typeguards",
],
config: {
argsAsStringLiteralUnion: true,
},
},
},
};
export const isUser = (field: {
__typename?: "User" | "Address";
}): field is User => field.__typename === "User";
export const isAddress = (field: {
__typename?: "User" | "Address";
}): field is Address => field.__typename === "Address";
See LICENSE
FAQs
A plugin for graphql code generator that generate user defined type guards with __typename
The npm package graphql-code-generator-plugin-typescript-typename-typeguards receives a total of 4 weekly downloads. As such, graphql-code-generator-plugin-typescript-typename-typeguards popularity was classified as not popular.
We found that graphql-code-generator-plugin-typescript-typename-typeguards demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.