mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 10:23:34 +00:00
feat(graphql): enable enum creation into schema from model
This commit is contained in:
parent
be4a45b5dd
commit
ce2f41a5be
@ -137,6 +137,14 @@ module.exports = {
|
||||
if (definition.type) {
|
||||
let type = 'String';
|
||||
|
||||
if (definition.type === 'enumeration') {
|
||||
if (definition.enumNamea) {
|
||||
type = definition.enumName;
|
||||
} else {
|
||||
type = `${definition.enum[0].toUpperCase()}_TYPE`;
|
||||
}
|
||||
}
|
||||
|
||||
switch (definition.type) {
|
||||
case 'string':
|
||||
case 'text':
|
||||
@ -180,6 +188,23 @@ module.exports = {
|
||||
return definition.model ? 'Morph' : '[Morph]';
|
||||
},
|
||||
|
||||
/**
|
||||
* Convert Strapi enum to GraphQL enum.
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
|
||||
convertEnum: (definition) => {
|
||||
let name = '';
|
||||
if (definition.enumName) {
|
||||
name = definition.enumName;
|
||||
} else {
|
||||
name = `${definition.enum[0].toUpperCase}_TYPE`;
|
||||
}
|
||||
|
||||
return `enum ${name} { ${definition.enum.join('\n')} }`;
|
||||
},
|
||||
|
||||
/**
|
||||
* Execute policies before the specified resolver.
|
||||
*
|
||||
@ -443,6 +468,19 @@ module.exports = {
|
||||
return acc;
|
||||
}, initialState);
|
||||
|
||||
// Detect enum and generate it for the schema definition
|
||||
const enums = Object.keys(model.attributes)
|
||||
.reduce((acc, attribute) => {
|
||||
const definition = model.attributes[attribute];
|
||||
|
||||
if (definition.type && definition.type === 'enumeration') {
|
||||
acc.push(this.convertEnum(definition));
|
||||
}
|
||||
return acc;
|
||||
}, []).join(' ');
|
||||
|
||||
acc.definition += `${enums}`;
|
||||
|
||||
// Add parameters to optimize association query.
|
||||
(model.associations || [])
|
||||
.filter(association => association.type === 'collection')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user