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

@backref/graphql-to-json-schema

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@backref/graphql-to-json-schema

Transform a GraphQL Schema introspection file to a valid JSON Schema.

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

graphql-2-json-schema

Transform a GraphQL Schema introspection file to a valid JSON Schema.

This package adds support for decorators improving the utility of GraphQL schema as an IDL for everything.

Decorators

Decorators, aka annotations or attributes in other languages, allows metadata to be attached to GraphQL schema entities. The decorator metadata appear as properties in the resulting JSON schema. Decorator metadata can be used for validations, ACL roles, code generation, form generation ...

The syntax is simple

# decorator with map value
+go_tag({"json": "id", "db": "id"})

# truthy decorator, empty parens default to true
+read_only()
+read_only(false)

# array
+acl_roles(["session", "admin"])

# string
+go_ident("ID")

# number (int, float, etc)
+form_pos(1)

Decorator rules

  • MUST be within a quotes "" description or docstring
  • MUST be on its own line
  • MUST be preceeded by a + symbol to disambiguate against @ directives
  • MUST have valid JSON value within parentheses. An empty parentheses () is converted to boolean value of true.

Decorators example in GraphQL schema

type Todo {
  """
  The primary key.

  +read_only()
  +go_ident("ID")
  +go_tag({"db": "id", "json": "id,omitempty"})
  """
  id: String!
  name: String!
  completed: Boolean
}

The result JSON schema is enriched with __decorators property

Todo: {
    type: 'object',
    properties: {
        id: {
            __decorators: {
                go_ident: "ID",
                go_tag: {db:"id", json: "id,omitempty"},
                read_only: true
            },
            description: 'The primary key',
            type: 'string'
        },
        name: { type: 'string' },
        completed: { type: 'boolean' },
    },
    required: ['id', 'name']
}

Usage

To use the gql2js CLI utility, first install the package

yarn global add @backref/graphql-to-json-schema

# OR
npm install -g @backref/graphql-to-json-schema

Running the utility

# output to STDOUT
gql2js example/example.graphql

# output to file
gql2js example/example.graphql -o example.json

# output to directory (use single quotes)
gql2js 'example/**/*.graphql' -d _temp

Programmatic

yarn add @backref/graphql-to-json-schema
// node example
const g2j = require('@backref/graphql-to-json-schema');

const text = `
type Todo {
  """
  The primary key.

  +read_only()
  +go_ident("ID")
  +go_tag({"db": "id", "json": "id,omitempty"})
  """
  id: String!
  name: String!
  completed: Boolean
}
`;

const jsonSchema = g2j.parseGraphQL(text);
console.log(JSON.stringify(jsonSchema, null, 2));

License

This package is MIT licensed

Original work by

  • wittydeveloper

Enhancements by

FAQs

Package last updated on 19 Jul 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