graphql-modules

The @graphql-codegen/graphql-modules-preset generates .ts file with TypeScript types, per each GraphQL-Modules module definition.

The generates files will be generated based on each module definition, and based on the GraphQL schema defined in that specific module, allowing you to write type-safe resolvers, while keeping modules types boundaries.

Usage Requirements

This preset generates code for graphql-modules @ v1. Previous versions are not supported.

Configuration

Installation

graphql-modules-preset plugin version
Using yarn
yarn add -D @graphql-codegen/graphql-modules-preset

API Reference

baseTypesPath

type: string

Required, should point to the base schema types file. The key of the output is used a the base path for this file.

cwd

type: string default: process.cwd()

Optional, override the cwd of the execution. We are using cwd to figure out the imports between files. Use this if your execuion path is not your project root directory.

importTypesNamespace

type: string default: Types

Optional, override the name of the import namespace used to import from the baseTypesPath file.

filename

type: string

Required, sets the file name for the generated files.

encapsulateModuleTypes

type: string default: namespace

Configure how to encapsulate the module types, to avoid confusion.

namespace (default): will wrap all types in a TypeScript namespace, using the module name. prefix: will prefix all types from a specific module with the module name. none: will skip encapsulation, and generate type as-is.

Usage Example

Here's a short example for generating types and resolvers for 2 modules:

schema: './src/modules/**/typedefs/*.graphql'
generates:
./server/src/modules/:
preset: graphql-modules
presetConfig:
baseTypesPath: ../generated-types/graphql.ts # Where to create the complete schema types
filename: generated-types/module-types.ts # Where to create each module types
plugins:
- add:
content: '/* eslint-disable */'
- typescript
- typescript-resolvers

This will generate a file called module-types.ts under each module you have.

To use the generates resolvers, you can use Resolvers signature and apply it to your resolvers object within the module:

// src/modules/user/resolvers.ts
import { MyModule } from './generated-types/module-types';
export const resolvers: MyModule.Resolvers = {
// Here you can implement only the types and fields defined in your module!
};

You can find an example project here.