| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { inputObjectType, nonNull } = require('nexus'); | 
					
						
							| 
									
										
										
										
											2021-11-18 16:36:29 +01:00
										 |  |  | const { | 
					
						
							| 
									
										
										
										
											2021-11-18 17:15:33 +01:00
										 |  |  |   contentTypes: { isWritableAttribute }, | 
					
						
							| 
									
										
										
										
											2021-11-18 16:36:29 +01:00
										 |  |  | } = require('@strapi/utils'); | 
					
						
							| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | module.exports = (context) => { | 
					
						
							| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  |   const { strapi } = context; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  |   const { naming, mappers, attributes } = strapi.plugin('graphql').service('utils'); | 
					
						
							| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  |   const extension = strapi.plugin('graphql').service('extension'); | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |   const { getComponentInputName, getContentTypeInputName, getEnumName, getDynamicZoneInputName } = | 
					
						
							|  |  |  |     naming; | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     isStrapiScalar, | 
					
						
							|  |  |  |     isRelation, | 
					
						
							|  |  |  |     isMorphRelation, | 
					
						
							|  |  |  |     isMedia, | 
					
						
							|  |  |  |     isEnumeration, | 
					
						
							|  |  |  |     isComponent, | 
					
						
							|  |  |  |     isDynamicZone, | 
					
						
							|  |  |  |   } = attributes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  |   return { | 
					
						
							|  |  |  |     buildInputType(contentType) { | 
					
						
							|  |  |  |       const { attributes, modelType } = contentType; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |       const name = ( | 
					
						
							|  |  |  |         modelType === 'component' ? getComponentInputName : getContentTypeInputName | 
					
						
							| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  |       ).call(null, contentType); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return inputObjectType({ | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         definition(t) { | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |           const isFieldEnabled = (fieldName) => { | 
					
						
							|  |  |  |             return extension.shadowCRUD(contentType.uid).field(fieldName).hasInputEnabled(); | 
					
						
							| 
									
										
										
										
											2021-09-28 14:59:14 +02:00
										 |  |  |           }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:15:33 +01:00
										 |  |  |           const validAttributes = Object.entries(attributes).filter(([attributeName]) => { | 
					
						
							|  |  |  |             return isWritableAttribute(contentType, attributeName) && isFieldEnabled(attributeName); | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2021-09-22 16:52:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 14:59:14 +02:00
										 |  |  |           // Add the ID for the component to enable inplace updates
 | 
					
						
							|  |  |  |           if (modelType === 'component' && isFieldEnabled('id')) { | 
					
						
							|  |  |  |             t.id('id'); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 17:17:24 +02:00
										 |  |  |           validAttributes.forEach(([attributeName, attribute]) => { | 
					
						
							| 
									
										
										
										
											2021-12-23 10:59:24 +09:00
										 |  |  |             // Enums
 | 
					
						
							|  |  |  |             if (isEnumeration(attribute)) { | 
					
						
							|  |  |  |               const enumTypeName = getEnumName(contentType, attributeName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               t.field(attributeName, { type: enumTypeName }); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  |             // Scalars
 | 
					
						
							| 
									
										
										
										
											2021-12-23 10:59:24 +09:00
										 |  |  |             else 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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  |               if (extension.shadowCRUD('plugin::upload.file').isDisabled()) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |               if (isMultiple) { | 
					
						
							|  |  |  |                 t.list.id(attributeName); | 
					
						
							|  |  |  |               } else { | 
					
						
							|  |  |  |                 t.id(attributeName); | 
					
						
							|  |  |  |               } | 
					
						
							| 
									
										
										
										
											2021-08-06 16:16:07 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Regular Relations (ignore polymorphic relations)
 | 
					
						
							|  |  |  |             else if (isRelation(attribute) && !isMorphRelation(attribute)) { | 
					
						
							| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  |               if (extension.shadowCRUD(attribute.target).isDisabled()) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:16:07 +02:00
										 |  |  |               const isToManyRelation = attribute.relation.endsWith('Many'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |               if (isToManyRelation) { | 
					
						
							|  |  |  |                 t.list.id(attributeName); | 
					
						
							|  |  |  |               } else { | 
					
						
							|  |  |  |                 t.id(attributeName); | 
					
						
							|  |  |  |               } | 
					
						
							| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // 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) }); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-09-27 17:17:24 +02:00
										 |  |  |           }); | 
					
						
							| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; |