GraphTypeGen is a command-line tool that generates a fully-typed TypeScript library from your GraphQL schema files. It streamlines the process of creating type-safe query and mutation functions, making your codebase more maintainable, readable, and less error-prone.
- 🔒 Type-safe: Automatically generates TypeScript interfaces from your GraphQL types.
- ⚡ Fast & Efficient: Quickly produces ready-to-use TypeScript functions for queries and mutations.
- 🧩 Customizable: Add custom headers and specify client configurations (e.g., Apollo Client).
- 🚀 Error Handling: Optional built-in error handling for safer API calls.
- 📝 Clean Output: Organized, readable, and production-ready TypeScript code.
- Provide a GraphQL Schema: Pass your
.graphql
or.gql
schema file to the tool. - Configure Output: Specify the output path, client name (e.g.,
apolloClient
), and optional error handling preferences. - Generate Code: The tool creates TypeScript interfaces for your GraphQL types and functions for your queries and mutations.
graph-type-gen \
-schema="./schema.graphql" \
-output="./generated/graphql.ts" \
-header="import { gql } from '@apollo/client';" \
-client="apolloClient" \
-error="true"
- TypeScript interfaces matching your GraphQL types.
- Query and Mutation functions using your provided client.
- Automatic error handling (if
-error
flag is provided).
export interface User {
id: string;
name: string;
email: string;
}
async function getUser(id: string): Promise<[User | null, any | null]> {
try {
const response = await apolloClient.query({
query: gql(`
query getUser($id: String) {
getUser(id: $id) {
id
name
email
}
}
`),
variables: { id },
});
return [response.data.getUser, null];
} catch (error) {
return [null, error];
}
}
Flag | Description | Required |
---|---|---|
-schema |
Path to the GraphQL schema file | ✅ |
-output |
Path to the generated TypeScript file | ✅ |
-header |
Header code to insert (e.g., imports) | ✅ |
-client |
Name of the GraphQL client (e.g., apolloClient ) |
✅ |
-error |
Return errors in function calls (optional) | ❌ |
-
Clone the repository:
git clone https://github.com/MichaelHoelzl/GraphTypeGen.git cd GraphTypeGen
-
Build and run the tool:
go build -o graph-type-gen ./graph-type-gen -schema="./schema.graphql" -output="./generated.ts" -header="import { gql } from '@apollo/client';" -client="apolloClient"
This project is licensed under the MIT License – feel free to use and contribute!