Registry / devops / gql2ts

gql2ts

JSON →
library1.10.1jsnpmunverified

Converts GraphQL schemas (SDL) to TypeScript type definitions. Stable v1.10.1, low release cadence. Supports custom scalars, enums, and unions. Differentiator: outputs TypeScript interfaces/classes with optional field mapping. Requires graphql as a peer dependency. No browser support; Node.js-only since v1.

npm install gql2ts
INSTALL
IMPORT
SIG · GQL2TS
G
gql2ts
devopsjavascriptv1.10.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

gql2ts
import { gql2ts } from 'gql2ts'
import gql2ts from 'gql2ts'
Default export is not available; named export only.
convert
import { convert } from 'gql2ts'
import { convert } from 'gql2ts/convert'
convert is a named export from the main entry, not a subpath.
Schema
import { Schema } from 'gql2ts'
const { Schema } = require('gql2ts')
CJS require works, but preferred ESM import. TypeScript type: Schema is also exported as a type.
IGQL2TSOptions
import type { IGQL2TSOptions } from 'gql2ts'
import { IGQL2TSOptions } from 'gql2ts'
Use type-only import for interfaces to avoid runtime overhead.

Converts a GraphQL schema to TypeScript type definitions with module and namespace options.

import { gql2ts } from 'gql2ts'; import { buildSchema } from 'graphql'; const schema = ` type Query { user(id: ID!): User } type User { id: ID! name: String } `; const gqlSchema = buildSchema(schema); const types = gql2ts(gqlSchema, { module: 'commonjs', namespace: 'MyTypes' }); console.log(types); // Outputs TypeScript definitions string
Debug
Known issues
breakinggql2ts v2 introduced breaking changes to the API. Input must be a GraphQLSchema object (not SDL string), and options have changed.
fix
Use buildSchema from graphql to convert SDL string to Schema object before passing to gql2ts.
affects: >=2.0.0
deprecatedThe 'module' option is deprecated in v2. Use 'moduleType' instead.
fix
Rename 'module' to 'moduleType' where used.
affects: >=2.0.0 <2.0.0
gotchaCustom scalar types are output as 'any' by default, which may lead to type unsafety.
fix
Provide custom scalar mapping via 'scalars' option in the configuration.
affects: <=1.10.1
gotchaEnum values are output as string literal types, not numeric or other types.
fix
If numeric enums needed, manual type override required.
affects: <=1.10.1
gotchaInterfaces are generated with optional fields for nullable types, but required for non-null. This may conflict with GraphQL required arguments.
fix
Verify generated types match your schema's nullability expectations.
affects: <=1.10.1
Errors
Common errors & fixes
TypeError: gql2ts is not a function
gql2ts is a named export, not the default export.
fix
Use named import: import { gql2ts } from 'gql2ts'
Error: Cannot find module 'graphql'
graphql is a required peer dependency not installed.
fix
Run npm install graphql
Unexpected token: 'export' - require() of ES Module
gql2ts v2+ is ESM-only; older Node versions or CJS require fails.
fix
Use import syntax or upgrade to Node 14+ and use dynamic import().
The schema argument must be a GraphQLSchema instance
Passed an SDL string instead of a built schema.
fix
Use buildSchema from graphql first: const schema = buildSchema(sdlString);
Upgrade
Version history
1.10.1latest on npm
Audit
Dependencies
graphqlrequiredParses GraphQL schemas during conversion
Agent activity
6 hits · last 30 days
node
6
Resources
packagegql2ts
gql2ts — npm install gql2ts · libregistry