mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 23:24:03 +00:00
merge from develop
This commit is contained in:
commit
84ecec8140
@ -0,0 +1,3 @@
|
||||
{
|
||||
"amountLimit": 5
|
||||
}
|
||||
@ -91,7 +91,10 @@ function serverRestartWatcher(response) {
|
||||
'Keep-Alive': false,
|
||||
},
|
||||
})
|
||||
.then(() => {
|
||||
.then(res => {
|
||||
if (res.status >= 400) {
|
||||
throw new Error('not available');
|
||||
}
|
||||
// Hide the global OverlayBlocker
|
||||
strapi.unlockApp();
|
||||
resolve(response);
|
||||
|
||||
@ -49,10 +49,16 @@ module.exports = {
|
||||
*/
|
||||
|
||||
amountLimiting: (params = {}) => {
|
||||
if (params.limit && params.limit < 0) {
|
||||
const { amountLimit } = strapi.plugins.graphql.config;
|
||||
|
||||
if (!params.limit) return params;
|
||||
|
||||
if (params.limit === -1) {
|
||||
params.limit = amountLimit;
|
||||
} else if (params.limit < 0) {
|
||||
params.limit = 0;
|
||||
} else if (params.limit && params.limit > strapi.plugins.graphql.config.amountLimit) {
|
||||
params.limit = strapi.plugins.graphql.config.amountLimit;
|
||||
} else if (params.limit > amountLimit) {
|
||||
params.limit = amountLimit;
|
||||
}
|
||||
|
||||
return params;
|
||||
@ -69,7 +75,9 @@ module.exports = {
|
||||
model: name,
|
||||
};
|
||||
|
||||
const model = plugin ? strapi.plugins[plugin].models[name] : strapi.models[name];
|
||||
const model = plugin
|
||||
? strapi.plugins[plugin].models[name]
|
||||
: strapi.models[name];
|
||||
|
||||
// Extract custom resolver or type description.
|
||||
const { resolver: handler = {} } = _schema;
|
||||
@ -79,7 +87,9 @@ module.exports = {
|
||||
if (isSingular === 'force') {
|
||||
queryName = name;
|
||||
} else {
|
||||
queryName = isSingular ? pluralize.singular(name) : pluralize.plural(name);
|
||||
queryName = isSingular
|
||||
? pluralize.singular(name)
|
||||
: pluralize.plural(name);
|
||||
}
|
||||
|
||||
// Retrieve policies.
|
||||
@ -100,17 +110,24 @@ module.exports = {
|
||||
const resolver = _.get(handler, `Query.${queryName}.resolver`);
|
||||
|
||||
if (_.isString(resolver) || _.isPlainObject(resolver)) {
|
||||
const { handler = resolver } = _.isPlainObject(resolver) ? resolver : {};
|
||||
const { handler = resolver } = _.isPlainObject(resolver)
|
||||
? resolver
|
||||
: {};
|
||||
|
||||
// Retrieve the controller's action to be executed.
|
||||
const [name, action] = handler.split('.');
|
||||
|
||||
const controller = plugin
|
||||
? _.get(strapi.plugins, `${plugin}.controllers.${_.toLower(name)}.${action}`)
|
||||
? _.get(
|
||||
strapi.plugins,
|
||||
`${plugin}.controllers.${_.toLower(name)}.${action}`
|
||||
)
|
||||
: _.get(strapi.controllers, `${_.toLower(name)}.${action}`);
|
||||
|
||||
if (!controller) {
|
||||
return new Error(`Cannot find the controller's action ${name}.${action}`);
|
||||
return new Error(
|
||||
`Cannot find the controller's action ${name}.${action}`
|
||||
);
|
||||
}
|
||||
|
||||
// We're going to return a controller instead.
|
||||
@ -138,7 +155,9 @@ module.exports = {
|
||||
// We're going to return a controller instead.
|
||||
isController = true;
|
||||
|
||||
const controllers = plugin ? strapi.plugins[plugin].controllers : strapi.controllers;
|
||||
const controllers = plugin
|
||||
? strapi.plugins[plugin].controllers
|
||||
: strapi.controllers;
|
||||
|
||||
// Try to find the controller that should be related to this model.
|
||||
const controller = isSingular
|
||||
@ -147,7 +166,9 @@ module.exports = {
|
||||
|
||||
if (!controller) {
|
||||
return new Error(
|
||||
`Cannot find the controller's action ${name}.${isSingular ? 'findOne' : 'find'}`
|
||||
`Cannot find the controller's action ${name}.${
|
||||
isSingular ? 'findOne' : 'find'
|
||||
}`
|
||||
);
|
||||
}
|
||||
|
||||
@ -193,11 +214,16 @@ module.exports = {
|
||||
const [name, action] = resolverOf.split('.');
|
||||
|
||||
const controller = plugin
|
||||
? _.get(strapi.plugins, `${plugin}.controllers.${_.toLower(name)}.${action}`)
|
||||
? _.get(
|
||||
strapi.plugins,
|
||||
`${plugin}.controllers.${_.toLower(name)}.${action}`
|
||||
)
|
||||
: _.get(strapi.controllers, `${_.toLower(name)}.${action}`);
|
||||
|
||||
if (!controller) {
|
||||
return new Error(`Cannot find the controller's action ${name}.${action}`);
|
||||
return new Error(
|
||||
`Cannot find the controller's action ${name}.${action}`
|
||||
);
|
||||
}
|
||||
|
||||
policiesFn[0] = policyUtils.globalPolicy(
|
||||
@ -216,7 +242,13 @@ module.exports = {
|
||||
|
||||
// Populate policies.
|
||||
policies.forEach(policy =>
|
||||
policyUtils.get(policy, plugin, policiesFn, `GraphQL query "${queryName}"`, name)
|
||||
policyUtils.get(
|
||||
policy,
|
||||
plugin,
|
||||
policiesFn,
|
||||
`GraphQL query "${queryName}"`,
|
||||
name
|
||||
)
|
||||
);
|
||||
|
||||
return async (obj, options = {}, { context }) => {
|
||||
@ -233,7 +265,10 @@ module.exports = {
|
||||
const policy = await strapi.koaMiddlewares.compose(policiesFn)(ctx);
|
||||
|
||||
// Policy doesn't always return errors but they update the current context.
|
||||
if (_.isError(ctx.request.graphql) || _.get(ctx.request.graphql, 'isBoom')) {
|
||||
if (
|
||||
_.isError(ctx.request.graphql) ||
|
||||
_.get(ctx.request.graphql, 'isBoom')
|
||||
) {
|
||||
return ctx.request.graphql;
|
||||
}
|
||||
|
||||
@ -246,17 +281,22 @@ module.exports = {
|
||||
if (_.isFunction(resolver)) {
|
||||
// Note: we've to used the Object.defineProperties to reset the prototype. It seems that the cloning the context
|
||||
// cause a lost of the Object prototype.
|
||||
const opts = this.amountLimiting(_options);
|
||||
|
||||
Object.defineProperties(ctx, {
|
||||
query: {
|
||||
value: {
|
||||
...this.convertToParams(_.omit(_options, 'where'), model.primaryKey),
|
||||
...this.convertToQuery(_options.where),
|
||||
...this.convertToParams(
|
||||
_.omit(opts, 'where'),
|
||||
model ? model.primaryKey : 'id'
|
||||
),
|
||||
...this.convertToQuery(opts.where),
|
||||
},
|
||||
writable: true,
|
||||
configurable: true,
|
||||
},
|
||||
params: {
|
||||
value: this.convertToParams(this.amountLimiting(_options), model.primaryKey),
|
||||
value: this.convertToParams(opts, model ? model.primaryKey : 'id'),
|
||||
writable: true,
|
||||
configurable: true,
|
||||
},
|
||||
@ -272,7 +312,7 @@ module.exports = {
|
||||
return values && values.toJSON ? values.toJSON() : values;
|
||||
}
|
||||
|
||||
return resolver.call(null, obj, _options, ctx);
|
||||
return resolver.call(null, obj, opts, ctx);
|
||||
}
|
||||
|
||||
// Resolver can be a promise.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user