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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@pretto/vite-graphql-codegen-loader

v0.9.0

Published

Vite plugin for GraphQL code generation

Readme

@pretto/vite-graphql-codegen-loader

Vite plugin that generates TypeScript types and framework-specific Apollo hooks from GraphQL files during development and build.

Features

  • 🔥 Hot reload - Types and hooks generated on-the-fly
  • 🎯 Multiple schemas - Support for multiple GraphQL schemas
  • 📦 Smart fragment support - Automatic fragment loading with intelligent filtering (only includes used fragments)
  • 🚀 Multi-framework - React Apollo hooks & Vue Apollo composables
  • Vite optimized - Fast builds with caching
  • 🔄 Auto-generated schema files - Centralized enums.ts and schema.d.ts files regenerated on startup
  • 🏗️ Pre-generation - All GraphQL files are pre-generated at build start (no import required)
  • 🎯 Optimized output - Fragment tree-shaking ensures minimal file sizes

Installation

yarn add @pretto/vite-graphql-codegen-loader

Configuration

// vite.config.ts
import { graphqlCodegenPlugin } from "@pretto/vite-graphql-codegen-loader";

export default defineConfig({
  plugins: [
    graphqlCodegenPlugin({
      framework: "react", // Required: "react", "vue", or "none"
      debug: true, // Enable debug logs in development
      schemas: {
        api: {
          url: "https://api.example.com/graphql",
          match: "*.api.graphql", // Files ending with .api.graphql use this schema
        },
        gateway: {
          file: "./src/schema.graphql",
          match: "*.gateway.graphql", // Files ending with .gateway.graphql use this schema
          fragmentsPaths: [
            "./src/fragments/**/*.graphql", // Glob patterns supported
          ],
        },
      },
      schemaTypesPath: "./src/types",
      scalars: {
        DateTime: "string",
        Date: "string",
        JSON: "any",
      },
    }),
  ],
});

How it Works

Automatic Pre-generation

The plugin automatically pre-generates TypeScript types for ALL GraphQL files in your project at build start. This means:

  • ✅ You don't need to import GraphQL files before their types are available
  • ✅ No "chicken-and-egg" problem with circular dependencies
  • ✅ All hooks and types are immediately available for use

Smart Fragment Filtering

When using fragments, the plugin intelligently analyzes your GraphQL operations and only includes the fragments that are actually used:

  • 📉 Smaller generated files (only necessary fragments included)
  • 🔍 Recursive fragment resolution (nested fragments are properly detected)
  • ⚡ Better performance with minimal bundle size

Usage

Create GraphQL files with .api.graphql or .gateway.graphql extensions:

# queries.api.graphql
query GetUser($id: ID!) {
  user(id: $id) {
    id
    name
    email
  }
}

mutation UpdateUser($input: UserInput!) {
  updateUser(input: $input) {
    id
    name
  }
}

With Fragments

# user.gateway.graphql
query GetUserWithDetails {
  user {
    ...UserDetails  # Only this fragment will be included
  }
}

# fragments.graphql (configured in fragmentsPaths)
fragment UserDetails on User {
  id
  name
  profile {
    ...ProfileInfo  # Nested fragment also included
  }
}

fragment ProfileInfo on Profile {
  avatar
  bio
}

fragment UnusedFragment on User {  # This won't be included in generated file
  someField
}

The plugin automatically generates:

Centralized Schema Files (per schema)

  • src/types/api/enums.ts - All GraphQL enums from the API schema
  • src/types/api/schema.d.ts - All TypeScript types from the API schema
  • src/types/gateway/enums.ts - All GraphQL enums from the Gateway schema
  • src/types/gateway/schema.d.ts - All TypeScript types from the Gateway schema

These files are regenerated on every Vite startup, ensuring they stay up-to-date with the latest schema changes. You can safely add them to .gitignore.

Per-Query Files

// queries.api.graphql.d.ts - TypeScript types
export type GetUserQuery = { ... }
export type UpdateUserMutation = { ... }

// queries.api.graphql - JavaScript runtime
export const GetUserDocument = gql`...`
export function useGetUserQuery(options) { ... } // React hooks or Vue composables based on framework
export function useUpdateUserMutation(options) { ... }

Import and use:

React

import { useGetUserQuery, useUpdateUserMutation } from "./queries.api.graphql";

function UserProfile({ userId }: { userId: string }) {
  const { data, loading } = useGetUserQuery({ variables: { id: userId } });
  const [updateUser] = useUpdateUserMutation();

  // TypeScript knows the exact shape of data!
}

Vue

import { useGetUserQuery, useUpdateUserMutation } from "./queries.api.graphql";

export default {
  setup() {
    const { result, loading } = useGetUserQuery({ id: userId });
    const { mutate: updateUser } = useUpdateUserMutation();

    // TypeScript knows the exact shape of data!
    return { result, loading, updateUser };
  }
}

Configuration Options

| Option | Type | Default | Description | | ----------------- | ------------------------------ | ------------- | --------------------------------------------- | | framework | "react" \| "vue" \| "none" | required | Target framework for hook/composable generation | | debug | boolean | false | Enable debug logging | | schemas | Record<string, SchemaConfig> | {} | Schema configurations | | schemaTypesPath | string | "src/types" | Path for centralized schema files | | scalars | Record<string, string> | {} | Custom scalar type mappings |

Schema Configuration

interface SchemaConfig {
  url?: string;             // Remote GraphQL endpoint
  file?: string;            // Local schema file
  match?: string;           // File pattern to match (e.g., "*.api.graphql")
  fragmentsPaths?: string[]; // Fragment files to include (supports glob patterns)
}

Framework Options

  • react (default): Generates React Apollo hooks (useQuery, useMutation, etc.)
  • vue: Generates Vue Apollo composables (useQuery, useMutation, etc.)
  • none: Only generates TypeScript types without framework-specific hooks

Development

# Install dependencies
yarn install

# Development mode
yarn dev

# Build
yarn build

License

MIT