/
githubmirror
/
strapi
Обзор
Документация
Войти
/
githubmirror
/
strapi
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/plugins/graphql/server/src/services/content-api/policy.ts
72 строки
2 KB
Alexandre Bodin
chore: reintegrate policy logic into core
25 мар 2024, 10:51
25 мар 2024, 10:51
78d30d2
Код
Авторство
О чём код?
import { propOr } from 'lodash/fp'; import { GraphQLFieldResolver, GraphQLResolveInfo } from 'graphql'; import { policy as policyUtils, errors } from '@strapi/utils'; import type { Core } from '@strapi/types'; const { PolicyError } = errors; const getPoliciesConfig = propOr([], 'policies'); const createPoliciesMiddleware = (resolverConfig: any, { strapi }: { strapi: Core.Strapi }) => { const resolverPolicies = getPoliciesConfig(resolverConfig); const policies = strapi.get('policies').resolve(resolverPolicies, {}); return async ( resolve: GraphQLFieldResolver<any, any>, parent: any, args: any, context: any, info: GraphQLResolveInfo ) => { // Create a graphql policy context const policyContext = createGraphQLPolicyContext(parent, args, context, info); // Run policies & throw an error if one of them fails for (const { handler, config } of policies) { const result = await handler(policyContext, config, { strapi }); if (![true, undefined].includes(result)) { throw new PolicyError(); } } return resolve(parent, args, context, info); }; }; const createGraphQLPolicyContext = ( parent: any, args: any, context: any, info: GraphQLResolveInfo ) => { const policyContext = { 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; }, }; return policyUtils.createPolicyContext('graphql', policyContext); }; export { createPoliciesMiddleware };