# graphql-to-json-schema

> Converts GraphQL Schema Definition to JSONSchema

Latest version **1.0.0** (published 2017-12-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install graphql-to-json-schema
pnpm add graphql-to-json-schema
yarn add graphql-to-json-schema
bun add graphql-to-json-schema
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-12-24 |
| First published | 2017-12-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 30 |
| Author | Luc Alapini |
| Maintainers | alapini |
| Keywords | graphql, json-schema |

## Links

- npm: https://www.npmjs.com/package/graphql-to-json-schema
- Repository: https://github.com/alapini/graphql-to-jsonschema
- Homepage: https://github.com/alapini/graphql-to-jsonschema#readme
- Issues: https://github.com/alapini/graphql-to-jsonschema/issues
- npm.io page: https://npm.io/package/graphql-to-json-schema

## Dependencies (1)

- [graphql](https://npm.io/package/graphql.md) ^0.10.1

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2017-12-24
- 0.1.0 — 2017-12-24

## README

# graphql-to-jsonschema
[![](https://travis-ci.org/alapini/graphql-to-jsonschema.svg?branch=master)](https://travis-ci.org/alapini/graphql-to-jsonschema)

Converts GraphQL Schema Definition to JSONSchema  
**(Compatible with [mozilla-services/react-jsonschema-form](https://github.com/mozilla-services/react-jsonschema-form))**

## Installation

```shell
npm install graphql-to-json-schema
```

## Usage

```js
  const transform = require('graphql-to-json-schema');

  const schema = transform(`
    scalar Foo

    union MyUnion = Foo | String | Float

    enum MyEnum {
      FIRST_ITEM
      SECOND_ITEM
      THIRD_ITEM
    }

    type Stuff {
      my_field: Int
      req_field: String!
      recursion: MoreStuff
      custom_scalar: Foo
      enum: MyEnum
    }

    type MoreStuff {
      first: [Float]
      identifier: [ID]!
      reference: Stuff!
      bool: Boolean!
      union: MyUnion
      with_params(param1: Int, param2: [Float]): Int
    }
  `);

  console.log(schema);
```

The code above returns the following JSON as a plain JS object:

```json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "Foo": {
      "title": "Foo",
      "type": "GRAPHQL_SCALAR"
    },
    "MyUnion": {
      "title": "MyUnion",
      "type": "GRAPHQL_UNION",
      "oneOf": [
        {
          "$ref": "#/definitions/Foo"
        },
        {
          "type": "string",
          "required": false
        },
        {
          "type": "number",
          "required": false
        }
      ]
    },
    "MyEnum": {
      "title": "MyEnum",
      "type": "GRAPHQL_ENUM",
      "enum": [
        "FIRST_ITEM",
        "SECOND_ITEM",
        "THIRD_ITEM"
      ]
    },
    "Stuff": {
      "title": "Stuff",
      "type": "object",
      "properties": {
        "my_field": {
          "type": "integer",
          "required": false,
          "title": "my_field",
          "arguments": []
        },
        "req_field": {
          "type": "string",
          "required": true,
          "title": "req_field",
          "arguments": []
        },
        "recursion": {
          "$ref": "#/definitions/MoreStuff",
          "title": "recursion",
          "arguments": []
        },
        "custom_scalar": {
          "$ref": "#/definitions/Foo",
          "title": "custom_scalar",
          "arguments": []
        },
        "enum": {
          "$ref": "#/definitions/MyEnum",
          "title": "enum",
          "arguments": []
        }
      },
      "required": [
        "req_field"
      ]
    },
    "MoreStuff": {
      "title": "MoreStuff",
      "type": "object",
      "properties": {
        "first": {
          "type": "array",
          "items": {
            "type": {
              "type": "number",
              "required": false
            }
          },
          "title": "first",
          "arguments": []
        },
        "identifier": {
          "type": "array",
          "items": {
            "type": {
              "type": "string",
              "required": false
            }
          },
          "required": true,
          "title": "identifier",
          "arguments": []
        },
        "reference": {
          "$ref": "#/definitions/Stuff",
          "required": true,
          "title": "reference",
          "arguments": []
        },
        "bool": {
          "type": "boolean",
          "required": true,
          "title": "bool",
          "arguments": []
        },
        "union": {
          "$ref": "#/definitions/MyUnion",
          "title": "union",
          "arguments": []
        },
        "with_params": {
          "type": "integer",
          "required": false,
          "title": "with_params",
          "arguments": [
            {
              "title": "param1",
              "type": {
                "type": "integer",
                "required": false
              },
              "defaultValue": null
            },
            {
              "title": "param2",
              "type": {
                "type": "array",
                "items": {
                  "type": {
                    "type": "number",
                    "required": false
                  }
                }
              },
              "defaultValue": null
            }
          ]
        }
      },
      "required": [
        "identifier",
        "reference",
        "bool"
      ]
    }
  }
}
```

---
_Source: https://npm.io/package/graphql-to-json-schema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
