Code generation from schema helps you create ready-to-use code automatically from your database structure. This saves time and reduces mistakes.
Code generation from schema in GraphQL
Introduction
Syntax
GraphQL
graphql-codegen --config codegen.yml
You usually run a command line tool with a config file to generate code.
The config file tells the tool what schema to use and what code to create.
Examples
codegen.yml.GraphQL
graphql-codegen --config codegen.yml
GraphQL
schema: ./schema.graphql
documents: ./src/**/*.graphql
generates:
./src/generated/graphql.ts:
plugins:
- typescript
- typescript-operations
- typescript-react-apolloSample Program
This example shows how to generate TypeScript code from a simple GraphQL schema with one query.
GraphQL
# 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
Important Notes
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.
Summary
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.
Practice
1. What is the main purpose of code generation from a GraphQL schema?
easy
Solution
Step 1: Understand code generation concept
Code generation means creating code automatically from a source, here the GraphQL schema.Step 2: Identify the purpose in GraphQL context
In GraphQL, code generation helps create types and queries automatically from the schema to save time.Final Answer:
To automatically create code based on the GraphQL schema -> Option CQuick Check:
Code generation = automatic code creation [OK]
Hint: Code generation means automatic code creation from schema [OK]
Common Mistakes:
- Thinking code generation means manual coding
- Confusing code generation with deleting tables
- Assuming it converts queries to HTML
2. Which of the following is the correct way to specify the schema file in a GraphQL code generator config?
easy
Solution
Step 1: Recall config syntax for GraphQL code generator
The config uses key-value pairs with colon and string paths in quotes.Step 2: Identify correct syntax
"schema: './schema.graphql'" uses colon and quotes correctly: "schema: './schema.graphql'".Final Answer:
"schema: './schema.graphql'" -> Option DQuick Check:
Config uses colon and quotes for paths [OK]
Hint: Config uses colon and quotes for file paths [OK]
Common Mistakes:
- Using equals sign instead of colon
- Omitting quotes around file path
- Using arrow syntax which is invalid here
3. Given this config snippet:
What will be generated after running the code generator?
{
schema: './schema.graphql',
generates: {
'./src/types.ts': { plugins: ['typescript'] }
}
}What will be generated after running the code generator?
medium
Solution
Step 1: Analyze the config's generates section
The config says to generate './src/types.ts' using the 'typescript' plugin.Step 2: Understand what the 'typescript' plugin does
This plugin creates TypeScript types based on the GraphQL schema.Final Answer:
A TypeScript file with types matching the GraphQL schema -> Option BQuick Check:
typescript plugin = TypeScript types file [OK]
Hint: Plugin name hints the generated file type [OK]
Common Mistakes:
- Confusing TypeScript with JavaScript output
- Expecting HTML or JSON instead of types
- Ignoring the plugin specified
4. You run the GraphQL code generator but get an error: "Cannot find schema file './schema.graphql'". What is the most likely cause?
medium
Solution
Step 1: Understand the error message
The error says it cannot find the schema file at the given path.Step 2: Identify the cause
This usually means the path is wrong or the file does not exist at that location.Final Answer:
The schema file path in the config is incorrect or file is missing -> Option AQuick Check:
File not found = wrong path or missing file [OK]
Hint: Check schema file path and existence first [OK]
Common Mistakes:
- Blaming plugins or output path
- Assuming code generator lacks schema support
- Ignoring file system errors
5. You want to generate both TypeScript types and React hooks from your GraphQL schema. Which config snippet correctly sets this up?
hard
Solution
Step 1: Identify plugins for types and hooks
'typescript' plugin generates TypeScript types; 'typescript-react-apollo' generates React Apollo hooks.Step 2: Match plugins to correct output files
Types go to './src/types.ts' with 'typescript'; hooks go to './src/hooks.ts' with 'typescript-react-apollo'.Final Answer:
Config with 'typescript' for types and 'typescript-react-apollo' for hooks -> Option AQuick Check:
Correct plugins match output files [OK]
Hint: Match plugins to output files by their purpose [OK]
Common Mistakes:
- Swapping plugins between files
- Using non-existent plugins like 'react-hooks'
- Missing one of the required plugins
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
