UNPKG

graphql-shield

Version:

GraphQL Server permissions as another layer of abstraction!

78 lines (77 loc) 3.47 kB
import { GraphQLFieldResolver, GraphQLResolveInfo } from 'graphql'; export declare type ShieldRule = IRule | ILogicRule; export interface IRule { readonly name: string; equals(rule: IRule): boolean; extractFragment(): IFragment | undefined; resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>; } export interface IRuleOptions { cache: ICache; fragment?: IFragment; } export interface ILogicRule { getRules(): ShieldRule[]; extractFragments(): IFragment[]; evaluate(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult[]>; resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>; } export declare type IFragment = string; export declare type ICache = 'strict' | 'contextual' | 'no_cache' | ICacheKeyFn; export declare type ICacheKeyFn = (parent: any, args: any, ctx: any, info: GraphQLResolveInfo) => string; export declare type IRuleResult = boolean | string | Error; export declare type IRuleFunction = (parent: any, args: any, ctx: any, info: GraphQLResolveInfo) => IRuleResult | Promise<IRuleResult>; export declare type ICacheContructorOptions = ICache | boolean; export interface IRuleConstructorOptions { cache?: ICacheContructorOptions; fragment?: IFragment; } export interface IRuleTypeMap { [key: string]: ShieldRule | IRuleFieldMap; } export interface IRuleFieldMap { [key: string]: ShieldRule; } export declare type IRules = ShieldRule | IRuleTypeMap; export declare type IHashFunction = (arg: { parent: any; args: any; }) => string; export declare type IFallbackErrorMapperType = (err: unknown, parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo) => Promise<Error> | Error; export declare type IFallbackErrorType = Error | IFallbackErrorMapperType; export interface IOptions { debug: boolean; allowExternalErrors: boolean; fallbackRule: ShieldRule; fallbackError?: IFallbackErrorType; hashFunction: IHashFunction; disableFragmentsAndPostExecRules: boolean; } export interface IOptionsConstructor { debug?: boolean; allowExternalErrors?: boolean; fallbackRule?: ShieldRule; fallbackError?: string | IFallbackErrorType; hashFunction?: IHashFunction; disableFragmentsAndPostExecRules?: boolean; } export interface IShieldContext { _shield: { cache: { [key: string]: IRuleResult | Promise<IRuleResult>; }; }; } export declare type IMiddlewareResolver<TSource = any, TContext = any, TArgs = any> = (resolve: GraphQLFieldResolver<TSource, TContext, TArgs>, parent: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo) => Promise<any>; export interface IMiddlewareWithOptions<TSource = any, TContext = any, TArgs = any> { fragment?: string; fragments?: string[]; resolve?: IMiddlewareResolver<TSource, TContext, TArgs>; } export declare type IMiddlewareFunction<TSource = any, TContext = any, TArgs = any> = IMiddlewareWithOptions<TSource, TContext, TArgs>; export interface IMiddlewareFieldMap<TSource = any, TContext = any, TArgs = any> { [key: string]: IMiddlewareFunction<TSource, TContext, TArgs>; } export interface IMiddlewareTypeMap<TSource = any, TContext = any, TArgs = any> { [key: string]: IMiddlewareFieldMap<TSource, TContext, TArgs>; }