Registry / development / graphql-to-json-schema

graphql-to-json-schema

JSON →
library1.0.0jsnpmunverified

Converts GraphQL Schema Definition Language (SDL) to JSON Schema (draft-04). Version 1.0.0. Supports scalars (including custom), enums, unions, object types with required fields, lists, and recursive references. Output is compatible with react-jsonschema-form. No active releases since initial 2016. Differentiators: simple string-to-object conversion without GraphQL execution, ESM-only via npm package. Caveats: no support for interfaces, input types, directives, or deprecation. Output uses non-standard types like GRAPHQL_SCALAR for custom scalars and GRAPHQL_UNION.

npm install graphql-to-json-schema
INSTALL
IMPORT
SIG · GRAPHQL-TO-JSON-SC
G
graphql-to-json-schema
developmentjavascriptv1.0.0
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.

default
import transform from 'graphql-to-json-schema'
const transform = require('graphql-to-json-schema')
ESM-only package; CJS require will fail unless using bundler that interops. Default export is function.
transform
import transform from 'graphql-to-json-schema'
import { transform } from 'graphql-to-json-schema'
Only a default export is provided. Named import results in undefined.
TypeScript types
import transform from 'graphql-to-json-schema'
import type { Transform } from 'graphql-to-json-schema'
No type definitions shipped. Use @types/graphql-to-json-schema if available, or declare module.

Demonstrates converting a simple GraphQL SDL string to a JSON Schema object.

import transform from 'graphql-to-json-schema'; const sdl = ` type Query { hello: String! } `; const jsonSchema = transform(sdl); console.log(JSON.stringify(jsonSchema, null, 2)); // Output: // { // "$schema": "http://json-schema.org/draft-04/schema#", // "definitions": { // "Query": { // "title": "Query", // "type": "object", // "properties": { // "hello": { // "type": "string", // "required": true, // "title": "hello", // "arguments": [] // } // }, // "required": ["hello"] // } // } // }
Debug
Known issues
breakingESM-only package; require() will throw ERR_REQUIRE_ESM
fix
Use dynamic import() or convert project to ESM.
affects: >=1.0.0
gotchaOutput JSON uses non-standard types like GRAPHQL_SCALAR and GRAPHQL_UNION, not valid JSON Schema types
fix
Post-process output to replace GRAPHQL_SCALAR with 'string' or skip custom scalars.
affects: >=0.0.0
gotchaDoes not support input types, interfaces, or directives; those parts of SDL are ignored
fix
Pre-process SDL to remove unsupported elements or use another library like graphql-2-json-schema.
affects: >=0.0.0
gotchaAll fields without required marker are still listed with required: false; only explicitly non-null fields become required in output
fix
Check if GraphQL schema marks fields correctly; nullable fields may appear as required if not handled.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: transform is not a function
Using named import instead of default import.
fix
Use `import transform from 'graphql-to-json-schema'` (no curly braces).
ERR_REQUIRE_ESM
Using CommonJS require() with an ESM-only package.
fix
Switch to ES modules or use dynamic import: `const transform = (await import('graphql-to-json-schema')).default;`
Cannot find module 'graphql'
Missing peer dependency graphql.
fix
Run `npm install graphql` as a direct dependency.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
graphqloptionalPeer dependency for parsing GraphQL SDL; required at runtime.
Agent activity
41 hits · last 30 days
node
36
Resources
graphql-to-json-schema — npm install graphql-to-json-schema · libregistry