Skip to content

Commit bfce429

Browse files
authored
Merge pull request #2 from jakubfiala/1-handle-input
Handle Inputs
2 parents f74e600 + c666b66 commit bfce429

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

spec/data/mock_schema.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ type MoreStuff {
2424
union: MyUnion
2525
with_params(param1: Int, param2: [Float]): Int
2626
}
27+
28+
input InputType {
29+
an_int: Int!
30+
a_string: String
31+
}

spec/data/mock_schema.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,26 @@
169169
"identifier",
170170
"bool"
171171
]
172+
},
173+
"InputType": {
174+
"title": "InputType",
175+
"type": "object",
176+
"input": true,
177+
"properties": {
178+
"an_int": {
179+
"type": "integer",
180+
"required": true,
181+
"title": "an_int"
182+
},
183+
"a_string": {
184+
"type": "string",
185+
"required": false,
186+
"title": "a_string"
187+
}
188+
},
189+
"required": [
190+
"an_int"
191+
]
172192
}
173193
}
174194
}

spec/transformSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ describe('GraphQL to JSON Schema transform', () => {
1919
});
2020

2121
it('parses a test GraphQL Schema properly', () => {
22-
expect(mockJSONSchema).toEqual(transform(mockGraphQL));
22+
expect(transform(mockGraphQL)).toEqual(mockJSONSchema);
2323
});
2424
})

transform.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ const getPropertyType = type => {
4141
}
4242
}
4343

44+
/**
45+
* converts the GQL arguments array into a plain JSON schema array
46+
*
47+
* @param {Array} _arguments The GQL arguments
48+
* @return {Object} a plain JSON array
49+
*/
50+
const toFieldArguments = _arguments => {
51+
return _arguments.map(a => {
52+
return {
53+
title: a.name.value,
54+
type: getPropertyType(a.type),
55+
defaultValue: a.defaultValue
56+
};
57+
});
58+
}
59+
4460
/**
4561
* maps a GQL type field onto a JSON Schema property
4662
*
@@ -52,16 +68,11 @@ const toSchemaProperty = field => {
5268

5369
if ('$ref' in propertyType) propertyType = { allOf: [propertyType, { title: field.name.value }] };
5470

55-
return Object.assign(propertyType, {
56-
title: field.name.value,
57-
arguments: field.arguments.map(a => {
58-
return {
59-
title: a.name.value,
60-
type: getPropertyType(a.type),
61-
defaultValue: a.defaultValue
62-
};
63-
})
64-
});
71+
return Object.assign(
72+
propertyType,
73+
{ title: field.name.value },
74+
field.arguments ? { arguments: toFieldArguments(field.arguments) } : {}
75+
);
6576
}
6677

6778
/**
@@ -101,12 +112,18 @@ const toSchemaObject = definition => {
101112
.filter(f => f.required)
102113
.map(f => f.title);
103114

104-
return {
115+
let schemaObject = {
105116
title: definition.name.value,
106117
type: 'object',
107118
properties,
108-
required
119+
required,
120+
};
121+
122+
if (definition.kind === 'InputObjectTypeDefinition') {
123+
Object.assign(schemaObject, { input: true });
109124
}
125+
126+
return schemaObject;
110127
}
111128

112129
/**

0 commit comments

Comments
 (0)