GraphQL Modules Preset
Package name | Weekly Downloads | Version | License | Updated |
---|---|---|---|---|
@graphql-codegen/graphql-modules-preset | Nov 22nd, 2024 |
Installation
npm i -D @graphql-codegen/graphql-modules-preset
Config API Reference
baseTypesPath
type: string
Required, should point to the base schema types file. The key of the output is used a base path for this file.
importBaseTypesFrom
type: string
Overrides the package import for the base types. Use this if you are within a monorepo, and you wish to import the base types directly from a different package, and not from a relative path.
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 execution 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.
requireRootResolvers
type: boolean
default: false
Generate resolvers of root types (Query, Mutation and Subscription) as non-optional.
useGraphQLModules
type: boolean
default: true
By default, the generated types will generate some code specific to graphql-modules
library.
If you are not using GraphQL-Modules, you can disable this feature by setting this to false
.
The @graphql-codegen/graphql-modules-preset
generates .ts
file with TypeScript types, per each directory that contains GraphQL SDL definitions.
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
/ v2
(previous versions are not supported) by default.
If you are not using graphql-modules
, you can set useGraphQLModules: false
to disable this behaviour.
Usage Example
Given a folder structure with the following files:
- src/
- modules/
- user/
- resolvers.ts
- typedefs/
- user.graphql
- product/
- resolvers.ts
- typedefs/
- product.graphql
Here’s a short example for generating types and resolvers for 2 modules:
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
schema: './src/modules/**/typedefs/*.graphql',
generates: {
'./src/modules/': {
preset: 'graphql-modules',
presetConfig: {
baseTypesPath: '../generated-types/graphql.ts',
filename: 'generated-types/module-types.ts'
},
plugins: [
{
add: {
content: '/* eslint-disable */'
}
},
'typescript',
'typescript-resolvers'
]
}
}
}
export default config
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:
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.
Using without GraphQL-Modules
By default, this preset it generating code for graphql-modules
, but if you are not using it, you can set useGraphQLModules: false
in your preset configuration to generate fully agnostic types that are based on folder structure only.