Loading GraphQL to JSON Schema Converter...
How to Convert GraphQL to JSON Schema - Step by Step Guide
Input Your GraphQL Schema
Start with your GraphQL schema definition. Whether you're working with Apollo Server, need to validate schemas, or want to generate JSON Schema docs, you have several options:
Example: GraphQL Schema Input
Here's a sample GraphQL type:
type User {
id: ID!
name: String!
age: Int
posts: [Post!]!
}
type Post {
id: ID!
title: String!
}Automatic Conversion
The converter automatically transforms your GraphQL types to JSON Schema:
Example: JSON Schema Output
What the converter produces for the two types in Step 1:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"age": {
"type": [
"integer",
"null"
],
"minimum": -2147483648,
"maximum": 2147483647
},
"posts": {
"type": "array",
"items": {
"$ref": "#/definitions/Post"
}
}
},
"required": [
"id",
"name",
"posts"
]
},
"Post": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"title": {
"type": "string"
}
},
"required": [
"id",
"title"
]
}
}
}Review and Customize
Verify the generated JSON Schema:
Export and Use
Get your JSON Schema ready for use:
Frequently Asked Questions
Why turn a GraphQL schema into JSON Schema?
Plenty of tooling speaks JSON Schema and not GraphQL: request validators such as Ajv, form generators, contract tests, mock data tools and documentation sites. Converting the SDL lets those tools check data against the same types your GraphQL server uses, without writing the shapes twice.
How are GraphQL types mapped?
String and ID become "string", Int becomes "integer" limited to the 32-bit range GraphQL allows, Float becomes "number" and Boolean becomes "boolean". Object, interface and input types become objects whose non-null fields are listed in "required". Lists become arrays, enums keep their exact value names, unions become anyOf over $ref links to their member types, and custom scalars get a definition that accepts any value, because GraphQL does not say what JSON a custom scalar uses. In input types an ID may also be an integer, since GraphQL accepts both there.
Why do nullable fields accept null?
Because a GraphQL server really does send null for them. A field typed age: Int can come back as "age": null, so its schema is ["integer", "null"]. Only fields marked with ! are required and refuse null. Inside lists the same rule applies to the items: [String!] rejects a null item, [String] allows one.
What happens to descriptions, deprecations and default values?
Type and field descriptions, including multi-line block strings, are copied into "description". A @deprecated reason is added to the field description. Default values on input fields become "default", written exactly as typed, so 14.50 stays 14.50 and "00501" stays a string. A non-null input field with a default is not required, because GraphQL fills it in for you. Field arguments are not part of the output, since they describe a request, not the returned object.
What if my schema has an error?
Nothing is generated and you see the line and column of the first problem, with the line itself underneath. That covers syntax mistakes, types used but never declared, duplicate names, a type that does not implement its interface, and default values GraphQL would reject, for example an Int default of 8901240544102066246, which does not fit in 32 bits. If you paste a query or JSON by mistake, the page tells you that too.
Can I use the output with Ajv or OpenAPI?
The output declares draft-07 and compiles in Ajv, strict mode included. Validate against one type with {"$ref": "schema.json#/definitions/TypeName"}. OpenAPI 3.1 accepts this JSON Schema as it is; OpenAPI 3.0 uses an older dialect without ["integer", "null"] type lists, so run the result through a JSON Schema to OpenAPI converter first.
Is my schema uploaded anywhere?
No. The conversion runs in your browser with the graphql-js library, and nothing you paste or upload is sent to a server or kept. Reloading the page starts with an empty editor.
Related pages: check a GraphQL schema for errors, turn GraphQL types into TypeScript, convert JSON Schema to OpenAPI and build a JSON Schema from sample JSON.
Related Tools
GraphQL Query Formatter
Format and beautify GraphQL queries and schemas with syntax highlighting
GraphQL Schema Validator
Validate GraphQL schemas with syntax and structure checking
GraphQL Fixer
Fix broken GraphQL queries and schemas automatically, repair syntax errors and malformed types
GraphQL to TypeScript
Generate TypeScript types from GraphQL schemas and queries
GraphQL Minifier
Compress and optimize GraphQL queries for production
GraphQL to JSON
Convert GraphQL schemas and queries to JSON format