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 },
|
|
|
|
} = require('@strapi/utils');
|
2021-07-05 10:43:36 +02:00
|
|
|
|
|
|
|
const {
|
2021-07-30 11:44:26 +02:00
|
|
|
mappers: { strapiScalarToGraphQLScalar, graphQLFiltersToStrapiQuery },
|
2021-07-05 10:43:36 +02:00
|
|
|
utils: { isScalar, getScalarFilterInputTypeName },
|
|
|
|
} = 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
|
|
|
|
const transformArgs = (args, { contentType, usePagination = false }) => {
|
|
|
|
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-07-05 10:43:36 +02:00
|
|
|
module.exports = {
|
|
|
|
getUniqueScalarAttributes,
|
|
|
|
scalarAttributesToFiltersMap,
|
2021-07-30 11:44:26 +02:00
|
|
|
transformArgs,
|
2021-07-05 10:43:36 +02:00
|
|
|
};
|