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.9.0 to 0.9.1

2

CHANGELOG.md

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

### [0.9.1](https://github.com/wittydeveloper/graphql-to-json-schema/compare/v0.9.0...v0.9.1) (2022-03-07)
## [0.9.0](https://github.com/wittydeveloper/graphql-to-json-schema/compare/v0.8.0...v0.9.0) (2021-09-22)

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

13

dist/lib/reducer.js
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.introspectionTypeReducer = exports.resolveDefaultValue = exports.introspectionFieldReducerGenerator = exports.getRequiredFields = void 0;
const json5_1 = __importDefault(require("json5"));
const lodash_1 = require("lodash");

@@ -63,4 +67,4 @@ const typeGuards_1 = require("./typeGuards");

}
if (!typeGuards_1.isIntrospectionEnumType(type)) {
return JSON.parse(curr.defaultValue);
if (typeof curr.defaultValue === 'string' && !typeGuards_1.isIntrospectionEnumType(type)) {
return json5_1.default.parse(curr.defaultValue);
}

@@ -70,3 +74,6 @@ if (!isList || !curr.defaultValue || typeof curr.defaultValue !== 'string') {

}
return curr.defaultValue.substr(1, curr.defaultValue.length - 2).split(',').map((val) => val.trim());
return curr.defaultValue
.substr(1, curr.defaultValue.length - 2)
.split(',')
.map((val) => val.trim());
};

@@ -73,0 +80,0 @@ exports.resolveDefaultValue = resolveDefaultValue;

import { IntrospectionEnumType, IntrospectionField, IntrospectionInputObjectType, IntrospectionInterfaceType, IntrospectionInputTypeRef, IntrospectionInputValue, IntrospectionListTypeRef, IntrospectionNamedTypeRef, IntrospectionNonNullTypeRef, IntrospectionObjectType, IntrospectionOutputTypeRef, IntrospectionSchema, IntrospectionType, IntrospectionTypeRef, IntrospectionUnionType, IntrospectionScalarType } from 'graphql';
export declare const SUPPORTED_KINDS: ("SCALAR" | "OBJECT" | "INPUT_OBJECT" | "INTERFACE" | "ENUM" | "UNION")[];
export declare const isIntrospectionField: (type: IntrospectionField | IntrospectionInputValue) => type is IntrospectionField;
export declare const isIntrospectionInputValue: (type: IntrospectionField | IntrospectionInputValue) => type is IntrospectionInputValue;
export declare const isIntrospectionListTypeRef: (type: IntrospectionTypeRef | IntrospectionInputTypeRef | IntrospectionOutputTypeRef) => type is IntrospectionListTypeRef<IntrospectionTypeRef>;
export declare const isNonNullIntrospectionType: (type: IntrospectionTypeRef) => type is IntrospectionNonNullTypeRef<IntrospectionNamedTypeRef<IntrospectionType>>;
export declare const isIntrospectionScalarType: (type: IntrospectionSchema['types'][0]) => type is IntrospectionScalarType;
export declare const isIntrospectionObjectType: (type: IntrospectionSchema['types'][0]) => type is IntrospectionObjectType;

@@ -10,4 +13,2 @@ export declare const isIntrospectionInputObjectType: (type: IntrospectionSchema['types'][0]) => type is IntrospectionInputObjectType;

export declare const isIntrospectionUnionType: (type: IntrospectionSchema['types'][0]) => type is IntrospectionUnionType;
export declare const isNonNullIntrospectionType: (type: IntrospectionTypeRef) => type is IntrospectionNonNullTypeRef<IntrospectionNamedTypeRef<IntrospectionType>>;
export declare const isIntrospectionScalarType: (type: IntrospectionSchema['types'][0]) => type is IntrospectionScalarType;
export declare const isIntrospectionDefaultScalarType: (type: IntrospectionSchema['types'][0]) => type is IntrospectionScalarType;

@@ -14,0 +15,0 @@ export interface FilterDefinitionsTypesOptions {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.filterDefinitionsTypes = exports.isIntrospectionDefaultScalarType = exports.isIntrospectionScalarType = exports.isNonNullIntrospectionType = exports.isIntrospectionUnionType = exports.isIntrospectionEnumType = exports.isIntrospectionInterfaceType = exports.isIntrospectionInputObjectType = exports.isIntrospectionObjectType = exports.isIntrospectionListTypeRef = exports.isIntrospectionInputValue = exports.isIntrospectionField = void 0;
exports.filterDefinitionsTypes = exports.isIntrospectionDefaultScalarType = exports.isIntrospectionUnionType = exports.isIntrospectionEnumType = exports.isIntrospectionInterfaceType = exports.isIntrospectionInputObjectType = exports.isIntrospectionObjectType = exports.isIntrospectionScalarType = exports.isNonNullIntrospectionType = exports.isIntrospectionListTypeRef = exports.isIntrospectionInputValue = exports.isIntrospectionField = exports.SUPPORTED_KINDS = void 0;
const graphql_1 = require("graphql");
const lodash_1 = require("lodash");
exports.SUPPORTED_KINDS = [
graphql_1.TypeKind.SCALAR,
graphql_1.TypeKind.OBJECT,
graphql_1.TypeKind.INPUT_OBJECT,
graphql_1.TypeKind.INTERFACE,
graphql_1.TypeKind.ENUM,
graphql_1.TypeKind.UNION,
];
const isIntrospectionField = (type) => lodash_1.has(type, 'args');

@@ -9,19 +18,19 @@ exports.isIntrospectionField = isIntrospectionField;

exports.isIntrospectionInputValue = isIntrospectionInputValue;
const isIntrospectionListTypeRef = (type) => type.kind === 'LIST';
const isIntrospectionListTypeRef = (type) => type.kind === graphql_1.TypeKind.LIST;
exports.isIntrospectionListTypeRef = isIntrospectionListTypeRef;
const isIntrospectionObjectType = (type) => type.kind === 'OBJECT';
const isNonNullIntrospectionType = (type) => type.kind === graphql_1.TypeKind.NON_NULL;
exports.isNonNullIntrospectionType = isNonNullIntrospectionType;
const isIntrospectionScalarType = (type) => type.kind === graphql_1.TypeKind.SCALAR;
exports.isIntrospectionScalarType = isIntrospectionScalarType;
const isIntrospectionObjectType = (type) => type.kind === graphql_1.TypeKind.OBJECT;
exports.isIntrospectionObjectType = isIntrospectionObjectType;
const isIntrospectionInputObjectType = (type) => type.kind === 'INPUT_OBJECT';
const isIntrospectionInputObjectType = (type) => type.kind === graphql_1.TypeKind.INPUT_OBJECT;
exports.isIntrospectionInputObjectType = isIntrospectionInputObjectType;
const isIntrospectionInterfaceType = (type) => type.kind === 'INTERFACE';
const isIntrospectionInterfaceType = (type) => type.kind === graphql_1.TypeKind.INTERFACE;
exports.isIntrospectionInterfaceType = isIntrospectionInterfaceType;
const isIntrospectionEnumType = (type) => type.kind === 'ENUM';
const isIntrospectionEnumType = (type) => type.kind === graphql_1.TypeKind.ENUM;
exports.isIntrospectionEnumType = isIntrospectionEnumType;
const isIntrospectionUnionType = (type) => type.kind === 'UNION';
const isIntrospectionUnionType = (type) => type.kind === graphql_1.TypeKind.UNION;
exports.isIntrospectionUnionType = isIntrospectionUnionType;
const isNonNullIntrospectionType = (type) => type.kind === 'NON_NULL';
exports.isNonNullIntrospectionType = isNonNullIntrospectionType;
const isIntrospectionScalarType = (type) => type.kind === 'SCALAR';
exports.isIntrospectionScalarType = isIntrospectionScalarType;
const isIntrospectionDefaultScalarType = (type) => type.kind === 'SCALAR' &&
const isIntrospectionDefaultScalarType = (type) => type.kind === graphql_1.TypeKind.SCALAR &&
lodash_1.includes(['Boolean', 'String', 'Int', 'Float'], type.name);

@@ -31,10 +40,10 @@ exports.isIntrospectionDefaultScalarType = isIntrospectionDefaultScalarType;

const ignoreInternals = opts && opts.ignoreInternals;
return lodash_1.filter(types, (type) => ((exports.isIntrospectionObjectType(type) && !!type.fields) ||
return lodash_1.filter(types, (type) => ((exports.isIntrospectionScalarType(type) && !!type.name) ||
(exports.isIntrospectionObjectType(type) && !!type.fields) ||
(exports.isIntrospectionInputObjectType(type) && !!type.inputFields) ||
(exports.isIntrospectionInterfaceType(type) && !!type.fields) ||
(exports.isIntrospectionEnumType(type) && !!type.enumValues) ||
(exports.isIntrospectionUnionType(type) && !!type.possibleTypes) ||
(exports.isIntrospectionScalarType(type) && !!type.name)) &&
(exports.isIntrospectionUnionType(type) && !!type.possibleTypes)) &&
(!ignoreInternals || (ignoreInternals && !lodash_1.startsWith(type.name, '__'))));
};
exports.filterDefinitionsTypes = filterDefinitionsTypes;

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

const jsonType = {};
if (lodash_1.includes(['OBJECT', 'INPUT_OBJECT', 'INTERFACE', 'ENUM', 'SCALAR'], k.kind)) {
if (lodash_1.includes(typeGuards_1.SUPPORTED_KINDS, k.kind)) {
jsonType.$ref = `#/definitions/${name}`;

@@ -30,0 +30,0 @@ }

@@ -61,11 +61,21 @@ "use strict";

input TodoInputType {
name: String!
completed: Boolean
color: Color=RED
name: String!
completed: Boolean
color: Color=RED
contactInfo: ContactInfoInputType = {
email: "spam@example.dev"
}
}
"""
Description of ContactInfoInputType.
"""
input ContactInfoInputType {
email: String
}
"Anything with an ID can be a node"
interface Node {
"A unique identifier"
id: String!
"A unique identifier"
id: String!
}

@@ -95,2 +105,3 @@

todos: [Todo!]!
todoUnions: [TodoUnion]
node(

@@ -105,2 +116,3 @@ "Node identifier"

create_todo(todo: TodoInputType!): Todo
create_todo_union(id: String!): TodoUnion
}

@@ -157,3 +169,6 @@ `);

requiredColor: { $ref: '#/definitions/Color' },
requiredColorWithDefault: { $ref: '#/definitions/Color', default: 'RED' },
requiredColorWithDefault: {
$ref: '#/definitions/Color',
default: 'RED',
},
colors: {

@@ -221,2 +236,19 @@ type: 'array',

},
todoUnions: {
type: 'object',
properties: {
arguments: {
type: 'object',
properties: {},
required: [],
},
return: {
type: 'array',
items: {
anyOf: [{ $ref: '#/definitions/TodoUnion' }, { type: 'null' }],
},
},
},
required: [],
},
node: {

@@ -280,2 +312,18 @@ type: 'object',

},
create_todo_union: {
type: 'object',
properties: {
arguments: {
type: 'object',
properties: {
id: { $ref: '#/definitions/String' },
},
required: ['id'],
},
return: {
$ref: '#/definitions/TodoUnion',
},
},
required: [],
},
},

@@ -497,5 +545,17 @@ required: [],

color: { default: 'RED', $ref: '#/definitions/Color' },
contactInfo: {
$ref: '#/definitions/ContactInfoInputType',
default: { email: 'spam@example.dev' },
},
},
required: ['name'],
},
ContactInfoInputType: {
type: 'object',
description: 'Description of ContactInfoInputType.',
properties: {
email: { $ref: '#/definitions/String' },
},
required: [],
},
TodoUnion: {

@@ -502,0 +562,0 @@ description: 'A Union of Todo and SimpleTodo',

@@ -0,1 +1,2 @@

import JSON5 from 'json5'
import {

@@ -115,21 +116,24 @@ IntrospectionField,

if (isIntrospectionListTypeRef(type)) {
isList = true
type = type.ofType
isList = true
type = type.ofType
} else if (isNonNullIntrospectionType(type)) {
type = type.ofType
type = type.ofType
} else {
break
break
}
}
// Not an ENUM? No problem...just JSON parse it
if (!isIntrospectionEnumType(type)) {
return JSON.parse(curr.defaultValue)
if (typeof curr.defaultValue === 'string' && !isIntrospectionEnumType(type)) {
return JSON5.parse(curr.defaultValue)
}
if (!isList || !curr.defaultValue || typeof curr.defaultValue !== 'string') {
return curr.defaultValue
return curr.defaultValue
}
// Take a string like "[RED, GREEN]" and convert it to ["RED", "GREEN"]
return curr.defaultValue.substr(1, curr.defaultValue.length - 2).split(',').map((val: string) => val.trim())
return curr.defaultValue
.substr(1, curr.defaultValue.length - 2)
.split(',')
.map((val: string) => val.trim())
}

@@ -136,0 +140,0 @@

@@ -18,5 +18,15 @@ import {

IntrospectionScalarType,
TypeKind,
} from 'graphql'
import { filter, has, startsWith, includes } from 'lodash'
export const SUPPORTED_KINDS = [
TypeKind.SCALAR,
TypeKind.OBJECT,
TypeKind.INPUT_OBJECT,
TypeKind.INTERFACE,
TypeKind.ENUM,
TypeKind.UNION,
]
///////////////////

@@ -40,37 +50,38 @@ /// Type guards ///

| IntrospectionOutputTypeRef
): type is IntrospectionListTypeRef => type.kind === 'LIST'
): type is IntrospectionListTypeRef => type.kind === TypeKind.LIST
export const isNonNullIntrospectionType = (
type: IntrospectionTypeRef
): type is IntrospectionNonNullTypeRef<
IntrospectionNamedTypeRef<IntrospectionType>
> => type.kind === TypeKind.NON_NULL
export const isIntrospectionScalarType = (
type: IntrospectionSchema['types'][0]
): type is IntrospectionScalarType => type.kind === TypeKind.SCALAR
export const isIntrospectionObjectType = (
type: IntrospectionSchema['types'][0]
): type is IntrospectionObjectType => type.kind === 'OBJECT'
): type is IntrospectionObjectType => type.kind === TypeKind.OBJECT
export const isIntrospectionInputObjectType = (
type: IntrospectionSchema['types'][0]
): type is IntrospectionInputObjectType => type.kind === 'INPUT_OBJECT'
): type is IntrospectionInputObjectType => type.kind === TypeKind.INPUT_OBJECT
export const isIntrospectionInterfaceType = (
type: IntrospectionSchema['types'][0]
): type is IntrospectionInterfaceType => type.kind === 'INTERFACE'
): type is IntrospectionInterfaceType => type.kind === TypeKind.INTERFACE
export const isIntrospectionEnumType = (
type: IntrospectionSchema['types'][0]
): type is IntrospectionEnumType => type.kind === 'ENUM'
): type is IntrospectionEnumType => type.kind === TypeKind.ENUM
export const isIntrospectionUnionType = (
type: IntrospectionSchema['types'][0]
): type is IntrospectionUnionType => type.kind === 'UNION'
): type is IntrospectionUnionType => type.kind === TypeKind.UNION
export const isNonNullIntrospectionType = (
type: IntrospectionTypeRef
): type is IntrospectionNonNullTypeRef<
IntrospectionNamedTypeRef<IntrospectionType>
> => type.kind === 'NON_NULL'
export const isIntrospectionScalarType = (
type: IntrospectionSchema['types'][0]
): type is IntrospectionScalarType => type.kind === 'SCALAR'
export const isIntrospectionDefaultScalarType = (
type: IntrospectionSchema['types'][0]
): type is IntrospectionScalarType =>
type.kind === 'SCALAR' &&
type.kind === TypeKind.SCALAR &&
includes(['Boolean', 'String', 'Int', 'Float'], type.name)

@@ -90,10 +101,10 @@

(type) =>
((isIntrospectionObjectType(type) && !!type.fields) ||
((isIntrospectionScalarType(type) && !!type.name) ||
(isIntrospectionObjectType(type) && !!type.fields) ||
(isIntrospectionInputObjectType(type) && !!type.inputFields) ||
(isIntrospectionInterfaceType(type) && !!type.fields) ||
(isIntrospectionEnumType(type) && !!type.enumValues) ||
(isIntrospectionUnionType(type) && !!type.possibleTypes) ||
(isIntrospectionScalarType(type) && !!type.name)) &&
(isIntrospectionUnionType(type) && !!type.possibleTypes)) &&
(!ignoreInternals || (ignoreInternals && !startsWith(type.name, '__')))
)
}

@@ -12,2 +12,3 @@ import {

import {
SUPPORTED_KINDS,
isIntrospectionListTypeRef,

@@ -59,8 +60,3 @@ isNonNullIntrospectionType,

if (
includes(
['OBJECT', 'INPUT_OBJECT', 'INTERFACE', 'ENUM', 'SCALAR'],
k.kind
)
) {
if (includes(SUPPORTED_KINDS, k.kind)) {
jsonType.$ref = `#/definitions/${name}`

@@ -67,0 +63,0 @@ } else {

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

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

"dependencies": {
"json5": "^2.2.0",
"lodash": "^4.17.20"

@@ -14,0 +15,0 @@ },

@@ -71,11 +71,21 @@ import {

input TodoInputType {
name: String!
completed: Boolean
color: Color=RED
name: String!
completed: Boolean
color: Color=RED
contactInfo: ContactInfoInputType = {
email: "spam@example.dev"
}
}
"""
Description of ContactInfoInputType.
"""
input ContactInfoInputType {
email: String
}
"Anything with an ID can be a node"
interface Node {
"A unique identifier"
id: String!
"A unique identifier"
id: String!
}

@@ -105,2 +115,3 @@

todos: [Todo!]!
todoUnions: [TodoUnion]
node(

@@ -115,2 +126,3 @@ "Node identifier"

create_todo(todo: TodoInputType!): Todo
create_todo_union(id: String!): TodoUnion
}

@@ -170,3 +182,6 @@ `)

requiredColor: { $ref: '#/definitions/Color' },
requiredColorWithDefault: { $ref: '#/definitions/Color', default: 'RED' },
requiredColorWithDefault: {
$ref: '#/definitions/Color',
default: 'RED',
},

@@ -235,2 +250,19 @@ colors: {

},
todoUnions: {
type: 'object',
properties: {
arguments: {
type: 'object',
properties: {},
required: [],
},
return: {
type: 'array',
items: {
anyOf: [{ $ref: '#/definitions/TodoUnion' }, { type: 'null' }],
},
},
},
required: [],
},
node: {

@@ -296,2 +328,18 @@ type: 'object',

},
create_todo_union: {
type: 'object',
properties: {
arguments: {
type: 'object',
properties: {
id: { $ref: '#/definitions/String' },
},
required: ['id'],
},
return: {
$ref: '#/definitions/TodoUnion',
},
},
required: [],
},
},

@@ -520,5 +568,17 @@ // Inappropriate for individual mutations to be required, despite possibly having

color: { default: 'RED', $ref: '#/definitions/Color' },
contactInfo: {
$ref: '#/definitions/ContactInfoInputType',
default: { email: 'spam@example.dev' },
},
},
required: ['name'],
},
ContactInfoInputType: {
type: 'object',
description: 'Description of ContactInfoInputType.',
properties: {
email: { $ref: '#/definitions/String' },
},
required: [],
},
TodoUnion: {

@@ -525,0 +585,0 @@ description: 'A Union of Todo and SimpleTodo',

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