2021-07-05 10:43:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { inputObjectType, nonNull } = require('nexus');
|
|
|
|
|
|
|
|
module.exports = context => {
|
|
|
|
const { strapi } = context;
|
|
|
|
|
2021-08-24 17:56:44 +02:00
|
|
|
const { naming, mappers, attributes } = strapi.plugin('graphql').service('utils');
|
|
|
|
|
|
|
|
const {
|
|
|
|
getComponentInputName,
|
|
|
|
getContentTypeInputName,
|
|
|
|
getEnumName,
|
|
|
|
getDynamicZoneInputName,
|
|
|
|
} = naming;
|
|
|
|
|
|
|
|
const {
|
|
|
|
isStrapiScalar,
|
|
|
|
isRelation,
|
|
|
|
isMorphRelation,
|
|
|
|
isMedia,
|
|
|
|
isEnumeration,
|
|
|
|
isComponent,
|
|
|
|
isDynamicZone,
|
|
|
|
} = attributes;
|
|
|
|
|
2021-07-05 10:43:36 +02:00
|
|
|
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-08-18 14:56:46 +02:00
|
|
|
if (isStrapiScalar(attribute)) {
|
2021-07-05 10:43:36 +02:00
|
|
|
const gqlScalar = mappers.strapiScalarToGraphQLScalar(attribute.type);
|
|
|
|
|
|
|
|
t.field(attributeName, { type: gqlScalar });
|
|
|
|
}
|
|
|
|
|
2021-08-06 16:16:07 +02:00
|
|
|
// Media
|
|
|
|
else if (isMedia(attribute)) {
|
|
|
|
const isMultiple = attribute.multiple === true;
|
|
|
|
|
|
|
|
isMultiple ? t.list.id(attributeName) : t.id(attributeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular Relations (ignore polymorphic relations)
|
|
|
|
else if (isRelation(attribute) && !isMorphRelation(attribute)) {
|
|
|
|
const isToManyRelation = attribute.relation.endsWith('Many');
|
|
|
|
|
|
|
|
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-08-06 16:16:07 +02:00
|
|
|
const isRepeatable = attribute.repeatable === true;
|
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
|
|
|
|
2021-08-06 16:16:07 +02:00
|
|
|
if (isRepeatable) {
|
2021-07-05 10:43:36 +02:00
|
|
|
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) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|