🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

graphql-code-generator-plugin-typescript-typename-typeguards

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-code-generator-plugin-typescript-typename-typeguards

A plugin for graphql code generator that generate user defined type guards with __typename

0.1.0
latest
Source
npm
Version published
Weekly downloads
5
400%
Maintainers
1
Weekly downloads
 
Created
Source

GraphQL Code Generator Plugin Typescript Typename Typeguards

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

Installation

npm install -D graphql-code-generator-plugin-typescript-typename-typeguards

Usage

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

Example

Input

type User {
  id: ID!
  name: String!
  age: Int!
  email: String!
  address: Address!
  friends: [User!]!
}

type Address {
  country: String!
  prefecture: String!
  city: String!
  street: String!
}

Output

export const isUser = (field: { __typename?: string }): field is User =>
  field.__typename === "User";

export const isAddress = (field: { __typename?: string }): field is Address =>
  field.__typename === "Address";

Config

argsAsStringLiteralUnion

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";

License(MIT)

See LICENSE

Keywords

graphql

FAQs

Package last updated on 17 Aug 2023

Did you know?

Socket

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.

Install

Related posts