From aa91ca7a9627ee7abdb86e75ee4d90d03b6b991f Mon Sep 17 00:00:00 2001 From: Don Masakayan Date: Wed, 16 Oct 2019 22:23:17 +0800 Subject: [PATCH] Made ctx.params and ctx.query available to policies executed via graphql requests --- .../services/Mutation.js | 26 +++++++++---------- .../strapi-plugin-graphql/services/Query.js | 22 ++++++++-------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/packages/strapi-plugin-graphql/services/Mutation.js b/packages/strapi-plugin-graphql/services/Mutation.js index 3b1f590cf0..e98d4b4af7 100644 --- a/packages/strapi-plugin-graphql/services/Mutation.js +++ b/packages/strapi-plugin-graphql/services/Mutation.js @@ -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 diff --git a/packages/strapi-plugin-graphql/services/Query.js b/packages/strapi-plugin-graphql/services/Query.js index 28dff969ff..1283c4aa35 100644 --- a/packages/strapi-plugin-graphql/services/Query.js +++ b/packages/strapi-plugin-graphql/services/Query.js @@ -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);