2021-07-05 10:43:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { enumType } = require('nexus');
|
|
|
|
const { set } = require('lodash/fp');
|
2022-02-21 18:04:36 +01:00
|
|
|
const { toRegressedEnumValue } = require('@strapi/utils');
|
2021-07-05 10:43:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a Nexus enum type from a Strapi enum attribute
|
|
|
|
* @param {object} definition - The definition of the enum
|
|
|
|
* @param {string[]} definition.enum - The params of the enum
|
2021-09-23 17:25:25 +02:00
|
|
|
* @param {string} name - The name of the enum
|
2021-07-05 10:43:36 +02:00
|
|
|
* @return {NexusEnumTypeDef}
|
|
|
|
*/
|
2021-09-23 17:25:25 +02:00
|
|
|
const buildEnumTypeDefinition = (definition, name) => {
|
2021-07-05 10:43:36 +02:00
|
|
|
return enumType({
|
|
|
|
name,
|
2022-02-21 18:04:36 +01:00
|
|
|
members: definition.enum.reduce(
|
|
|
|
(acc, value) => set(toRegressedEnumValue(value), value, acc),
|
|
|
|
{}
|
|
|
|
),
|
2021-07-05 10:43:36 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = () => ({
|
|
|
|
buildEnumTypeDefinition,
|
|
|
|
});
|