CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-graphql-codegen--typescript

GraphQL Code Generator plugin for generating TypeScript types from GraphQL schemas

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

utility-types.mddocs/

Utility Types

Pre-defined TypeScript utility type signatures for advanced type manipulation, exact type matching, and GraphQL-specific type operations. These constants provide ready-to-use TypeScript utility types.

Capabilities

Type Signature Constants

Pre-defined string constants containing TypeScript utility type definitions that can be included in generated code.

/**
 * Type signature for exact type matching
 * Ensures all properties match exactly without excess properties
 */
const EXACT_SIGNATURE: string;

/**
 * Type signature for making properties optional with Maybe wrapper
 * Converts specified keys to optional with Maybe<T> type
 */
const MAKE_OPTIONAL_SIGNATURE: string;

/**
 * Type signature for making properties maybe (nullable)  
 * Converts specified keys to Maybe<T> type while keeping them required
 */
const MAKE_MAYBE_SIGNATURE: string;

/**
 * Type signature for making properties empty/never
 * Useful for removing properties from types
 */
const MAKE_EMPTY_SIGNATURE: string;

/**
 * Type signature for incremental delivery support
 * Supports GraphQL @defer and @stream directives
 */
const MAKE_INCREMENTAL_SIGNATURE: string;

Exact Type Signature

Provides precise type matching that prevents excess properties.

const EXACT_SIGNATURE = `type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };`;

Usage Examples:

// Generated type with exact matching
type CreateUserInput = Exact<{
  name: string;
  email: string;
  age?: number;
}>;

// This works
const validInput: CreateUserInput = {
  name: "John",
  email: "john@example.com",
  age: 30
};

// This fails - excess property 'id'
const invalidInput: CreateUserInput = {
  name: "John", 
  email: "john@example.com",
  id: "123" // Error: Object literal may only specify known properties
};

Optional Type Signature

Converts specified properties to optional with Maybe wrapper.

const MAKE_OPTIONAL_SIGNATURE = `type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };`;

Usage Examples:

type User = {
  id: string;
  name: string;
  email: string;
  age: number;
};

// Make email and age optional
type PartialUser = MakeOptional<User, 'email' | 'age'>;
// Result: { id: string; name: string; email?: Maybe<string>; age?: Maybe<number>; }

Maybe Type Signature

Converts specified properties to Maybe (nullable) while keeping them required.

const MAKE_MAYBE_SIGNATURE = `type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };`;

Usage Examples:

type User = {
  id: string;
  name: string;
  email: string;
  age: number;
};

// Make email and age nullable but still required
type UserWithNullables = MakeMaybe<User, 'email' | 'age'>;
// Result: { id: string; name: string; email: Maybe<string>; age: Maybe<number>; }

const user: UserWithNullables = {
  id: "123",
  name: "John",
  email: null, // Valid
  age: null    // Valid
};

Empty Type Signature

Makes specified properties never/empty, effectively removing them.

const MAKE_EMPTY_SIGNATURE = `type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };`;

Usage Examples:

type User = {
  id: string;
  name: string;
  email: string;
  password: string;
};

// Remove sensitive fields
type PublicUser = Omit<User, 'password'> & MakeEmpty<User, 'password'>;
// Result: { id: string; name: string; email: string; password?: never; }

const publicUser: PublicUser = {
  id: "123",
  name: "John",
  email: "john@example.com"
  // password cannot be included
};

Incremental Type Signature

Supports GraphQL incremental delivery features (@defer and @stream directives).

const MAKE_INCREMENTAL_SIGNATURE = `type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };`;

Usage Examples:

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

// Support incremental delivery
type IncrementalUser = Incremental<User>;
// Allows partial objects with __typename preserved

const incrementalUser: IncrementalUser = {
  __typename: 'User'
  // Other fields can be delivered incrementally
};

Type Signature Usage in Generated Code

These signatures are typically included in the generated TypeScript output when specific configurations are enabled:

// In generated code prepend section
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };

// Generated types using utilities
export type CreateUserInput = Exact<{
  name: Scalars['String']['input'];
  email: Scalars['String']['input'];
  age?: InputMaybe<Scalars['Int']['input']>;
}>;

Combining Utility Types

These utility types can be combined for complex type manipulations:

type User = {
  id: string;
  name: string;
  email: string;
  password: string;
  createdAt: Date;
  updatedAt: Date;
};

// Create a type for user updates
type UserUpdate = MakeOptional<
  MakeEmpty<User, 'id' | 'createdAt'>,
  'email' | 'password' | 'updatedAt'
>;
// Result: {
//   id?: never;
//   name: string;
//   email?: Maybe<string>;
//   password?: Maybe<string>;
//   createdAt?: never;
//   updatedAt?: Maybe<Date>;
// }

docs

configuration.md

index.md

type-generation.md

utility-types.md

visitors.md

tile.json