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

graphql-2-json-schema

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-2-json-schema - npm Package Compare versions

Comparing version 0.4.0 to 0.5.0

dist/doc-exampleGenerator.d.ts

2

CHANGELOG.md

@@ -5,2 +5,4 @@ # Changelog

## [0.5.0](https://github.com/wittydeveloper/graphql-to-json-schema/compare/v0.4.0...v0.5.0) (2021-02-01)
## [0.4.0](https://github.com/wittydeveloper/graphql-to-json-schema/compare/v0.3.1...v0.4.0) (2021-01-24)

@@ -7,0 +9,0 @@

3

dist/lib/reducer.d.ts

@@ -10,6 +10,5 @@ import { IntrospectionField, IntrospectionInputValue, IntrospectionType } from 'graphql';

export declare type IntrospectionFieldReducerItem = IntrospectionField | IntrospectionInputValue;
export declare const propertiesIntrospectionFieldReducer: MemoListIterator<IntrospectionFieldReducerItem, JSONSchema6Acc, ReadonlyArray<IntrospectionFieldReducerItem>>;
export declare const definitionsIntrospectionFieldReducer: MemoListIterator<IntrospectionFieldReducerItem, JSONSchema6Acc, ReadonlyArray<IntrospectionFieldReducerItem>>;
export declare const introspectionFieldReducer: MemoListIterator<IntrospectionFieldReducerItem, JSONSchema6Acc, ReadonlyArray<IntrospectionFieldReducerItem>>;
export declare const resolveDefaultValue: (curr: any) => any;
export declare const introspectionTypeReducer: (type: 'definitions' | 'properties') => MemoListIterator<IntrospectionType, JSONSchema6Acc, IntrospectionType[]>;
export {};
"use strict";
exports.__esModule = true;
exports.introspectionTypeReducer = exports.resolveDefaultValue = exports.definitionsIntrospectionFieldReducer = exports.propertiesIntrospectionFieldReducer = exports.getRequiredFields = void 0;
exports.introspectionTypeReducer = exports.resolveDefaultValue = exports.introspectionFieldReducer = exports.getRequiredFields = void 0;
var lodash_1 = require("lodash");

@@ -16,3 +16,3 @@ var typeGuards_1 = require("./typeGuards");

exports.getRequiredFields = getRequiredFields;
var propertiesIntrospectionFieldReducer = function (acc, curr) {
var introspectionFieldReducer = function (acc, curr) {
if (typeGuards_1.isIntrospectionField(curr)) {

@@ -28,3 +28,3 @@ var returnType = typeGuards_1.isNonNullIntrospectionType(curr.type)

type: 'object',
properties: lodash_1.reduce(curr.args, exports.propertiesIntrospectionFieldReducer, {}),
properties: lodash_1.reduce(curr.args, exports.introspectionFieldReducer, {}),
required: exports.getRequiredFields(curr.args)

@@ -48,23 +48,3 @@ }

};
exports.propertiesIntrospectionFieldReducer = propertiesIntrospectionFieldReducer;
var definitionsIntrospectionFieldReducer = function (acc, curr) {
if (typeGuards_1.isIntrospectionField(curr)) {
var returnType = typeGuards_1.isNonNullIntrospectionType(curr.type)
? typesMapping_1.graphqlToJSONType(curr.type.ofType)
: typesMapping_1.graphqlToJSONType(curr.type);
acc[curr.name] = returnType;
}
else if (typeGuards_1.isIntrospectionInputValue(curr)) {
var returnType = typeGuards_1.isNonNullIntrospectionType(curr.type)
? typesMapping_1.graphqlToJSONType(curr.type.ofType)
: typesMapping_1.graphqlToJSONType(curr.type);
acc[curr.name] = returnType;
if (curr.defaultValue) {
acc[curr.name]["default"] = exports.resolveDefaultValue(curr);
}
}
acc[curr.name].description = curr.description || undefined;
return acc;
};
exports.definitionsIntrospectionFieldReducer = definitionsIntrospectionFieldReducer;
exports.introspectionFieldReducer = introspectionFieldReducer;
var resolveDefaultValue = function (curr) {

@@ -77,10 +57,8 @@ return typeGuards_1.isIntrospectionEnumType(curr.type)

var introspectionTypeReducer = function (type) { return function (acc, curr) {
var fieldReducer = type === 'definitions'
? exports.definitionsIntrospectionFieldReducer
: exports.propertiesIntrospectionFieldReducer;
var isQueriesOrMutations = type === 'properties';
if (typeGuards_1.isIntrospectionObjectType(curr)) {
acc[curr.name] = {
type: 'object',
properties: lodash_1.reduce(curr.fields, fieldReducer, {}),
required: type === 'definitions' ? exports.getRequiredFields(curr.fields) : []
properties: lodash_1.reduce(curr.fields, exports.introspectionFieldReducer, {}),
required: isQueriesOrMutations ? [] : exports.getRequiredFields(curr.fields)
};

@@ -91,3 +69,3 @@ }

type: 'object',
properties: lodash_1.reduce(curr.inputFields, fieldReducer, {}),
properties: lodash_1.reduce(curr.inputFields, exports.introspectionFieldReducer, {}),
required: exports.getRequiredFields(curr.inputFields)

@@ -94,0 +72,0 @@ };

@@ -6,3 +6,3 @@ "use strict";

var getTodoSchemaIntrospection = function () {
var schema = graphql_1.buildSchema("\n \"A ToDo Object\"\n type Todo {\n \"A unique identifier\"\n id: String!\n name: String!\n completed: Boolean\n color: Color\n \"A required list containing colors that cannot contain nulls\"\n requiredColors: [Color!]!\n \"A non-required list containing colors that cannot contain nulls\"\n optionalColors: [Color!]\n }\n\n \"\"\"\n A type that describes ToDoInputType. Its description might not\n fit within the bounds of 80 width and so you want MULTILINE\n \"\"\"\n input TodoInputType {\n name: String!\n completed: Boolean\n color: Color=RED\n }\n\n enum Color {\n \"Red color\"\n RED\n \"Green color\"\n GREEN\n }\n\n type Query {\n todo(\n \"todo identifier\"\n id: String!\n isCompleted: Boolean=false\n requiredStatuses: [String!]!\n optionalStatuses: [String!]\n ): Todo!\n todos: [Todo!]!\n }\n\n type Mutation {\n update_todo(id: String!, todo: TodoInputType!): Todo\n create_todo(todo: TodoInputType!): Todo\n }\n");
var schema = graphql_1.buildSchema("\n \"A ToDo Object\"\n type Todo {\n \"A unique identifier\"\n id: String!\n name: String!\n completed: Boolean\n color: Color\n \"A required list containing colors that cannot contain nulls\"\n requiredColors: [Color!]!\n \"A non-required list containing colors that cannot contain nulls\"\n optionalColors: [Color!]\n fieldWithOptionalArgument(\n optionalFilter: [String!]\n ): [String!]\n fieldWithRequiredArgument(\n requiredFilter: [String!]!\n ): [String!]\n }\n\n \"\"\"\n A type that describes ToDoInputType. Its description might not\n fit within the bounds of 80 width and so you want MULTILINE\n \"\"\"\n input TodoInputType {\n name: String!\n completed: Boolean\n color: Color=RED\n }\n\n enum Color {\n \"Red color\"\n RED\n \"Green color\"\n GREEN\n }\n\n type Query {\n todo(\n \"todo identifier\"\n id: String!\n isCompleted: Boolean=false\n requiredStatuses: [String!]!\n optionalStatuses: [String!]\n ): Todo!\n todos: [Todo!]!\n }\n\n type Mutation {\n update_todo(id: String!, todo: TodoInputType!): Todo\n create_todo(todo: TodoInputType!): Todo\n }\n");
var result = graphql_1.graphqlSync(schema, graphql_1.getIntrospectionQuery());

@@ -104,3 +104,4 @@ return {

}
}
},
required: []
}

@@ -113,15 +114,98 @@ },

properties: {
id: { type: 'string', description: 'A unique identifier' },
name: { type: 'string' },
completed: { type: 'boolean' },
color: { $ref: '#/definitions/Color' },
id: {
description: 'A unique identifier',
type: 'object',
properties: {
"return": { type: 'string' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
},
name: {
type: 'object',
properties: {
"return": { type: 'string' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
},
completed: {
type: 'object',
properties: {
"return": { type: 'boolean' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
},
color: {
type: 'object',
properties: {
"return": { $ref: '#/definitions/Color' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
},
requiredColors: {
description: 'A required list containing colors that cannot contain nulls',
type: 'array',
items: { $ref: '#/definitions/Color' }
type: 'object',
properties: {
"return": {
type: 'array',
items: { $ref: '#/definitions/Color' }
},
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
},
optionalColors: {
description: 'A non-required list containing colors that cannot contain nulls',
type: 'array',
items: { $ref: '#/definitions/Color' }
type: 'object',
properties: {
"return": {
type: 'array',
items: { $ref: '#/definitions/Color' }
},
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
},
fieldWithOptionalArgument: {
type: 'object',
properties: {
"return": {
type: 'array',
items: { type: 'string' }
},
arguments: {
type: 'object',
properties: {
optionalFilter: {
type: 'array',
items: { type: 'string' }
}
},
required: []
}
},
required: []
},
fieldWithRequiredArgument: {
type: 'object',
properties: {
"return": {
type: 'array',
items: { type: 'string' }
},
arguments: {
type: 'object',
properties: {
requiredFilter: {
type: 'array',
items: { type: 'string' }
}
},
required: ['requiredFilter']
}
},
required: []
}

@@ -128,0 +212,0 @@ },

@@ -35,7 +35,7 @@ import {

if (isNonNullIntrospectionType(f.type)) {
acc.push(f.name)
acc.push(f.name)
}
return acc
},
[],
[]
)

@@ -47,4 +47,4 @@

// reducer for a queries/mutations
export const propertiesIntrospectionFieldReducer: MemoListIterator<
// reducer for a types and inputs
export const introspectionFieldReducer: MemoListIterator<
IntrospectionFieldReducerItem,

@@ -67,3 +67,3 @@ JSONSchema6Acc,

curr.args as IntrospectionFieldReducerItem[],
propertiesIntrospectionFieldReducer,
introspectionFieldReducer,
{}

@@ -91,29 +91,2 @@ ),

// reducer for a custom types
export const definitionsIntrospectionFieldReducer: MemoListIterator<
IntrospectionFieldReducerItem,
JSONSchema6Acc,
ReadonlyArray<IntrospectionFieldReducerItem>
> = (acc, curr: IntrospectionFieldReducerItem): JSONSchema6Acc => {
if (isIntrospectionField(curr)) {
const returnType = isNonNullIntrospectionType(curr.type)
? graphqlToJSONType(curr.type.ofType)
: graphqlToJSONType(curr.type)
acc[curr.name] = returnType
} else if (isIntrospectionInputValue(curr)) {
const returnType = isNonNullIntrospectionType(curr.type)
? graphqlToJSONType(curr.type.ofType)
: graphqlToJSONType(curr.type)
acc[curr.name] = returnType
if (curr.defaultValue) {
acc[curr.name].default = resolveDefaultValue(curr)
}
}
acc[curr.name].description = curr.description || undefined
return acc
}
// ENUM type defaults will not JSON.parse correctly, so if it is an ENUM then don't

@@ -136,6 +109,3 @@ // try to do that.

> = (type) => (acc, curr: IntrospectionType): JSONSchema6Acc => {
const fieldReducer =
type === 'definitions'
? definitionsIntrospectionFieldReducer
: propertiesIntrospectionFieldReducer
const isQueriesOrMutations = type === 'properties'

@@ -147,7 +117,9 @@ if (isIntrospectionObjectType(curr)) {

curr.fields as IntrospectionFieldReducerItem[],
fieldReducer,
introspectionFieldReducer,
{}
),
// ignore required for Mutations/Queries
required: type === 'definitions' ? getRequiredFields(curr.fields) : [],
// Query and Mutation are special Types, whose fields represent the individual
// queries and mutations. None of them ought to not be considered required, even if
// their return value is a NON_NULL one.
required: isQueriesOrMutations ? [] : getRequiredFields(curr.fields),
}

@@ -159,3 +131,3 @@ } else if (isIntrospectionInputObjectType(curr)) {

curr.inputFields as IntrospectionFieldReducerItem[],
fieldReducer,
introspectionFieldReducer,
{}

@@ -162,0 +134,0 @@ ),

{
"name": "graphql-2-json-schema",
"version": "0.4.0",
"version": "0.5.0",
"main": "dist/index.js",

@@ -17,3 +17,4 @@ "repository": "git@github.com:wittydeveloper/graphql-to-json-schema.git",

"prettier:check": "prettier -c \"**/*.ts\"",
"prettier:format": "prettier -w \"**/*.ts\""
"prettier:format": "prettier -w \"**/*.ts\"",
"generateReadmeExample": "npx ts-node doc-exampleGenerator.ts"
},

@@ -20,0 +21,0 @@ "devDependencies": {

@@ -28,36 +28,39 @@ # GraphQL Schema to JSON Schema [![npm version](https://badge.fury.io/js/graphql-2-json-schema.svg)](https://badge.fury.io/js/graphql-2-json-schema)

### Input
```graphql
type Todo {
id: String!
name: String!
completed: Boolean
color: Color
colors: [Color!]!
}
type Todo {
id: String!
name: String!
completed: Boolean
color: Color
"A field that requires an argument"
colors(filter: [Color!]!): [Color!]!
}
input TodoInputType {
name: String!
completed: Boolean
color: Color=RED
}
input TodoInputType {
name: String!
completed: Boolean
color: Color=RED
}
enum Color {
"Red color"
RED
"Green color"
GREEN
}
enum Color {
"Red color"
RED
"Green color"
GREEN
}
type Query {
todo(id: String!, isCompleted: Boolean=false): Todo
todos: [Todo]
}
type Query {
"A Query with 1 required argument and 1 optional argument"
todo(id: String!, isCompleted: Boolean=false): Todo
todos: [Todo]
}
type Mutation {
update_todo(id: String!, todo: TodoInputType!): Todo
create_todo(todo: TodoInputType!): Todo
}
type Mutation {
"A Mutation with 1 required argument"
create_todo(todo: TodoInputType!): Todo
"A Mutation with 2 required arguments"
update_todo(id: String!, todo: TodoInputType!): Todo
}
```

@@ -69,123 +72,167 @@

{
$schema: 'http://json-schema.org/draft-06/schema#',
properties: {
Query: {
type: 'object',
properties: {
todo: {
type: 'object',
properties: {
arguments: {
type: 'object',
properties: {
id: { type: 'string' },
isCompleted: {
type: 'boolean',
default: false
}
},
required: ['id']
},
return: {
$ref: '#/definitions/Todo'
}
},
required: []
},
todos: {
type: 'object',
properties: {
arguments: {
type: 'object',
properties: {},
required: []
},
return: {
type: 'array',
items: { $ref: '#/definitions/Todo' }
}
},
required: []
'$schema': 'http://json-schema.org/draft-06/schema#',
properties: {
Query: {
type: 'object',
properties: {
todo: {
description: 'A Query with 1 required argument and 1 optional argument',
type: 'object',
properties: {
return: { '$ref': '#/definitions/Todo' },
arguments: {
type: 'object',
properties: {
id: { '$ref': '#/definitions/String', type: 'string' },
isCompleted: {
'$ref': '#/definitions/Boolean',
type: 'boolean',
default: false
}
},
required: []
},
Mutation: {
type: 'object',
properties: {
update_todo: {
type: 'object',
properties: {
arguments: {
type: 'object',
properties: {
id: { type: 'string' },
todo: { $ref: '#/definitions/TodoInputType' }
},
required: ['id', 'todo']
},
return: {
$ref: '#/definitions/Todo'
}
},
required: []
},
create_todo: {
type: 'object',
properties: {
arguments: {
type: 'object',
properties: {
todo: { $ref: '#/definitions/TodoInputType' }
},
required: ['todo']
},
return: {
$ref: '#/definitions/Todo'
}
},
required: []
}
},
required: [ 'id' ]
}
},
required: []
},
todos: {
type: 'object',
properties: {
return: { type: 'array', items: { '$ref': '#/definitions/Todo' } },
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
}
},
required: []
},
definitions: {
'Todo': {
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
completed: { type: 'boolean' },
color: { $ref: '#/definitions/Color' },
colors: {
type: 'array',
items: { $ref: '#/definitions/Color' }
},
},
required: ['id', 'name', 'colors']
Mutation: {
type: 'object',
properties: {
create_todo: {
description: 'A Mutation with 1 required argument',
type: 'object',
properties: {
return: { '$ref': '#/definitions/Todo' },
arguments: {
type: 'object',
properties: { todo: { '$ref': '#/definitions/TodoInputType' } },
required: [ 'todo' ]
}
},
required: []
},
'Color': {
type: 'string',
anyOf: [
{
enum: ['RED'],
title: 'Red color',
},
{
enum: ['GREEN'],
title: 'Green color',
update_todo: {
description: 'A Mutation with 2 required arguments',
type: 'object',
properties: {
return: { '$ref': '#/definitions/Todo' },
arguments: {
type: 'object',
properties: {
id: { '$ref': '#/definitions/String', type: 'string' },
todo: { '$ref': '#/definitions/TodoInputType' }
},
required: [ 'id', 'todo' ]
}
},
required: []
}
},
required: []
}
},
definitions: {
Todo: {
type: 'object',
properties: {
id: {
type: 'object',
properties: {
return: { '$ref': '#/definitions/String', type: 'string' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
},
name: {
type: 'object',
properties: {
return: { '$ref': '#/definitions/String', type: 'string' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
},
completed: {
type: 'object',
properties: {
return: { '$ref': '#/definitions/Boolean', type: 'boolean' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
},
color: {
type: 'object',
properties: {
return: { '$ref': '#/definitions/Color' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: []
},
colors: {
description: 'A field that requires an argument',
type: 'object',
properties: {
return: { type: 'array', items: { '$ref': '#/definitions/Color' } },
arguments: {
type: 'object',
properties: {
filter: {
type: 'array',
items: { '$ref': '#/definitions/Color' }
}
]
},
required: [ 'filter' ]
}
},
required: []
}
},
required: [ 'id', 'name', 'colors' ]
},
TodoInputType: {
type: 'object',
properties: {
name: { '$ref': '#/definitions/String', type: 'string' },
completed: { '$ref': '#/definitions/Boolean', type: 'boolean' },
color: { '$ref': '#/definitions/Color', default: 'RED' }
},
required: [ 'name' ]
},
Color: {
type: 'string',
anyOf: [
{
description: 'Red color',
enum: [ 'RED' ],
title: 'Red color'
},
'TodoInputType': {
type: 'object',
properties: {
name: { type: 'string' },
completed: { type: 'boolean' },
color: { default: 'RED', $ref: '#/definitions/Color' },
},
required: ['name']
{
description: 'Green color',
enum: [ 'GREEN' ],
title: 'Green color'
}
]
},
String: {
description: 'The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.',
type: 'string',
title: 'String'
},
Boolean: {
description: 'The `Boolean` scalar type represents `true` or `false`.',
type: 'boolean',
title: 'Boolean'
}
}
}
```

@@ -27,2 +27,8 @@ import {

optionalColors: [Color!]
fieldWithOptionalArgument(
optionalFilter: [String!]
): [String!]
fieldWithRequiredArgument(
requiredFilter: [String!]!
): [String!]
}

@@ -65,2 +71,3 @@

const result = graphqlSync(schema, getIntrospectionQuery())
return {

@@ -87,12 +94,12 @@ introspection: result.data as IntrospectionQuery,

requiredStatuses: {
type: 'array',
items: {
type: 'string',
},
type: 'array',
items: {
type: 'string',
},
},
optionalStatuses: {
type: 'array',
items: {
type: 'string',
},
type: 'array',
items: {
type: 'string',
},
},

@@ -124,2 +131,4 @@ },

},
// Inappropriate for individual queries to be required, despite possibly having
// NON_NULL return types
required: [],

@@ -164,2 +173,5 @@ },

},
// Inappropriate for individual mutations to be required, despite possibly having
// NON_NULL return types
required: [],
},

@@ -172,16 +184,100 @@ },

properties: {
id: { type: 'string', description: 'A unique identifier' },
name: { type: 'string' },
completed: { type: 'boolean' },
color: { $ref: '#/definitions/Color' },
id: {
description: 'A unique identifier',
type: 'object',
properties: {
return: { type: 'string' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: [],
},
name: {
type: 'object',
properties: {
return: { type: 'string' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: [],
},
completed: {
type: 'object',
properties: {
return: { type: 'boolean' },
arguments: { type: 'object', properties: {}, required: [] }
},
required: [],
},
color: {
type: 'object',
properties: {
return: { $ref: '#/definitions/Color' },
arguments: { type: 'object', properties: {}, required: [] },
},
required: [],
},
requiredColors: {
description: 'A required list containing colors that cannot contain nulls',
type: 'array',
items: { $ref: '#/definitions/Color' },
type: 'object',
properties: {
return: {
type: 'array',
items: { $ref: '#/definitions/Color' },
},
arguments: { type: 'object', properties: {}, required: [] }
},
required: [],
},
optionalColors: {
description: 'A non-required list containing colors that cannot contain nulls',
type: 'array',
items: { $ref: '#/definitions/Color' },
description:
'A non-required list containing colors that cannot contain nulls',
type: 'object',
properties: {
return: {
type: 'array',
items: { $ref: '#/definitions/Color' },
},
arguments: { type: 'object', properties: {}, required: [] },
},
required: [],
},
fieldWithOptionalArgument: {
type: 'object',
properties: {
return: {
type: 'array',
items: { type: 'string' },
},
arguments: {
type: 'object',
properties: {
optionalFilter: {
type: 'array',
items: { type: 'string' },
}
},
required: [],
},
},
required: [],
},
fieldWithRequiredArgument: {
type: 'object',
properties: {
return: {
type: 'array',
items: { type: 'string' },
},
arguments: {
type: 'object',
properties: {
requiredFilter: {
type: 'array',
items: { type: 'string' },
}
},
required: ['requiredFilter'],
},
},
required: [],
}
},

@@ -188,0 +284,0 @@ required: ['id', 'name', 'requiredColors'],

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