diff --git a/packages/strapi-plugin-graphql/services/GraphQL.js b/packages/strapi-plugin-graphql/services/GraphQL.js index 0e43ea9674..f35fdab0d2 100644 --- a/packages/strapi-plugin-graphql/services/GraphQL.js +++ b/packages/strapi-plugin-graphql/services/GraphQL.js @@ -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: diff --git a/packages/strapi-utils/lib/models.js b/packages/strapi-utils/lib/models.js index ab3f12aff2..4cc6981971 100755 --- a/packages/strapi-utils/lib/models.js +++ b/packages/strapi-utils/lib/models.js @@ -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);