graphql-schema-typescript is a utility library for generating TypeScript type definitions directly from GraphQL schema definitions. Unlike client-side code generators such as Apollo-codegen, which focus on types for client queries, this library's primary purpose is to produce type-safe interfaces for GraphQL server-side development, specifically for writing resolvers. The current stable version is 1.6.1. While there isn't a strict release cadence, the project is actively maintained with recent updates to support newer GraphQL versions. Key differentiators include a 1-to-1 mapping from GraphQL types to TypeScript interfaces, conversion of GraphQL descriptions to JSDoc comments, and specialized types for resolver arguments, parent objects, and return values (e.g., `GQLResolver`, `RootQueryToUsersArgs`, `RootQueryToUsersResolver`). It offers both a programmatic API and a command-line interface (CLI) for integration into build workflows, supporting `.gql` and `.graphqls` schema file extensions.
npm install graphql-schema-typescriptVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically generate TypeScript types from a GraphQL schema string, including common configuration options like `smartTParent` and `contextType`. It uses `buildSchema` from `graphql` to create a schema object and writes the output to a file.
Always check the `Compatibility` table in the package's README or `package.json` to ensure your installed `graphql` version matches the `graphql-schema-typescript` peer dependency. Upgrade or downgrade `graphql` as needed (e.g., `npm install graphql@^16.0.0`).
Update your code to expect generated enum types where appropriate. If string unions are preferred, consider custom type mapping options or adjusting your schema. For `d.ts` generation, `export const enum` is used.
Explicitly set `smartTParent: false` and `smartTResult: false` in `GenerateTypescriptOptions` if you prefer the legacy `any` defaults or require manual type declaration for resolver parent/result types. Otherwise, adjust resolver implementations to match the inferred types.
If you need to apply linting rules to generated files, you can use a post-processing script to remove these comments or configure your linter to ignore specific patterns. Alternatively, rely on the `// eslint-disable` comments and focus linting efforts on handwritten code.
Install the correct `graphql` version as specified in `graphql-schema-typescript`'s `package.json` or README. For example, `npm install graphql@^16.0.0`.
Review your `.gql` or `.graphqls` schema files (or the schema object passed programmatically) and ensure all type, input, enum, and interface names are unique across the entire schema. Check for accidental re-declarations or issues with schema merging if using multiple files.
Ensure that the `schema` argument passed to `generateTypeScriptTypes` is a valid `GraphQLSchema` object, typically created with `buildSchema` from a correct SDL string. If using the CLI, verify that the `--schema` argument points to valid GraphQL schema files (`.gql`, `.graphqls`).
Verify that the `outputPath` in your `generateTypeScriptTypes` call (or CLI output path) is correct and accessible. Ensure your `tsconfig.json`'s `include` array covers the directory where the types are generated (e.g., `"include": ["./src", "./generated-types.ts"]`). Run the generation script to confirm the file is actually created.