Introduction
Code generation from schema helps you create ready-to-use code automatically from your database structure. This saves time and reduces mistakes.
Jump into concepts and practice - no test required
Code generation from schema helps you create ready-to-use code automatically from your database structure. This saves time and reduces mistakes.
graphql-codegen --config codegen.yml
codegen.yml.graphql-codegen --config codegen.yml
schema: ./schema.graphql
documents: ./src/**/*.graphql
generates:
./src/generated/graphql.ts:
plugins:
- typescript
- typescript-operations
- typescript-react-apolloThis example shows how to generate TypeScript code from a simple GraphQL schema with one query.
# 1. Create a schema file named schema.graphql # # type Query { # hello: String # } # 2. Create codegen.yml file: # schema: ./schema.graphql # generates: # ./generated/graphql.ts: # plugins: # - typescript # 3. Run command: graphql-codegen --config codegen.yml
Make sure your schema file is correct before generating code.
Generated code can be customized by changing the config file plugins.
Run code generation again after schema changes to keep code up to date.
Code generation saves time by creating code from your database schema automatically.
Use a config file to tell the tool what to generate and from which schema.
Run the generator whenever your schema changes to keep code synced.
{
schema: './schema.graphql',
generates: {
'./src/types.ts': { plugins: ['typescript'] }
}
}