buro26-graphql-codegen-sdk
v0.1.4
Published
Generate a graphql client sdk for any graphql api
Downloads
131
Maintainers
Readme
GraphQL Codegen SDK
Generate a SDK for your GraphQL API using GraphQL Codegen.
Getting started
npm i buro26-graphql-codegen-sdkUsage
Create a codegen.ts file in the root of your project with the following content:
Add a script to your package.json to run the codegen:
{
"scripts": {
"codegen": "graphql-codegen"
}
}Run the codegen script:
npm run codegenConfiguration
Create a codegen.ts file in the root of your project with the following content:
import {createConfig} from 'buro26-graphql-codegen-sdk'
export default createConfig({
schema: 'http://localhost:1337/graphql',
documents: 'src/lib/my-api/queries/**/!(*.generated).{ts,tsx}',
typesImportPath: '@/lib/my-api/generated/types',
schemaImportPath: '@/lib/my-api/generated/schema',
enumStyle: 'const', // 'const' (default) | 'enum' | 'union'
types: {
path: 'src/lib/my-api/generated/types.ts'
},
sdl: {
path: './schema.graphql'
},
client: {
path: 'src/lib/my-api/generated/client-factory.ts',
config: {
logger: true
}
},
zod: {
path: 'src/lib/my-api/generated/schema.ts',
config: {
exportFormDataSchema: true,
zodVersion: 4, // Optional: set to 4 for Zod v4 compatibility (default: 3)
},
}
})Usage
Add a script to your package.json to run the codegen:
{
"scripts": {
"codegen": "graphql-codegen"
}
}Run the codegen script:
npm run codegenClient
On more how to use the generated client, see the buro26-graphql-codegen-client package.
Enum Style
Control how GraphQL enums are generated using the enumStyle option:
| Style | Generated TypeScript | Runtime values | Union type |
|-------|---------------------|----------------|------------|
| 'const' (default) | const Status = { Active: 'ACTIVE' } as const + type Status = typeof Status[keyof typeof Status] | Status.Active | 'ACTIVE' \| 'INACTIVE' |
| 'enum' | enum Status { Active = 'ACTIVE' } | Status.Active | No |
| 'union' | type Status = 'ACTIVE' \| 'INACTIVE' | No | 'ACTIVE' \| 'INACTIVE' |
The enumStyle setting is automatically cascaded to both the TypeScript types plugin and the Zod schema plugin.
Zod Schema
On more how to use the generated zod schema, see the buro26-graphql-codegen-zod package.
Zod Version
The SDK supports both Zod v3 and Zod v4. Set zodVersion in the zod config to match your installed version:
zod: {
path: 'src/lib/my-api/generated/schema.ts',
config: {
zodVersion: 4, // For Zod v4 (default: 3)
},
}When using exportFormDataSchema, ensure you have a compatible version of zod-form-data installed (^2.0.0 for Zod v3, ^3.0.0 for Zod v4).
Test and Deploy
Running tests
To run tests, run the following command:
bun testContributing
Wish to contribute to this project? Pull the project from the repository and create a merge request.
Authors and acknowledgment
Buro26 - https://buro26.digital
Special thanks to all contributors and the open-source community for their support and contributions.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project status
The project is currently in active development. We are continuously working on adding new features and improving the existing ones. Check the issues section for the latest updates and planned features.
Feel free to reach out if you have any questions or suggestions!
