Skip to content

Commit

Permalink
Merge pull request #2 from jakubfiala/1-handle-input
Browse files Browse the repository at this point in the history
Handle Inputs
  • Loading branch information
jakubfiala authored Jan 21, 2018
2 parents f74e600 + c666b66 commit bfce429
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
5 changes: 5 additions & 0 deletions spec/data/mock_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ type MoreStuff {
union: MyUnion
with_params(param1: Int, param2: [Float]): Int
}

input InputType {
an_int: Int!
a_string: String
}
20 changes: 20 additions & 0 deletions spec/data/mock_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@
"identifier",
"bool"
]
},
"InputType": {
"title": "InputType",
"type": "object",
"input": true,
"properties": {
"an_int": {
"type": "integer",
"required": true,
"title": "an_int"
},
"a_string": {
"type": "string",
"required": false,
"title": "a_string"
}
},
"required": [
"an_int"
]
}
}
}
2 changes: 1 addition & 1 deletion spec/transformSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ describe('GraphQL to JSON Schema transform', () => {
});

it('parses a test GraphQL Schema properly', () => {
expect(mockJSONSchema).toEqual(transform(mockGraphQL));
expect(transform(mockGraphQL)).toEqual(mockJSONSchema);
});
})
41 changes: 29 additions & 12 deletions transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ const getPropertyType = type => {
}
}

/**
* converts the GQL arguments array into a plain JSON schema array
*
* @param {Array} _arguments The GQL arguments
* @return {Object} a plain JSON array
*/
const toFieldArguments = _arguments => {
return _arguments.map(a => {
return {
title: a.name.value,
type: getPropertyType(a.type),
defaultValue: a.defaultValue
};
});
}

/**
* maps a GQL type field onto a JSON Schema property
*
Expand All @@ -52,16 +68,11 @@ const toSchemaProperty = field => {

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

return Object.assign(propertyType, {
title: field.name.value,
arguments: field.arguments.map(a => {
return {
title: a.name.value,
type: getPropertyType(a.type),
defaultValue: a.defaultValue
};
})
});
return Object.assign(
propertyType,
{ title: field.name.value },
field.arguments ? { arguments: toFieldArguments(field.arguments) } : {}
);
}

/**
Expand Down Expand Up @@ -101,12 +112,18 @@ const toSchemaObject = definition => {
.filter(f => f.required)
.map(f => f.title);

return {
let schemaObject = {
title: definition.name.value,
type: 'object',
properties,
required
required,
};

if (definition.kind === 'InputObjectTypeDefinition') {
Object.assign(schemaObject, { input: true });
}

return schemaObject;
}

/**
Expand Down

0 comments on commit bfce429

Please sign in to comment.