2020-01-27 10:30:52 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges
|
|
|
|
*/
|
|
|
|
const mergeSchemas = (root, ...subs) => {
|
|
|
|
subs.forEach(sub => {
|
|
|
|
if (_.isEmpty(sub)) return;
|
|
|
|
const { definition = '', query = {}, mutation = {}, resolvers = {} } = sub;
|
|
|
|
|
|
|
|
root.definition += '\n' + definition;
|
|
|
|
_.merge(root, {
|
|
|
|
query,
|
|
|
|
mutation,
|
|
|
|
resolvers,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-01-27 22:43:44 +01:00
|
|
|
const createDefaultSchema = () => ({
|
|
|
|
definition: '',
|
2020-02-10 17:51:31 +01:00
|
|
|
query: {},
|
|
|
|
mutation: {},
|
2020-01-27 22:43:44 +01:00
|
|
|
resolvers: {},
|
|
|
|
});
|
|
|
|
|
2020-01-31 17:40:02 +01:00
|
|
|
const diffResolvers = (object, base) => {
|
|
|
|
let newObj = {};
|
|
|
|
|
|
|
|
Object.keys(object).forEach(type => {
|
|
|
|
Object.keys(object[type]).forEach(resolver => {
|
|
|
|
if (!_.has(base, [type, resolver])) {
|
|
|
|
_.set(newObj, [type, resolver], _.get(object, [type, resolver]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return newObj;
|
|
|
|
};
|
|
|
|
|
2020-02-10 14:19:54 +01:00
|
|
|
const convertToParams = params => {
|
|
|
|
return Object.keys(params).reduce((acc, current) => {
|
|
|
|
const key = current === 'id' ? 'id' : `_${current}`;
|
|
|
|
acc[key] = params[current];
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
|
|
|
const convertToQuery = params => {
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
_.forEach(params, (value, key) => {
|
|
|
|
if (_.isPlainObject(value)) {
|
|
|
|
const flatObject = convertToQuery(value);
|
|
|
|
_.forEach(flatObject, (_value, _key) => {
|
|
|
|
result[`${key}.${_key}`] = _value;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
result[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
const amountLimiting = (params = {}) => {
|
|
|
|
const { amountLimit } = strapi.plugins.graphql.config;
|
|
|
|
|
|
|
|
if (!amountLimit) return params;
|
|
|
|
|
|
|
|
if (!params.limit || params.limit === -1 || params.limit > amountLimit) {
|
|
|
|
params.limit = amountLimit;
|
|
|
|
} else if (params.limit < 0) {
|
|
|
|
params.limit = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
2020-01-27 10:30:52 +01:00
|
|
|
module.exports = {
|
2020-01-31 17:40:02 +01:00
|
|
|
diffResolvers,
|
2020-01-27 10:30:52 +01:00
|
|
|
mergeSchemas,
|
2020-01-27 22:43:44 +01:00
|
|
|
createDefaultSchema,
|
2020-02-10 14:19:54 +01:00
|
|
|
convertToParams,
|
|
|
|
convertToQuery,
|
|
|
|
amountLimiting,
|
2020-01-27 10:30:52 +01:00
|
|
|
};
|