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
5
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-graphql-from-jsonschema - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

build/lib/toBreadcrumb.d.ts

4

build/lib/getGraphqlFromJsonSchema.d.ts
import { JSONSchema4 } from 'json-schema';
declare const getGraphqlFromJsonSchema: ({ name, schema, direction }: {
name: string;
declare const getGraphqlFromJsonSchema: ({ rootName, schema, direction }: {
rootName: string;
schema: JSONSchema4;

@@ -5,0 +5,0 @@ direction?: "input" | "output" | undefined;

@@ -7,5 +7,5 @@ "use strict";

const parseSchema_1 = __importDefault(require("./parseSchema"));
const getGraphqlFromJsonSchema = function ({ name, schema, direction = 'output' }) {
return parseSchema_1.default({ name, schema, direction });
const getGraphqlFromJsonSchema = function ({ rootName, schema, direction = 'output' }) {
return parseSchema_1.default({ path: [rootName], schema, direction });
};
exports.default = getGraphqlFromJsonSchema;
import { Direction } from './Direction';
import { JSONSchema4 } from 'json-schema';
declare const handleArrayType: ({ name, schema, direction }: {
name: string;
declare const handleArrayType: ({ path, schema, direction }: {
path: string[];
schema: JSONSchema4;

@@ -6,0 +6,0 @@ direction: Direction;

@@ -8,10 +8,11 @@ "use strict";

const parseSchema_1 = __importDefault(require("./parseSchema"));
const handleArrayType = function ({ name, schema, direction }) {
const toBreadcrumb_1 = __importDefault(require("./toBreadcrumb"));
const handleArrayType = function ({ path, schema, direction }) {
if (!schema.items) {
throw new errors_1.default.SchemaInvalid(`Property 'items' is missing.`);
throw new errors_1.default.SchemaInvalid(`Property 'items' at '${toBreadcrumb_1.default(path)}' is missing.`);
}
if (Array.isArray(schema.items)) {
throw new errors_1.default.SchemaInvalid(`Property 'items' must not be an array.`);
throw new errors_1.default.SchemaInvalid(`Property 'items' at '${toBreadcrumb_1.default(path)}' must not be an array.`);
}
const { typeName: graphqlTypeName, typeDefinitions: graphqlTypeDefinitions } = parseSchema_1.default({ name, schema: schema.items, direction });
const { typeName: graphqlTypeName, typeDefinitions: graphqlTypeDefinitions } = parseSchema_1.default({ path, schema: schema.items, direction });
return {

@@ -18,0 +19,0 @@ typeName: `[${graphqlTypeName}]`,

import { Direction } from './Direction';
import { JSONSchema4 } from 'json-schema';
declare const handleObjectType: ({ name, schema, direction }: {
name: string;
declare const handleObjectType: ({ path, schema, direction }: {
path: string[];
schema: JSONSchema4;

@@ -6,0 +6,0 @@ direction: Direction;

@@ -8,8 +8,12 @@ "use strict";

const parseSchema_1 = __importDefault(require("./parseSchema"));
const toBreadcrumb_1 = __importDefault(require("./toBreadcrumb"));
const toPascalCase_1 = __importDefault(require("./toPascalCase"));
const handleObjectType = function ({ name, schema, direction }) {
const handleObjectType = function ({ path, schema, direction }) {
if (!schema.properties) {
throw new errors_1.default.SchemaInvalid(`Property 'properties' is missing.`);
throw new errors_1.default.SchemaInvalid(`Property 'properties' at '${toBreadcrumb_1.default(path)}' is missing.`);
}
const graphqlTypeName = toPascalCase_1.default([name]);
if (schema.additionalProperties) {
throw new errors_1.default.SchemaInvalid(`Property 'additionalProperties' at '${toBreadcrumb_1.default(path)}' must not be true.`);
}
const graphqlTypeName = toPascalCase_1.default(path);
const graphqlTypeDefinitions = [];

@@ -27,3 +31,3 @@ let currentGraphqlTypeDefinition = '';

const { typeName: propertyGraphqlTypeName, typeDefinitions: propertyGraphqlTypeDefinitions } = parseSchema_1.default({
name: toPascalCase_1.default([name, propertyName]),
path: [...path, propertyName],
schema: propertySchema,

@@ -30,0 +34,0 @@ direction

import { Direction } from './Direction';
import { JSONSchema4 } from 'json-schema';
declare const parseSchema: ({ name, schema, direction }: {
name: string;
declare const parseSchema: ({ path, schema, direction }: {
path: string[];
schema: JSONSchema4;

@@ -6,0 +6,0 @@ direction: Direction;

@@ -13,5 +13,6 @@ "use strict";

const isScalarType_1 = __importDefault(require("./isScalarType"));
const parseSchema = function ({ name, schema, direction }) {
const toBreadcrumb_1 = __importDefault(require("./toBreadcrumb"));
const parseSchema = function ({ path, schema, direction }) {
if (!schema.type) {
throw new errors_1.default.SchemaInvalid(`Property 'type' is missing.`);
throw new errors_1.default.SchemaInvalid(`Property 'type' at '${toBreadcrumb_1.default(path)}' is missing.`);
}

@@ -27,9 +28,9 @@ const jsonTypes = [schema.type].flat();

else if (isArrayType_1.default({ type: jsonType })) {
result = handleArrayType_1.default({ name, schema, direction });
result = handleArrayType_1.default({ path, schema, direction });
}
else if (isObjectType_1.default({ type: jsonType })) {
result = handleObjectType_1.default({ name, schema, direction });
result = handleObjectType_1.default({ path, schema, direction });
}
else {
throw new errors_1.default.TypeInvalid(`Type '${jsonType}' is invalid.`);
throw new errors_1.default.TypeInvalid(`Type '${jsonType}' at '${path}' is invalid.`);
}

@@ -36,0 +37,0 @@ graphqlTypeNames.push(result.typeName);

@@ -5,10 +5,10 @@ import { Direction } from './Direction';

const getGraphqlFromJsonSchema = function ({ name, schema, direction = 'output' }: {
name: string;
const getGraphqlFromJsonSchema = function ({ rootName, schema, direction = 'output' }: {
rootName: string;
schema: JSONSchema4;
direction?: Direction;
}): { typeName: string; typeDefinitions: string[] } {
return parseSchema({ name, schema, direction });
return parseSchema({ path: [ rootName ], schema, direction });
};
export default getGraphqlFromJsonSchema;

@@ -5,5 +5,6 @@ import { Direction } from './Direction';

import parseSchema from './parseSchema';
import toBreadcrumb from './toBreadcrumb';
const handleArrayType = function ({ name, schema, direction }: {
name: string;
const handleArrayType = function ({ path, schema, direction }: {
path: string[];
schema: JSONSchema4;

@@ -13,6 +14,6 @@ direction: Direction;

if (!schema.items) {
throw new errors.SchemaInvalid(`Property 'items' is missing.`);
throw new errors.SchemaInvalid(`Property 'items' at '${toBreadcrumb(path)}' is missing.`);
}
if (Array.isArray(schema.items)) {
throw new errors.SchemaInvalid(`Property 'items' must not be an array.`);
throw new errors.SchemaInvalid(`Property 'items' at '${toBreadcrumb(path)}' must not be an array.`);
}

@@ -23,3 +24,3 @@

typeDefinitions: graphqlTypeDefinitions
} = parseSchema({ name, schema: schema.items, direction });
} = parseSchema({ path, schema: schema.items, direction });

@@ -26,0 +27,0 @@ return {

@@ -5,6 +5,7 @@ import { Direction } from './Direction';

import parseSchema from './parseSchema';
import toBreadcrumb from './toBreadcrumb';
import toPascalCase from './toPascalCase';
const handleObjectType = function ({ name, schema, direction }: {
name: string;
const handleObjectType = function ({ path, schema, direction }: {
path: string[];
schema: JSONSchema4;

@@ -14,6 +15,9 @@ direction: Direction;

if (!schema.properties) {
throw new errors.SchemaInvalid(`Property 'properties' is missing.`);
throw new errors.SchemaInvalid(`Property 'properties' at '${toBreadcrumb(path)}' is missing.`);
}
if (schema.additionalProperties) {
throw new errors.SchemaInvalid(`Property 'additionalProperties' at '${toBreadcrumb(path)}' must not be true.`);
}
const graphqlTypeName = toPascalCase([ name ]);
const graphqlTypeName = toPascalCase(path);
const graphqlTypeDefinitions: string[] = [];

@@ -40,3 +44,3 @@

} = parseSchema({
name: toPascalCase([ name, propertyName ]),
path: [ ...path, propertyName ],
schema: propertySchema,

@@ -43,0 +47,0 @@ direction

@@ -10,5 +10,6 @@ import { Direction } from './Direction';

import { JSONSchema4 } from 'json-schema';
import toBreadcrumb from './toBreadcrumb';
const parseSchema = function ({ name, schema, direction }: {
name: string;
const parseSchema = function ({ path, schema, direction }: {
path: string[];
schema: JSONSchema4;

@@ -18,3 +19,3 @@ direction: Direction;

if (!schema.type) {
throw new errors.SchemaInvalid(`Property 'type' is missing.`);
throw new errors.SchemaInvalid(`Property 'type' at '${toBreadcrumb(path)}' is missing.`);
}

@@ -33,7 +34,7 @@

} else if (isArrayType({ type: jsonType })) {
result = handleArrayType({ name, schema, direction });
result = handleArrayType({ path, schema, direction });
} else if (isObjectType({ type: jsonType })) {
result = handleObjectType({ name, schema, direction });
result = handleObjectType({ path, schema, direction });
} else {
throw new errors.TypeInvalid(`Type '${jsonType}' is invalid.`);
throw new errors.TypeInvalid(`Type '${jsonType}' at '${path}' is invalid.`);
}

@@ -40,0 +41,0 @@

{
"name": "get-graphql-from-jsonschema",
"version": "1.0.0",
"version": "2.0.0",
"description": "get-graphql-from-jsonschema gets a GraphQL schema from a JSON schema.",

@@ -5,0 +5,0 @@ "contributors": [

@@ -35,7 +35,7 @@ # get-graphql-from-jsonschema

To get a GraphQL schema from a JSON schema, call the `getGraphqlFromJsonSchema` function and hand over the 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:
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:
```javascript
const { typeName, typeDefinitions } = getGraphqlFromJsonSchema({
name: 'person',
rootName: 'person',
schema: {

@@ -99,3 +99,3 @@ type: 'object',

const { typeName, typeDefinitions } = getGraphqlFromJsonSchema({
name: 'person',
rootName: 'person',
schema: {

@@ -102,0 +102,0 @@ // ...

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