Skip to content

Commit

Permalink
Merge pull request #50 from overhead525/ISSUE-34-optional-types
Browse files Browse the repository at this point in the history
Issue 31 optional types
  • Loading branch information
mdlavin committed Mar 13, 2020
2 parents 1438086 + 429b657 commit fb473b5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,21 @@ function getReferenceName (referenceName, buildingInputType) {
}

function mapBasicAttributeType (type, attributeName) {
switch (type) {
let processedType;
if (type instanceof Array) {
if (type.length === 2) {
if (type.includes('null')) {
processedType = type.find(element => element !== 'null');
} else {
throw new Error('JSON Schema type attribute arrays should only be used to specify nullable type "[null, string]"');
}
} else {
throw new Error(`JSON Schema attribute type array can only have a max of 2 types/elements`);
}
} else {
processedType = type;
}
switch (processedType) {
case 'string': return GraphQLString;
case 'integer': return GraphQLInt;
case 'number': return GraphQLFloat;
Expand Down
74 changes: 74 additions & 0 deletions test/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,85 @@ test('boolean attributes', async function (test) {
await testAttrbuteType(test, 'boolean', 'Boolean');
});

test('null | string attributes', async function (test) {
await testAttrbuteType(test, ['null', 'string'], 'String');
});

test('null | integer attributes', async function (test) {
await testAttrbuteType(test, ['null', 'integer'], 'Int');
});

test('null | float attributes', async function (test) {
await testAttrbuteType(test, ['null', 'number'], 'Float');
});

test('null | boolean attributes', async function (test) {
await testAttrbuteType(test, ['null', 'boolean'], 'Boolean');
});

test('fail on unknown types attributes', async function (test) {
const assertion = testAttrbuteType(test, 'unknown', 'unknown', { skipValidation: true });
await test.throwsAsync(() => assertion, 'A JSON Schema attribute type unknown on attribute Simple.attribute does not have a known GraphQL mapping');
});

test('fail if array includes does not include "null"', async function (test) {
const assertion = testAttrbuteType(test, ['int', 'string'], 'String', { skipValidation: true });
await test.throwsAsync(() => assertion, 'JSON Schema type attribute arrays should only be used to specify nullable type "[null, string]"');
});

test('fail with more than 2 elements in type array', async function (test) {
const assertion = testAttrbuteType(test, ['null', 'string', 'int'], 'int', { skipValidation: true });
await test.throwsAsync(() => assertion, 'JSON Schema attribute type array can only have a max of 2 types/elements');
});

test('process attributes within array - variation --> String', async function (test) {
const optionalTypes = {
id: 'OptionalSchema',
type: 'object',
properties: {
attribute: {
type: ['null', 'string']
}
}
};

const expectedType = `
type OptionalSchema {
attribute: String
}
input OptionalSchema${INPUT_SUFFIX} {
attribute: String
}
`;

await testConversion(test, optionalTypes, 'OptionalSchema', expectedType);
});

test('process attributes within array - variation --> Float', async function (test) {
const optionalTypes = {
id: 'OptionalSchema',
type: 'object',
properties: {
attribute: {
type: ['null', 'number']
}
}
};

const expectedType = `
type OptionalSchema {
attribute: Float
}
input OptionalSchema${INPUT_SUFFIX} {
attribute: Float
}
`;

await testConversion(test, optionalTypes, 'OptionalSchema', expectedType);
});

test('array attributes', async function (test) {
const simpleType = {
id: 'Array',
Expand Down

0 comments on commit fb473b5

Please sign in to comment.