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
|
|
|
|
|
|
|
const {
|
2021-08-09 18:40:25 +02:00
|
|
|
args,
|
2021-07-30 11:44:26 +02:00
|
|
|
mappers: { strapiScalarToGraphQLScalar, graphQLFiltersToStrapiQuery },
|
2021-08-09 18:40:25 +02:00
|
|
|
utils: { isScalar, getScalarFilterInputTypeName, getFiltersInputTypeName },
|
2021-07-05 10:43:36 +02:00
|
|
|
} = require('../../types');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter an object entries and keep only those whose value is a unique scalar attribute
|
|
|
|
* @param {object} attributes
|
|
|
|
* @return {Object<string, object>}
|
|
|
|
*/
|
|
|
|
const getUniqueScalarAttributes = attributes => {
|
|
|
|
const uniqueAttributes = entries(attributes).filter(
|
|
|
|
([, attribute]) => isScalar(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>}
|
|
|
|
*/
|
|
|
|
const scalarAttributesToFiltersMap = mapValues(attribute => {
|
|
|
|
const gqlScalar = strapiScalarToGraphQLScalar(attribute.type);
|
|
|
|
|
|
|
|
return getScalarFilterInputTypeName(gqlScalar);
|
|
|
|
});
|
|
|
|
|
2021-07-30 11:44:26 +02:00
|
|
|
/**
|
|
|
|
* Apply basic transform to GQL args
|
|
|
|
*/
|
|
|
|
// todo[v4]: unify & move elsewhere
|
2021-08-09 18:40:25 +02:00
|
|
|
const transformArgs = (args, { contentType, usePagination = false } = {}) => {
|
2021-07-30 11:44:26 +02:00
|
|
|
const { pagination = {}, filters = {} } = args;
|
|
|
|
|
|
|
|
// Init
|
|
|
|
const newArgs = omit(['pagination', 'filters'], args);
|
|
|
|
|
|
|
|
// Pagination
|
|
|
|
if (usePagination) {
|
|
|
|
Object.assign(
|
|
|
|
newArgs,
|
|
|
|
withDefaultPagination(pagination /*, config.get(graphql.pagination.defaults)*/)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filters
|
|
|
|
if (args.filters) {
|
|
|
|
Object.assign(newArgs, { filters: graphQLFiltersToStrapiQuery(filters, contentType) });
|
|
|
|
}
|
|
|
|
|
|
|
|
return newArgs;
|
|
|
|
};
|
|
|
|
|
2021-08-09 18:40:25 +02:00
|
|
|
/**
|
|
|
|
* Get every args for a given content type
|
|
|
|
* @param {object} contentType
|
|
|
|
* @param {object} options
|
|
|
|
* @param {boolean} options.multiple
|
|
|
|
* @return {object}
|
|
|
|
*/
|
|
|
|
const getContentTypeArgs = (contentType, { multiple = true } = {}) => {
|
|
|
|
const { kind, modelType } = contentType;
|
|
|
|
|
|
|
|
// Components
|
|
|
|
if (modelType === 'component') {
|
|
|
|
return {
|
|
|
|
sort: args.SortArg,
|
|
|
|
pagination: args.PaginationArg,
|
|
|
|
filters: getFiltersInputTypeName(contentType),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collection Types
|
|
|
|
else if (kind === 'collectionType') {
|
2021-08-11 16:26:33 +02:00
|
|
|
// hasDraftAndPublish
|
|
|
|
|
|
|
|
if (!multiple) {
|
|
|
|
return { id: 'ID' };
|
|
|
|
}
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
pagination: args.PaginationArg,
|
|
|
|
sort: args.SortArg,
|
|
|
|
filters: getFiltersInputTypeName(contentType),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (hasDraftAndPublish(contentType)) {
|
|
|
|
Object.assign(params, { publicationState: args.PublicationStateArg });
|
|
|
|
}
|
|
|
|
|
|
|
|
return params;
|
2021-08-09 18:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Single Types
|
|
|
|
else if (kind === 'singleType') {
|
|
|
|
return {
|
|
|
|
id: 'ID',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-05 10:43:36 +02:00
|
|
|
module.exports = {
|
2021-08-09 18:40:25 +02:00
|
|
|
getContentTypeArgs,
|
2021-07-05 10:43:36 +02:00
|
|
|
getUniqueScalarAttributes,
|
|
|
|
scalarAttributesToFiltersMap,
|
2021-07-30 11:44:26 +02:00
|
|
|
transformArgs,
|
2021-07-05 10:43:36 +02:00
|
|
|
};
|