2021-08-05 15:57:31 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { unionType } = require('nexus');
|
|
|
|
const { prop } = require('lodash/fp');
|
|
|
|
|
|
|
|
const {
|
2021-08-06 16:16:07 +02:00
|
|
|
utils,
|
2021-08-16 15:21:00 +02:00
|
|
|
constants: { GENERIC_MORPH_TYPENAME, KINDS },
|
2021-08-24 12:10:47 +02:00
|
|
|
} = require('../types');
|
2021-08-05 15:57:31 +02:00
|
|
|
|
2021-08-06 16:16:07 +02:00
|
|
|
module.exports = ({ strapi, registry }) => ({
|
2021-08-05 15:57:31 +02:00
|
|
|
buildGenericMorphDefinition() {
|
|
|
|
return unionType({
|
|
|
|
name: GENERIC_MORPH_TYPENAME,
|
|
|
|
|
2021-08-06 16:16:07 +02:00
|
|
|
resolveType(obj) {
|
|
|
|
const contentType = strapi.getModel(obj.__type);
|
|
|
|
|
|
|
|
if (!contentType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contentType.modelType === 'component') {
|
|
|
|
return utils.getComponentName(contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
return utils.getTypeName(contentType);
|
|
|
|
},
|
2021-08-05 15:57:31 +02:00
|
|
|
|
|
|
|
definition(t) {
|
|
|
|
const members = registry
|
2021-08-16 15:21:00 +02:00
|
|
|
// Resolve every content-type or component
|
|
|
|
.where(({ config }) => [KINDS.type, KINDS.component].includes(config.kind))
|
|
|
|
// Only keep their name (the type's id)
|
2021-08-05 15:57:31 +02:00
|
|
|
.map(prop('name'));
|
|
|
|
|
|
|
|
t.members(...members);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|