| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-30 11:44:26 +02:00
										 |  |  | const { entries, mapValues, omit } = require('lodash/fp'); | 
					
						
							|  |  |  | const { | 
					
						
							|  |  |  |   pagination: { withDefaultPagination }, | 
					
						
							| 
									
										
										
										
											2021-08-11 16:26:33 +02:00
										 |  |  |   contentTypes: { hasDraftAndPublish }, | 
					
						
							| 
									
										
										
										
											2021-07-30 11:44:26 +02:00
										 |  |  | } = require('@strapi/utils'); | 
					
						
							| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  | module.exports = ({ strapi }) => { | 
					
						
							| 
									
										
										
										
											2021-09-07 11:23:49 +02:00
										 |  |  |   const { service: getService } = strapi.plugin('graphql'); | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get every args for a given content type | 
					
						
							|  |  |  |      * @param {object} contentType | 
					
						
							|  |  |  |      * @param {object} options | 
					
						
							|  |  |  |      * @param {boolean} options.multiple | 
					
						
							|  |  |  |      * @return {object} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     getContentTypeArgs(contentType, { multiple = true } = {}) { | 
					
						
							| 
									
										
										
										
											2021-09-07 11:23:49 +02:00
										 |  |  |       const { naming } = getService('utils'); | 
					
						
							|  |  |  |       const { args } = getService('internals'); | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       const { kind, modelType } = contentType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Components
 | 
					
						
							|  |  |  |       if (modelType === 'component') { | 
					
						
							| 
									
										
										
										
											2021-11-15 18:12:06 +01:00
										 |  |  |         if (!multiple) return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  |         return { | 
					
						
							|  |  |  |           filters: naming.getFiltersInputTypeName(contentType), | 
					
						
							| 
									
										
										
										
											2021-09-01 12:06:51 +02:00
										 |  |  |           pagination: args.PaginationArg, | 
					
						
							|  |  |  |           sort: args.SortArg, | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  |         }; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Collection Types
 | 
					
						
							|  |  |  |       else if (kind === 'collectionType') { | 
					
						
							|  |  |  |         if (!multiple) { | 
					
						
							|  |  |  |           return { id: 'ID' }; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const params = { | 
					
						
							| 
									
										
										
										
											2021-09-01 12:06:51 +02:00
										 |  |  |           filters: naming.getFiltersInputTypeName(contentType), | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  |           pagination: args.PaginationArg, | 
					
						
							|  |  |  |           sort: args.SortArg, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (hasDraftAndPublish(contentType)) { | 
					
						
							|  |  |  |           Object.assign(params, { publicationState: args.PublicationStateArg }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return params; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Single Types
 | 
					
						
							|  |  |  |       else if (kind === 'singleType') { | 
					
						
							| 
									
										
										
										
											2021-09-09 11:31:29 +02:00
										 |  |  |         const params = {}; | 
					
						
							| 
									
										
										
										
											2021-09-01 12:06:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (hasDraftAndPublish(contentType)) { | 
					
						
							|  |  |  |           Object.assign(params, { publicationState: args.PublicationStateArg }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return params; | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Filter an object entries and keep only those whose value is a unique scalar attribute | 
					
						
							|  |  |  |      * @param {object} attributes | 
					
						
							|  |  |  |      * @return {Object<string, object>} | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-09-22 15:15:03 +02:00
										 |  |  |     getUniqueScalarAttributes(attributes) { | 
					
						
							| 
									
										
										
										
											2021-09-07 11:23:49 +02:00
										 |  |  |       const { isStrapiScalar } = getService('utils').attributes; | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       const uniqueAttributes = entries(attributes).filter( | 
					
						
							|  |  |  |         ([, attribute]) => isStrapiScalar(attribute) && attribute.unique | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return Object.fromEntries(uniqueAttributes); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Map each value from an attribute to a FiltersInput type name | 
					
						
							|  |  |  |      * @param {object} attributes - The attributes object to transform | 
					
						
							|  |  |  |      * @return {Object<string, string>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     scalarAttributesToFiltersMap: mapValues(attribute => { | 
					
						
							| 
									
										
										
										
											2021-09-07 11:23:49 +02:00
										 |  |  |       const { mappers, naming } = getService('utils'); | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       const gqlScalar = mappers.strapiScalarToGraphQLScalar(attribute.type); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return naming.getScalarFilterInputTypeName(gqlScalar); | 
					
						
							|  |  |  |     }), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Apply basic transform to GQL args | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     transformArgs(args, { contentType, usePagination = false } = {}) { | 
					
						
							| 
									
										
										
										
											2021-11-12 10:07:08 +01:00
										 |  |  |       const { mappers } = getService('utils'); | 
					
						
							|  |  |  |       const { config } = strapi.plugin('graphql'); | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  |       const { pagination = {}, filters = {} } = args; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Init
 | 
					
						
							|  |  |  |       const newArgs = omit(['pagination', 'filters'], args); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Pagination
 | 
					
						
							|  |  |  |       if (usePagination) { | 
					
						
							| 
									
										
										
										
											2021-11-12 10:07:08 +01:00
										 |  |  |         const defaultLimit = config('defaultLimit'); | 
					
						
							|  |  |  |         const maxLimit = config('maxLimit'); | 
					
						
							| 
									
										
										
										
											2021-09-07 11:23:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  |         Object.assign( | 
					
						
							|  |  |  |           newArgs, | 
					
						
							| 
									
										
										
										
											2021-09-07 11:23:49 +02:00
										 |  |  |           withDefaultPagination(pagination, { | 
					
						
							|  |  |  |             maxLimit, | 
					
						
							|  |  |  |             defaults: { | 
					
						
							|  |  |  |               offset: { limit: defaultLimit }, | 
					
						
							|  |  |  |               page: { pageSize: defaultLimit }, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }) | 
					
						
							| 
									
										
										
										
											2021-08-24 17:56:44 +02:00
										 |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Filters
 | 
					
						
							|  |  |  |       if (args.filters) { | 
					
						
							|  |  |  |         Object.assign(newArgs, { | 
					
						
							|  |  |  |           filters: mappers.graphQLFiltersToStrapiQuery(filters, contentType), | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return newArgs; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2021-07-05 10:43:36 +02:00
										 |  |  | }; |