UNPKG

@jcm/nexus-plugin-field-authentication

Version:

Adds a new field config "authentication" that should be used to determine if the field should be resolved or not

42 lines (41 loc) 2.39 kB
import { core } from 'nexus'; import { GraphQLResolveInfo } from 'graphql'; export declare type FieldAuthenticationResolverReturnValue = boolean | Error | [boolean, Error] | [boolean, core.ResultValue<any, any>]; export declare type FieldAuthenticationResolver<TypeName extends string, FieldName extends string> = (root: core.RootValueField<TypeName, FieldName>, args: core.ArgsValue<TypeName, FieldName>, ctx: core.GetGen<'context'>, info: GraphQLResolveInfo) => core.MaybePromise<FieldAuthenticationResolverReturnValue>; export interface FieldAuthenticationPluginErrorConfig { error: Error; root: any; args: any; ctx: core.GetGen<'context'>; info: GraphQLResolveInfo; } export declare const defaultFormatError: ({ error }: FieldAuthenticationPluginErrorConfig) => Error; export declare type FieldAuthenticationPluginConfigIsLogged<TypeName extends string, FieldName extends string> = (root: core.RootValueField<TypeName, FieldName>, args: core.ArgsValue<TypeName, FieldName>, ctx: core.GetGen<'context'>, info: GraphQLResolveInfo) => boolean | core.MaybePromise<boolean>; export interface FieldAuthenticationPluginConfig { /** * This will be called with the error that will be returned when resolving a field * that did not pass the authentication check when throwErrorOnFailedAuthenticationByDefault * was set to true or when the authentication had a tuple [boolean, Error] * By default this is the function ({error: Error}) => error */ formatError?: (authConfig: FieldAuthenticationPluginErrorConfig) => Error; /** * If this is true and the authentication field does not include * a default resolve value, an error will be thrown on failed authentication */ throwErrorOnFailedAuthenticationByDefault?: boolean; /** * If throwErrorOnFailedAuthenticationByDefault is true, */ defaultErrorMessage?: string; /** * If throwErrorOnFailedAuthenticationByDefault is false, a failed authentication check will return * this value as the resolved value of the field */ defaultResolveValue?: core.ResultValue<any, any>; /** * By default this checks if ctx.state.user exists */ isLogged?: FieldAuthenticationPluginConfigIsLogged<any, any>; } export declare function fieldAuthenticationPlugin(pluginConfig?: FieldAuthenticationPluginConfig): core.NexusPlugin;