2020-01-27 10:30:52 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
2020-10-15 13:22:40 +02:00
|
|
|
const { QUERY_OPERATORS } = require('strapi-utils');
|
2020-01-27 10:30:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-02-10 18:48:57 +01:00
|
|
|
|
|
|
|
return root;
|
2020-01-27 10:30:52 +01:00
|
|
|
};
|
|
|
|
|
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 => {
|
2020-03-30 15:28:00 +02:00
|
|
|
if (type === 'Query' || type === 'Mutation') {
|
2020-03-18 11:55:06 +01:00
|
|
|
if (!_.has(base, [type, resolver])) {
|
|
|
|
_.set(newObj, [type, resolver], _.get(object, [type, resolver]));
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-31 17:40:02 +01:00
|
|
|
_.set(newObj, [type, resolver], _.get(object, [type, resolver]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-18 11:57:04 +01:00
|
|
|
return newObj;
|
2020-01-31 17:40:02 +01:00
|
|
|
};
|
|
|
|
|
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) => {
|
2020-10-15 13:22:40 +02:00
|
|
|
if (QUERY_OPERATORS.includes(key)) {
|
|
|
|
result[key] = _.isArray(value) ? value.map(convertToQuery) : convertToQuery(value);
|
|
|
|
} else if (_.isPlainObject(value)) {
|
2020-02-10 14:19:54 +01:00
|
|
|
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-03-30 15:44:20 +02:00
|
|
|
const nonRequired = type => type.replace('!', '');
|
2020-03-30 15:28:00 +02:00
|
|
|
|
2020-04-22 15:41:40 +02:00
|
|
|
const actionExists = ({ resolver, resolverOf }) => {
|
|
|
|
if (isResolvablePath(resolverOf)) {
|
|
|
|
return true;
|
|
|
|
} else if (_.isFunction(resolver)) {
|
|
|
|
return true;
|
|
|
|
} else if (_.isString(resolver)) {
|
|
|
|
return _.isFunction(getActionFn(getActionDetails(resolver)));
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`Error building query. Expected \`resolver\` as string or a function, or \`resolverOf\` as a string. got ${{
|
|
|
|
resolver,
|
|
|
|
resolverOf,
|
|
|
|
}}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getAction = resolver => {
|
|
|
|
if (!_.isString(resolver)) {
|
|
|
|
throw new Error(`Error building query. Expected a string, got ${resolver}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const actionDetails = getActionDetails(resolver);
|
|
|
|
const actionFn = getActionFn(actionDetails);
|
|
|
|
|
|
|
|
if (!actionFn) {
|
|
|
|
throw new Error(
|
|
|
|
`[GraphQL] Cannot find action "${resolver}". Check your graphql configurations.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return actionFn;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getActionFn = details => {
|
|
|
|
const { controller, action, plugin, api } = details;
|
|
|
|
|
|
|
|
if (plugin) {
|
|
|
|
return _.get(strapi.plugins, [_.toLower(plugin), 'controllers', _.toLower(controller), action]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.get(strapi.api, [_.toLower(api), 'controllers', _.toLower(controller), action]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getActionDetails = resolver => {
|
|
|
|
if (resolver.startsWith('plugins::')) {
|
|
|
|
const [, path] = resolver.split('::');
|
|
|
|
const [plugin, controller, action] = path.split('.');
|
|
|
|
|
|
|
|
return { plugin, controller, action };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolver.startsWith('application::')) {
|
|
|
|
const [, path] = resolver.split('::');
|
|
|
|
const [api, controller, action] = path.split('.');
|
|
|
|
|
|
|
|
return { api, controller, action };
|
|
|
|
}
|
|
|
|
|
|
|
|
const args = resolver.split('.');
|
|
|
|
|
|
|
|
if (args.length === 3) {
|
|
|
|
const [api, controller, action] = args;
|
|
|
|
return { api, controller, action };
|
|
|
|
}
|
|
|
|
|
|
|
|
// if direct api access
|
|
|
|
if (args.length === 2) {
|
|
|
|
const [controller, action] = args;
|
|
|
|
return { api: controller, controller, action };
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(
|
|
|
|
`[GraphQL] Could not find action for resolver "${resolver}". Check your graphql configurations.`
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const isResolvablePath = path => _.isString(path) && !_.isEmpty(path);
|
|
|
|
|
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-03-30 15:44:20 +02:00
|
|
|
nonRequired,
|
2020-04-22 15:41:40 +02:00
|
|
|
actionExists,
|
|
|
|
getAction,
|
|
|
|
getActionDetails,
|
|
|
|
getActionFn,
|
|
|
|
isResolvablePath,
|
2020-01-27 10:30:52 +01:00
|
|
|
};
|