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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nmarks/graphql-codegen-per-operation-file-preset

v1.0.10

Published

GraphQL Code Generator preset for generating one file per operation/fragment

Downloads

2,746

Readme

@graphql-codegen/per-operation-file-preset

Generate one file per GraphQL operation or fragment, named after the operation/fragment itself.

Installation

npm install @graphql-codegen/per-operation-file-preset

Usage

// codegen.ts
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  schema: 'schema.graphql',
  documents: 'src/**/*.ts',
  generates: {
    'src/': {
      preset: 'per-operation-file',
      presetConfig: {
        baseTypesPath: 'types.ts',
      },
      plugins: ['typescript-operations'],
    },
  },
};
export default config;

How It Works

Unlike the near-operation-file preset which generates one file per source file, this preset generates one file per operation/fragment.

Example

Input:

// src/queries/user.ts
export const GetUserQuery = gql`
  query GetUser($id: ID!) {
    user(id: $id) {
      id
      name
    }
  }
`;

export const GetUsersQuery = gql`
  query GetUsers {
    users {
      id
      name
    }
  }
`;

Output:

src/queries/__generated__/GetUser.ts    # Generated types for GetUser query
src/queries/__generated__/GetUsers.ts   # Generated types for GetUsers query

Fragments

Input:

// src/fragments/user.ts
export const UserFieldsFragment = gql`
  fragment UserFields on User {
    id
    name
    email
  }
`;

Output:

src/fragments/__generated__/UserFields.ts

Configuration

baseTypesPath (required)

Path to your base schema types file (generated by the typescript plugin).

presetConfig: {
  baseTypesPath: 'types.ts'
}

You can also use npm packages:

presetConfig: {
  baseTypesPath: '~@myapp/graphql-types'
}

folder (optional)

Folder name for generated files, relative to the source file location.

Default: __generated__

presetConfig: {
  baseTypesPath: 'types.ts',
  folder: 'generated'  // Use 'generated' instead of '__generated__'
}

extension (optional)

File extension for generated files.

Default: .ts

presetConfig: {
  baseTypesPath: 'types.ts',
  extension: '.generated.ts'
}

importTypesNamespace (optional)

Namespace for importing base schema types.

Default: Types

presetConfig: {
  baseTypesPath: 'types.ts',
  importTypesNamespace: 'SchemaTypes'
}

cwd (optional)

Override the current working directory.

Default: process.cwd()

presetConfig: {
  baseTypesPath: 'types.ts',
  cwd: '/custom/path'
}

Fragment Imports

The preset automatically handles fragment imports. If an operation uses a fragment from another file, it will generate the correct import statement.

Example:

// src/fragments/user.ts
fragment UserFields on User { id name }

// src/queries/user.ts
query GetUser($id: ID!) {
  user(id: $id) {
    ...UserFields
  }
}

Generated:

// src/queries/__generated__/GetUser.ts
import { UserFieldsFragment, UserFieldsFragmentDoc } from '../../fragments/__generated__/UserFields';
// ... rest of generated code

Comparison with near-operation-file

| Feature | near-operation-file | per-operation-file | |---------|-------------------|-------------------| | File Strategy | One file per source file | One file per operation/fragment | | Output Naming | Based on source filename | Based on operation/fragment name | | Multiple Operations | Combined in one file | Split into separate files | | Fragment Imports | ✅ Supported | ✅ Supported | | Use Case | Co-location with source | Migration from other tools, explicit operation files |

When to Use This Preset

Use per-operation-file when:

  • ✅ Migrating from tools that generate one file per operation
  • ✅ You want explicit, discoverable operation files
  • ✅ You have multiple operations per source file
  • ✅ You want operation names to match generated file names

Use near-operation-file when:

  • ✅ You want generated files to mirror your source structure
  • ✅ You prefer one-to-one mapping between source and generated files
  • ✅ You typically have one operation per source file

License

MIT