Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

get-graphql-from-jsonschema

Package Overview
Dependencies
Maintainers
3
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-graphql-from-jsonschema

get-graphql-from-jsonschema gets a GraphQL schema from a JSON schema.

  • 7.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
increased by81.8%
Maintainers
3
Weekly downloads
 
Created
Source

get-graphql-from-jsonschema

get-graphql-from-jsonschema gets a GraphQL schema from a JSON schema.

Status

CategoryStatus
Versionnpm
DependenciesDavid
Dev dependenciesDavid
BuildGitHub Actions
LicenseGitHub

Installation

$ npm install get-graphql-from-jsonschema

Quick Start

First you need to add a reference to get-graphql-from-jsonschema to your application:

const { getGraphqlFromJsonSchema } = require('get-graphql-from-jsonschema');

If you use TypeScript, use the following code instead:

import { getGraphqlFromJsonSchema } from 'get-graphql-from-jsonschema';

To get a GraphQL schema from a JSON schema, call the getGraphqlFromJsonSchema function and hand over the root name of the schema you want to convert as well as the schema itself. As a result, you get back the root GraphQL type name and, if needed, additional GraphQL type definitions:

const { typeName, typeDefinitions } = getGraphqlFromJsonSchema({
  rootName: 'person',
  schema: {
    type: 'object',
    properties: {
      firstName: { type: 'string' },
      lastName: { type: 'string' },
      coordinates: {
        type: 'object',
        properties: {
          latitude: { type: 'number' },
          longitude: { type: 'number' }
        },
        required: [ 'latitude', 'longitude' ],
        additionalProperties: false
      },
      tags: {
        type: 'array',
        items: {
          type: 'object',
          properties: {
            key: { type: 'string' },
            value: { type: 'string' }
          },
          required: [ 'key', 'value' ],
          additionalProperties: false
        }
      }
    },
    required: [ 'firstName', 'tags' ],
    additionalProperties: false
  }
});

console.log(typeName);
// => PersonT0

console.log(typeDefinitions);
// => [
//      'type PersonT0CoordinatesT0 {
//        latitude: Float!
//        longitude: Float!
//      }',
//      'type PersonT0TagsT0T0 {
//        key: String!
//        value: String!
//      }',
//      'type PersonT0 {
//        firstName: String!
//        lastName: String
//        coordinates: PersonT0CoordinatesT0
//        tags: [PersonT0TagsT0T0]!
//      }'
//    ]

The T0 suffixes are due to enumerating the types in each schema. If a schema has multiple types, they are noted with increasing indexes, to differentiate them in resulting union types. This also happens with anyOf constructs.

If you want to use the generated types as input types for a mutation, additionally provide the direction option to the call to getGraphqlFromJsonSchema and set its value to input:

const { typeName, typeDefinitions } = getGraphqlFromJsonSchema({
  rootName: 'person',
  schema: {
    // ...
  },
  direction: 'input'
});

Using oneOf to generate union types

The oneOf keyword is supported with a limitation on its use: There must be no other properties on the same level as the oneOf.

const { typeName, typeDefinitions } = getGraphqlFromJsonSchema({
  rootName: 'foobar',
  schema: {
    oneOf: [
      {
        type: 'number'
      },
      {
        type: 'object',
        properties: {
          foo: { type: 'string' },
          bar: { type: 'number' }
        },
        required: [ 'foo' ],
        additionalProperties: false
      }
    ]
  }
});

console.log(typeName);
// => Foobar

console.log(typeDefinitions);
// => [
//     'type FoobarI1T0 {
//       foo: String!
//       bar: Float
//     }',
//     'union Foobar = Float | FoobarI1T0'
// ]

Knowing the limitations

Unfortunately, it is not possible to map every aspect of a JSON schema to a GraphQL schema. When using getGraphqlFromJsonSchema, the following limitations apply:

  • The null type gets ignored, since it can not be mapped to GraphQL directly.
  • The keywords allOf and anyOf get ignored, since their logic can not be mapped to GraphQL. However, the oneOf keyword is supported.

Running quality assurance

To run quality assurance for this module use roboter:

$ npx roboter

Keywords

FAQs

Package last updated on 26 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc