Merge pull request #1345 from kamalbennani/bug/graphql-do-not-formatted-values-of-type-object

Correctly determine the numeric value candidates in GraphQL
This commit is contained in:
Jim LAURIE 2018-06-19 16:44:44 +02:00 committed by GitHub
commit 96a2e8e4a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 11 deletions

View File

@ -157,10 +157,7 @@ module.exports = {
let type = 'String';
switch (definition.type) {
case 'string':
case 'text':
type = 'String';
break;
// TODO: Handle fields of type Array, Perhaps default to [Int] or [String] ...
case 'boolean':
type = 'Boolean';
break;
@ -567,7 +564,10 @@ module.exports = {
const entry = withRelated && withRelated.toJSON ? withRelated.toJSON() : withRelated;
entry[association.alias]._type = _.upperFirst(association.model);
// Set the _type only when the value is defined
if (entry[association.alias]) {
entry[association.alias]._type = _.upperFirst(association.model);
}
return entry[association.alias];
}
@ -651,8 +651,8 @@ module.exports = {
[ref.primaryKey]: arrayOfIds,
...where.where
}).where;
break;
}
break;
// falls through
}
default:

View File

@ -10,6 +10,11 @@ const path = require('path');
// Public node modules.
const _ = require('lodash');
// Following this discussion https://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric this function is the best implem to determine if a value is a valid number candidate
const isNumeric = (value) => {
return !isNaN(parseFloat(value)) && isFinite(value);
}
/* eslint-disable prefer-template */
/*
* Set of utils for models
@ -453,11 +458,10 @@ module.exports = {
let result;
let formattedValue;
try {
formattedValue = !_.isNaN(_.toNumber(value)) ? _.toNumber(value) : value;
} catch(err) {
formattedValue = value;
}
// Check if the value if a valid candidate to be converted to a number value
formattedValue = isNumeric(value)
? _.toNumber(value)
: value;
if (_.includes(['_start', '_limit'], key)) {
result = convertor(formattedValue, key);