Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate JSON Schema consistency #4

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ npm install graphql-json-schema
```js
const transform = require('graphql-json-schema');

// transform(schemaStr, strictMode=flase)
// - strict mode ensure a valid JSON schema generation

const schema = transform(`
scalar Foo

Expand Down Expand Up @@ -55,7 +58,7 @@ the code above prints the following JSON as a plain JS object:

```json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Foo": {
"title": "Foo",
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const parse = require('graphql/language').parse;
const transform = require('./transform.js');

module.exports = schema => {
module.exports = (schema, strictMode = false) => {
if (typeof schema !== 'string') throw new TypeError('GraphQL Schema must be a string');
return transform(parse(schema));
return transform(parse(schema), strictMode);
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"graphql": "^0.10.1"
},
"devDependencies": {
"ajv": "^6.4.0",
"jasmine": "^2.6.0"
}
}
41 changes: 10 additions & 31 deletions spec/data/mock_schema.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Foo": {
"title": "Foo",
"type": "GRAPHQL_SCALAR"
},
"MyUnion": {
"title": "MyUnion",
"type": "GRAPHQL_UNION",
"oneOf": [
{
"$ref": "#/definitions/Foo"
},
{
"type": "string",
"required": false
"type": "string"
},
{
"type": "number",
"required": false
"type": "number"
}
]
},
"MyEnum": {
"title": "MyEnum",
"type": "GRAPHQL_ENUM",
"enum": [
"FIRST_ITEM",
"SECOND_ITEM",
Expand All @@ -37,13 +33,11 @@
"properties": {
"my_field": {
"type": "integer",
"required": false,
"title": "my_field",
"arguments": []
},
"req_field": {
"type": "string",
"required": true,
"title": "req_field",
"arguments": []
},
Expand Down Expand Up @@ -89,31 +83,23 @@
"first": {
"type": "array",
"items": {
"type": {
"type": "number",
"required": false
}
"type": "number"
},
"title": "first",
"arguments": []
},
"identifier": {
"type": "array",
"items": {
"type": {
"type": "string",
"required": false
}
"type": "string"
},
"required": true,
"title": "identifier",
"arguments": []
},
"reference": {
"allOf": [
{
"$ref": "#/definitions/Stuff",
"required": true
"$ref": "#/definitions/Stuff"
},
{
"title": "reference"
Expand All @@ -122,7 +108,6 @@
},
"bool": {
"type": "boolean",
"required": true,
"title": "bool",
"arguments": []
},
Expand All @@ -138,14 +123,12 @@
},
"with_params": {
"type": "integer",
"required": false,
"title": "with_params",
"arguments": [
{
"title": "param1",
"type": {
"type": "integer",
"required": false
"type": "integer"
},
"defaultValue": null
},
Expand All @@ -154,10 +137,7 @@
"type": {
"type": "array",
"items": {
"type": {
"type": "number",
"required": false
}
"type": "number"
}
},
"defaultValue": null
Expand All @@ -167,6 +147,7 @@
},
"required": [
"identifier",
"reference",
"bool"
]
},
Expand All @@ -177,12 +158,10 @@
"properties": {
"an_int": {
"type": "integer",
"required": true,
"title": "an_int"
},
"a_string": {
"type": "string",
"required": false,
"title": "a_string"
}
},
Expand All @@ -191,4 +170,4 @@
]
}
}
}
}
147 changes: 147 additions & 0 deletions spec/data/mock_schema_strict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Foo": {
"title": "Foo"
},
"MyUnion": {
"title": "MyUnion",
"oneOf": [
{
"$ref": "#/definitions/Foo"
},
{
"type": "string"
},
{
"type": "number"
}
]
},
"MyEnum": {
"title": "MyEnum",
"enum": [
"FIRST_ITEM",
"SECOND_ITEM",
"THIRD_ITEM"
]
},
"Stuff": {
"title": "Stuff",
"type": "object",
"properties": {
"my_field": {
"type": "integer",
"title": "my_field"
},
"req_field": {
"type": "string",
"title": "req_field"
},
"recursion": {
"allOf": [
{
"$ref": "#/definitions/MoreStuff"
},
{
"title": "recursion"
}
]
},
"custom_scalar": {
"allOf": [
{
"$ref": "#/definitions/Foo"
},
{
"title": "custom_scalar"
}
]
},
"enum": {
"allOf": [
{
"$ref": "#/definitions/MyEnum"
},
{
"title": "enum"
}
]
}
},
"required": [
"req_field"
]
},
"MoreStuff": {
"title": "MoreStuff",
"type": "object",
"properties": {
"first": {
"type": "array",
"items": {
"type": "number"
},
"title": "first"
},
"identifier": {
"type": "array",
"items": {
"type": "string"
},
"title": "identifier"
},
"reference": {
"allOf": [
{
"$ref": "#/definitions/Stuff"
},
{
"title": "reference"
}
]
},
"bool": {
"type": "boolean",
"title": "bool"
},
"union": {
"allOf": [
{
"$ref": "#/definitions/MyUnion"
},
{
"title": "union"
}
]
},
"with_params": {
"type": "integer",
"title": "with_params"
}
},
"required": [
"identifier",
"reference",
"bool"
]
},
"InputType": {
"title": "InputType",
"type": "object",
"properties": {
"an_int": {
"type": "integer",
"title": "an_int"
},
"a_string": {
"type": "string",
"title": "a_string"
}
},
"required": [
"an_int"
]
}
}
}
32 changes: 32 additions & 0 deletions spec/transformSpec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const transform = require('../index.js');
const fs = require('fs');
const path = require('path');
const Ajv = require('ajv');

const ajv = new Ajv();
const mockJSONSchema = require(path.join(__dirname, 'data/mock_schema.json'));
const mockJSONSchemaStrict = require(path.join(__dirname, 'data/mock_schema_strict.json'));
const mockGraphQL = fs.readFileSync(path.join(__dirname, 'data/mock_schema.graphql'), { encoding: 'utf-8' });

describe('GraphQL to JSON Schema transform', () => {
Expand All @@ -21,4 +24,33 @@ describe('GraphQL to JSON Schema transform', () => {
it('parses a test GraphQL Schema properly', () => {
expect(transform(mockGraphQL)).toEqual(mockJSONSchema);
});

it('return a valid JSON Schema definition', () => {
const schema = transform(`
type Stuff {
my_field: Int
req_field: String!
recursion: MoreStuff
custom_scalar: Foo
enum: MyEnum
}
`);
const valid = ajv.validateSchema(schema);
expect(valid).toBe(true);
if (!valid) {
console.log(ajv.errors)
}
});

describe('with strict mode', () => {
it('parses a test GraphQL Schema properly', () => {
const schema = transform(mockGraphQL, true);
expect(schema).toEqual(mockJSONSchemaStrict);
const valid = ajv.validateSchema(schema);
expect(valid).toBe(true);
if (!valid) {
console.log(ajv.errors)
}
});
})
})
Loading