Registry / web-framework / graphql-schema-typescript

graphql-schema-typescript

JSON →
library1.6.1jsnpmunverified

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-typescript
INSTALL
IMPORT
SIG · GRAPHQL-SCHEMA-TYP
G
graphql-schema-typescript
web-frameworkjavascriptv1.6.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

generateTypeScriptTypes
import { generateTypeScriptTypes } from 'graphql-schema-typescript';
const { generateTypeScriptTypes } = require('graphql-schema-typescript');
Primary function for programmatic type generation. The library is primarily consumed as an ESM module in modern Node.js/TypeScript environments.
GenerateTypescriptOptions
import type { GenerateTypescriptOptions } from 'graphql-schema-typescript';
This type defines the configuration options for the `generateTypeScriptTypes` function. Always import types using `import type` for clarity and bundler optimization.

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.

import { generateTypeScriptTypes } from 'graphql-schema-typescript'; import { buildSchema } from 'graphql'; import * as fs from 'fs/promises'; async function generateTypes() { const schemaString = ` type Query { hello(name: String!): String! users: [User!]! } type User { id: ID! name: String! email: String } schema { query: Query } `; const schema = buildSchema(schemaString); const outputPath = './generated-types.ts'; try { await generateTypeScriptTypes(schema, outputPath, { debug: true, // Enable debug logging contextType: 'MyContextType', rootValueType: 'MyRootValueType', smartTParent: true, // Infer TParent type for resolvers smartTResult: true, // Infer TResult type for resolvers enumValueSuffix: 'Enum' // Suffix for generated enum types }); console.log(`Successfully generated TypeScript types to ${outputPath}`); const generatedContent = await fs.readFile(outputPath, 'utf-8'); console.log('Generated content preview:\n', generatedContent.substring(0, 500), '...'); } catch (err) { console.error('Error generating types:', err); process.exit(1); } } generateTypes();
graphql-schema-typescript --version
Debug
Known issues
breakingThe `graphql` peer dependency has specific version requirements for each major/minor release of `graphql-schema-typescript`. For example, v1.6.x requires `^16.0.0` of `graphql`, while v1.5.x required `^14.0.0` or `^15.0.0`. Using an incompatible `graphql` version will lead to runtime errors or incorrect type generation.
fix
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`).
affects: >=1.4.0
breakingIn version 1.3.1, enum types under the global scope are now generated as TypeScript `enum` instead of `string union` types. This is a breaking change that might affect existing code relying on string union behavior.
fix
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.
affects: >=1.3.1
gotchaThe default `TParent` and `TResult` types for generated resolvers can be implicitly `any` or inferred based on schema, but v1.2.2 introduced `smartTParent` and `smartTResult` options. If these are set to `true`, `TParent` might become `undefined` (if `rootValueType` is used) or `TResult` will be strongly inferred, which could alter resolver signatures.
fix
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.
affects: >=1.2.2
gotchaFrom v1.2.10 onwards, generated files include `/* eslint-disable */` comments. While this prevents linting errors in generated code, it might override project-specific linting configurations if you wish to apply custom rules to generated files or integrate them more tightly into your linting pipeline.
fix
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.
affects: >=1.2.10
Errors
Common errors & fixes
Error: Cannot find module 'graphql' or its corresponding type declarations.
The `graphql` peer dependency is either not installed, or its version is incompatible with `graphql-schema-typescript`.
fix
Install the correct `graphql` version as specified in `graphql-schema-typescript`'s `package.json` or README. For example, `npm install graphql@^16.0.0`.
Error: Schema must contain unique named types but contains multiple types named "YourType"
Your GraphQL schema definition has duplicate type names, which is not allowed by the GraphQL specification.
fix
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.
TypeError: Cannot read properties of undefined (reading 'kind') at buildASTSchema
The input provided to `buildSchema` (when using the programmatic API) or the schema files provided to the CLI are not valid GraphQL SDL strings or do not represent a parseable schema.
fix
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`).
TS2307: Cannot find module './generated-types' or its corresponding type declarations.
The generated TypeScript file (`generated-types.ts` in this example) is not being correctly picked up by your TypeScript compiler or IDE. This can be due to an incorrect `outputPath`, missing `include` in `tsconfig.json`, or the file not existing.
fix
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.
Upgrade
Version history
1.6.1latest on npm
Audit
Dependencies
graphqlrequiredRequired for GraphQL schema parsing and manipulation. Specific versions are mandated per graphql-schema-typescript major release.
typescriptrequiredThe library generates TypeScript code and relies on TypeScript for its own development and type definitions.
Agent activity
44 hits · last 30 days
node
36
OpenAI (training)
1
Resources