mirror of
https://github.com/strapi/strapi.git
synced 2025-07-29 20:10:21 +00:00

* Add basic implementation for the graphql extension service * Add createPolicyContext in @strapi/utils * policiesMiddleware implementation for graphql * wrapResolvers first implementation (authentication, middlewares, policies) * move the content API schema build from /generators to /content-api. Extract types' register functions into a dedicated folder * fix schema generation on bootstrap * update the graphql service file to match new services arch * fix single type queries * simplify entity's resolver * use apollo graphql conventions for resolver's args naming * use the graphql extension system in i18n to add a locale arg to localized queries & mutations
60 lines
1.2 KiB
JavaScript
60 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const { getOr } = require('lodash/fp');
|
|
const { policy: policyUtils } = require('@strapi/utils');
|
|
|
|
const createPoliciesMiddleware = (resolverConfig, { strapi }) => {
|
|
return async (resolve, ...rest) => {
|
|
const resolverPolicies = getOr([], 'policies', resolverConfig);
|
|
|
|
// Transform every policy into a unique format
|
|
const policies = resolverPolicies.map(policy => policyUtils.get(policy));
|
|
|
|
// Create a graphql policy context
|
|
const context = createGraphQLPolicyContext(...rest);
|
|
|
|
// Run policies & throw an error if one of them fails
|
|
for (const policy of policies) {
|
|
const result = await policy({ context, strapi });
|
|
|
|
if (!result) {
|
|
throw new Error('Policies failed');
|
|
}
|
|
}
|
|
|
|
return resolve(...rest);
|
|
};
|
|
};
|
|
|
|
const createGraphQLPolicyContext = (parent, args, context, info) => {
|
|
return policyUtils.createPolicyContext('graphql', {
|
|
get parent() {
|
|
return parent;
|
|
},
|
|
|
|
get args() {
|
|
return args;
|
|
},
|
|
|
|
get context() {
|
|
return context;
|
|
},
|
|
|
|
get info() {
|
|
return info;
|
|
},
|
|
|
|
get state() {
|
|
return this.context.state;
|
|
},
|
|
|
|
get http() {
|
|
return this.context.koaContext;
|
|
},
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
createPoliciesMiddleware,
|
|
};
|