Skip to content

Commit

Permalink
fix(getEnumType): Bypass graphql 0.9.4 block for true/false values
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Apr 28, 2017
1 parent 7d58b89 commit a756939
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/ElasticApiParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ export default class ElasticApiParser {
case 'list':
return GraphQLJSON;
case 'enum':
// $FlowFixMe
if (Array.isArray(paramCfg.options)) {
return this.getEnumType(fieldName, paramCfg.options);
}
Expand All @@ -380,7 +379,7 @@ export default class ElasticApiParser {
}
}

getEnumType(fieldName: string, vals: string[]): GraphQLEnumType {
getEnumType(fieldName: string, vals: mixed[]): GraphQLEnumType {
const key = fieldName;
const subKey = JSON.stringify(vals);

Expand All @@ -393,9 +392,20 @@ export default class ElasticApiParser {
(result, val) => {
if (val === '') {
result.empty_string = { value: '' };
} else if (val === 'true') {
result.true_string = { value: 'true' };
} else if (val === true) {
result.true_boolean = { value: true };
} else if (val === 'false') {
result.false_string = { value: 'false' };
} else if (val === false) {
result.false_boolean = { value: false };
} else if (val === 'null') {
result.null_string = { value: 'null' };
} else if (Number.isFinite(val)) {
// $FlowFixMe
result[`number_${val}`] = { value: val };
} else {
} else if (typeof val === 'string') {
result[val] = { value: val };
}
return result;
Expand Down
25 changes: 24 additions & 1 deletion src/__tests__/ElasticApiParser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,31 @@ describe('ElasticApiParser', () => {
});
});

it('should convert `true` to `true_value` (same for `false`)', () => {
// This is fix for https://github.com/graphql/graphql-js/pull/812
// Which throw error on `true`, `false` and `null`.
// But we allow to use this values, just renaming it.
const type = parser.getEnumType('f1', ['true', true, 'false', false]);
expect(type).toBeInstanceOf(GraphQLEnumType);
expect(type._values[0]).toMatchObject({
name: 'true_string',
value: 'true',
});
expect(type._values[1]).toMatchObject({
name: 'true_boolean',
value: true,
});
expect(type._values[2]).toMatchObject({
name: 'false_string',
value: 'false',
});
expect(type._values[3]).toMatchObject({
name: 'false_boolean',
value: false,
});
});

it('should convert 1 to number_1', () => {
// $FlowFixMe
const type = parser.getEnumType('f1', [1]);
expect(type).toBeInstanceOf(GraphQLEnumType);
expect(type._values[0]).toMatchObject({ name: 'number_1', value: 1 });
Expand Down

0 comments on commit a756939

Please sign in to comment.