Correctly detect an enum field

This commit is contained in:
Kamal Bennani 2018-06-26 18:23:18 +02:00
parent aedf09bdcb
commit 25eab80f64
No known key found for this signature in database
GPG Key ID: 4513063CDB1A1C25

View File

@ -29,15 +29,18 @@ module.exports = {
type === 'Float' || type === 'Float' ||
type === 'String' || type === 'String' ||
type === 'Boolean' || type === 'Boolean' ||
type === 'DateTime' type === 'DateTime' ||
type === 'JSON'
); );
}, },
/** /**
* Checks if the field is of type enum * Checks if the field is of type enum
*
* @returns {Boolean}
*/ */
isEnumType: (_type) => { isEnumType: (type) => {
return _.startsWith(_type, 'ENUM_'); return type === 'enumeration';
}, },
/** /**
@ -65,19 +68,25 @@ module.exports = {
/** /**
* Convert non-primitive type to string (non-primitive types corresponds to a reference to an other model) * Convert non-primitive type to string (non-primitive types corresponds to a reference to an other model)
* *
* @returns {String}
*
* @example
*
* extractType(String!) * extractType(String!)
* // => String * // => String
* *
* extractType(user) * extractType(user)
* // => ID * // => ID
* *
* @returns {String} * extractType(ENUM_TEST_FIELD, enumeration)
* // => String
*
*/ */
extractType: function (_type) { extractType: function (_type, attributeType) {
return this.isPrimitiveType(_type) return this.isPrimitiveType(_type)
? _type.replace('!', '') ? _type.replace('!', '')
: this.isEnumType(_type) : this.isEnumType(attributeType)
? 'String' ? 'String'
: 'ID'; : 'ID';
}, },
@ -96,7 +105,7 @@ module.exports = {
/** /**
* Use the field resolver otherwise fall through the field value * Use the field resolver otherwise fall through the field value
* *
* @returns {function} * @returns {function}
*/ */
fieldResolver: (field, key) => { fieldResolver: (field, key) => {
@ -110,7 +119,7 @@ module.exports = {
/** /**
* Create fields resolvers * Create fields resolvers
* *
* @return {Object} * @return {Object}
*/ */
createFieldsResolver: function(fields, resolver, typeCheck) { createFieldsResolver: function(fields, resolver, typeCheck) {
@ -142,22 +151,22 @@ module.exports = {
/** /**
* Create the resolvers for each aggregation field * Create the resolvers for each aggregation field
* *
* @return {Object} * @return {Object}
* *
* @example * @example
* *
* const model = // Strapi model * const model = // Strapi model
* *
* const fields = { * const fields = {
* username: String, * username: String,
* age: Int, * age: Int,
* } * }
* *
* const typeCheck = (type) => type === 'Int' || type === 'Float', * const typeCheck = (type) => type === 'Int' || type === 'Float',
* *
* const fieldsResoler = createAggregationFieldsResolver(model, fields, 'sum', typeCheck); * const fieldsResoler = createAggregationFieldsResolver(model, fields, 'sum', typeCheck);
* *
* // => { * // => {
* age: function ageResolver() { .... } * age: function ageResolver() { .... }
* } * }
@ -257,7 +266,7 @@ module.exports = {
* type UserAggregateAvg { * type UserAggregateAvg {
* age: Float * age: Float
* } * }
* *
* type UserGroupBy { * type UserGroupBy {
* username: [UserConnectionUsername] * username: [UserConnectionUsername]
* age: [UserConnectionAge] * age: [UserConnectionAge]
@ -336,12 +345,11 @@ module.exports = {
* @return {String} * @return {String}
*/ */
generateConnectionFieldsTypes: function (fields, model) { generateConnectionFieldsTypes: function (fields, model) {
const { globalId } = model; const { globalId, attributes } = model;
const primitiveFields = this.getFieldsByTypes( const primitiveFields = this.getFieldsByTypes(
fields, fields,
this.isNotOfTypeArray, this.isNotOfTypeArray,
type => this.extractType(type), (type, name) => this.extractType(type, (attributes[name] || {}).type),
); );
const connectionFields = _.mapValues(primitiveFields, (fieldType) => ({ const connectionFields = _.mapValues(primitiveFields, (fieldType) => ({