2021-07-05 10:43:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { inputObjectType, nonNull } = require('nexus');
|
|
|
|
|
|
|
|
const { utils, mappers } = require('../../types');
|
|
|
|
|
2021-07-30 11:44:26 +02:00
|
|
|
const {
|
|
|
|
getComponentInputName,
|
|
|
|
getContentTypeInputName,
|
|
|
|
getEnumName,
|
|
|
|
getDynamicZoneInputName,
|
|
|
|
isScalar,
|
|
|
|
isRelation,
|
|
|
|
isMorphRelation,
|
|
|
|
isMedia,
|
|
|
|
isEnumeration,
|
|
|
|
isComponent,
|
|
|
|
isDynamicZone,
|
|
|
|
} = utils;
|
|
|
|
|
2021-07-05 10:43:36 +02:00
|
|
|
module.exports = context => {
|
|
|
|
const { strapi } = context;
|
|
|
|
|
|
|
|
return {
|
|
|
|
buildInputType(contentType) {
|
|
|
|
const { attributes, modelType } = contentType;
|
|
|
|
|
|
|
|
const name = (modelType === 'component'
|
2021-07-30 11:44:26 +02:00
|
|
|
? getComponentInputName
|
|
|
|
: getContentTypeInputName
|
2021-07-05 10:43:36 +02:00
|
|
|
).call(null, contentType);
|
|
|
|
|
|
|
|
return inputObjectType({
|
|
|
|
name,
|
|
|
|
|
|
|
|
definition(t) {
|
|
|
|
for (const [attributeName, attribute] of Object.entries(attributes)) {
|
|
|
|
// Scalars
|
2021-07-30 11:44:26 +02:00
|
|
|
if (isScalar(attribute)) {
|
2021-07-05 10:43:36 +02:00
|
|
|
const gqlScalar = mappers.strapiScalarToGraphQLScalar(attribute.type);
|
|
|
|
|
|
|
|
t.field(attributeName, { type: gqlScalar });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relations
|
2021-07-30 11:44:26 +02:00
|
|
|
else if (isRelation(attribute) || isMorphRelation(attribute)) {
|
|
|
|
const isMultipleMedia = isMedia(attribute) && attribute.multiple;
|
|
|
|
const isToManyRelation = isRelation(attribute) && attribute.relation.endsWith('Many');
|
|
|
|
|
|
|
|
return isMultipleMedia || isToManyRelation
|
|
|
|
? t.list.id(attributeName)
|
|
|
|
: t.id(attributeName);
|
2021-07-05 10:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enums
|
2021-07-30 11:44:26 +02:00
|
|
|
else if (isEnumeration(attribute)) {
|
|
|
|
const enumTypeName = getEnumName(contentType, attributeName);
|
2021-07-05 10:43:36 +02:00
|
|
|
|
|
|
|
t.field(attributeName, { type: enumTypeName });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Components
|
2021-07-30 11:44:26 +02:00
|
|
|
else if (isComponent(attribute)) {
|
2021-07-05 10:43:36 +02:00
|
|
|
const component = strapi.components[attribute.component];
|
2021-07-30 11:44:26 +02:00
|
|
|
const componentInputType = getComponentInputName(component);
|
2021-07-05 10:43:36 +02:00
|
|
|
|
|
|
|
if (attribute.repeatable) {
|
|
|
|
t.list.field(attributeName, { type: componentInputType });
|
|
|
|
} else {
|
|
|
|
t.field(attributeName, { type: componentInputType });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dynamic Zones
|
2021-07-30 11:44:26 +02:00
|
|
|
else if (isDynamicZone(attribute)) {
|
|
|
|
const dzInputName = getDynamicZoneInputName(contentType, attributeName);
|
2021-07-05 10:43:36 +02:00
|
|
|
|
|
|
|
t.list.field(attributeName, { type: nonNull(dzInputName) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|