Made ctx.params and ctx.query available to policies executed via graphql requests

This commit is contained in:
Don Masakayan 2019-10-16 22:23:17 +08:00
parent 9f0713649b
commit aa91ca7a96
2 changed files with 24 additions and 24 deletions

View File

@ -183,6 +183,18 @@ module.exports = {
}),
});
if (options.input && options.input.where) {
ctx.params = Query.convertToParams(options.input.where || {});
} else {
ctx.params = {};
}
if (options.input && options.input.data) {
ctx.request.body = options.input.data || {};
} else {
ctx.request.body = options;
}
// Execute policies stack.
const policy = await compose(policiesFn)(ctx);
@ -203,20 +215,8 @@ module.exports = {
if (_.isFunction(resolver)) {
const normalizedName = _.toLower(name);
if (options.input && options.input.where) {
context.params = Query.convertToParams(options.input.where || {});
} else {
context.params = {};
}
if (options.input && options.input.data) {
context.request.body = options.input.data || {};
} else {
context.request.body = options;
}
if (isController) {
const values = await resolver.call(null, context);
const values = await resolver.call(null, ctx);
if (ctx.body) {
return options.input

View File

@ -257,6 +257,17 @@ module.exports = {
}),
});
// 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);
ctx.query = {
...this.convertToParams(_.omit(opts, 'where')),
...this.convertToQuery(opts.where),
};
ctx.params = this.convertToParams(opts);
// Execute policies stack.
const policy = await compose(policiesFn)(ctx);
@ -275,17 +286,6 @@ module.exports = {
// Resolver can be a function. Be also a native resolver or a controller's action.
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);
ctx.query = {
...this.convertToParams(_.omit(opts, 'where')),
...this.convertToQuery(opts.where),
};
ctx.params = this.convertToParams(opts);
if (isController) {
const values = await resolver.call(null, ctx, null);