Resolver
The most interesting class in graphql-compose
. The main goal of Resolver
is to keep available resolve methods for Type and use them for building relation with other types.
Properties
schemaComposer
schemaComposer: SchemaComposer<TContext>;
type
type: ComposeOutputType<TReturn, TContext>;
args
args: ComposeFieldConfigArgumentMap<any>;
resolve
resolve: (
resolveParams: Partial<ResolveParams<TSource, TContext, TArgs>>
) => Promise<any> | any;
name
name: string;
displayName
displayName: string | void;
kind
kind: ResolverKinds | void;
description
description: string | void;
parent
parent: Resolver<TSource, TContext, any> | void;
Output type methods
getType()
getType(): GraphQLOutputType
getTypeComposer()
getTypeComposer(): ObjectTypeComposer<TSource, TContext>
setType()
setType<TNewReturn>(
gqType: ComposeOutputType<TNewReturn, TContext>
): Resolver<TSource, TContext, TArgs, TNewReturn>
Args methods
hasArg()
hasArg(
argName: string
): boolean
getArg()
getArg(
argName: string
): ComposeArgumentConfig
getArgConfig()
getArgConfig(
argName: string
): GraphQLArgumentConfig
getArgType()
getArgType(
argName: string
): GraphQLInputType
getArgTC()
getArgTC(
argName: string
): InputTypeComposer<TContext>
getArgs()
getArgs(): ComposeFieldConfigArgumentMap<TArgs>
getArgNames()
getArgNames(): string[]
setArgs()
setArgs<TNewArgs>(
args: ComposeFieldConfigArgumentMap<TNewArgs>
): Resolver<TSource, TContext, TNewArgs>
setArg()
setArg(
argName: string,
argConfig: ComposeArgumentConfig
): this
extendArg()
extendArg(
argName: string,
partialArgConfig: Partial<ComposeArgumentConfigAsObject>
): this
addArgs()
addArgs(
newArgs: ComposeFieldConfigArgumentMap<TArgs>
): this
removeArg()
removeArg(
argNameOrArray: string | string[]
): this
removeOtherArgs()
removeOtherArgs(
argNameOrArray: string | string[]
): this
reorderArgs()
reorderArgs(
names: string[]
): this
cloneArg()
cloneArg(
argName: string,
newTypeName: string
): this
isRequired()
isRequired(
argName: string
): boolean
makeRequired()
makeRequired(
argNameOrArray: string | string[]
): this
makeOptional()
makeOptional(
argNameOrArray: string | string[]
): this
addFilterArg()
addFilterArg(
opts: ResolverFilterArgConfig<TSource, TContext, TArgs>
): Resolver<TSource, TContext, TArgs>
addSortArg()
addSortArg(
opts: ResolverSortArgConfig<TSource, TContext, TArgs>
): Resolver<TSource, TContext, TArgs>
Resolve methods
getResolve()
getResolve(): ResolverRpCb<TSource, TContext, TArgs>
setResolve()
setResolve(
resolve: ResolverRpCb<TSource, TContext, TArgs>
): Resolver<TSource, TContext, TArgs>
Wrap methods
withMiddlewares()
withMiddlewares(
middlewares: Array<ResolverMiddleware<TSource, TContext, TArgs>>
): Resolver<TSource, TContext, TArgs>
You may construct a new resolver with wrapped logic:
const log = [];
const mw1 = async (resolve, source, args, context, info) => {
log.push('m1.before');
const res = await resolve(source, args, context, info);
log.push('m1.after');
return res;
};
const mw2 = async (resolve, source, args, context, info) => {
log.push('m2.before');
const res = await resolve(source, args, context, info);
log.push('m2.after');
return res;
};
const newResolver = Resolver.withMiddlewares([mw1, mw2]);
await newResolver.resolve({});
expect(log).toEqual([
'm1.before',
'm2.before',
'call resolve',
'm2.after',
'm1.after'
]);
wrap()
wrap<TNewSource = TSource, TNewArgs = TArgs>(
cb: ResolverWrapCb<TNewSource, TSource, TContext, TNewArgs, TArgs>,
newResolverOpts: ResolverOpts<TNewSource, TContext, TArgs>
): Resolver<TNewSource, TContext, TNewArgs>
wrapResolve()
wrapResolve<TCSource = TSource, TCArgs = TArgs>(
cb: ResolverNextRpCb<TCSource, TContext, TCArgs>,
wrapperName: string
): Resolver<TCSource, TContext, TCArgs>
wrapArgs()
wrapArgs<TCArgs = TArgs>(
cb: ResolverWrapArgsCb<TCArgs>,
wrapperName: string
): Resolver<TSource, TContext, TCArgs>
wrapCloneArg()
wrapCloneArg<TCArgs = TArgs>(
argName: string,
newTypeName: string
): Resolver<TSource, TContext, TCArgs>
wrapType()
wrapType(
cb: ResolverWrapTypeCb<TContext>,
wrapperName: string
): Resolver<TSource, TContext, TArgs>
Misc methods
getFieldConfig()
getFieldConfig(
opts: {
projection?: ProjectionType;
}
): GraphQLFieldConfig<TSource, TContext, TArgs>
getKind()
getKind(): ResolverKinds | void
setKind()
setKind(
kind: string
): this
getDescription()
getDescription(): string | null
setDescription()
setDescription(
description: string | void
): this
get()
get(
path: string | string[]
): any
clone()
clone<TNewSource = TSource, TNewArgs = TArgs>(
opts: ResolverOpts<TNewSource, TContext, TNewArgs>
): Resolver<TNewSource, TContext, TNewArgs>
Debug methods
getNestedName()
getNestedName(): string
toString()
toString(
colors: boolean
): string
setDisplayName()
setDisplayName(
name: string
): this
toDebugStructure()
toDebugStructure(
colors: boolean
): object
debugExecTime()
debugExecTime(): Resolver<TSource, TContext, TArgs>
debugParams()
debugParams(
filterPaths: (string | string[]) | null,
opts: ResolveDebugOpts
): Resolver<TSource, TContext, TArgs>
debugPayload()
debugPayload(
filterPaths: (string | string[]) | null,
opts: ResolveDebugOpts
): Resolver<TSource, TContext, TArgs>
debug()
debug(
filterDotPaths: {
params?: string | string[];
payload?: string | string[];
},
opts: ResolveDebugOpts
): Resolver<TSource, TContext, TArgs>
Internal type definitions
ResolveParams
export type ResolveParams<TSource, TContext, TArgs = ArgsMap> = {
source: TSource;
args: TArgs;
context: TContext;
info: GraphQLResolveInfo;
projection: Partial<ProjectionType>;
[opt: string]: any;
};
ResolverKinds
export type ResolverKinds = 'query' | 'mutation' | 'subscription';
ResolverFilterArgFn
export type ResolverFilterArgFn<TSource, TContext, TArgs = ArgsMap> = (
query: any,
value: any,
resolveParams: ResolveParams<TSource, TContext, TArgs>,
) => any;
ResolverFilterArgConfig
export type ResolverFilterArgConfig<TSource, TContext, TArgs = ArgsMap> = {
name: string;
type: ComposeArgumentType;
description?: string;
query?: ResolverFilterArgFn<TSource, TContext, TArgs>;
filterTypeNameFallback?: string;
defaultValue?: any;
};
ResolverSortArgFn
export type ResolverSortArgFn<TSource, TContext, TArgs = ArgsMap> = (
resolveParams: ResolveParams<TSource, TContext, TArgs>,
) => any;
ResolverSortArgConfig
export type ResolverSortArgConfig<TSource, TContext, TArgs = ArgsMap> = {
name: string;
sortTypeNameFallback?: string;
value:
| { [key: string]: any }
| ResolverSortArgFn<TSource, TContext, TArgs>
| string
| number
| boolean
| any[];
deprecationReason?: string | null;
description?: string | null;
};
ResolverOpts
export type ResolverOpts<TSource, TContext, TArgs = ArgsMap, TReturn = any> = {
type?: ComposeOutputType<TReturn, TContext>;
resolve?: ResolverRpCb<TSource, TContext, TArgs>;
args?: ComposeFieldConfigArgumentMap<TArgs>;
name?: string;
displayName?: string;
kind?: ResolverKinds;
description?: string;
parent?: Resolver<any, TContext, any>;
};
ResolverWrapCb
export type ResolverWrapCb<
TNewSource,
TPrevSource,
TContext,
TNewArgs = ArgsMap,
TPrevArgs = ArgsMap
> = (
newResolver: Resolver<TNewSource, TContext, TNewArgs>,
prevResolver: Resolver<TPrevSource, TContext, TPrevArgs>,
) => Resolver<TNewSource, TContext, TNewArgs>;
ResolverRpCb
export type ResolverRpCb<TSource, TContext, TArgs = ArgsMap> = (
resolveParams: ResolveParams<TSource, TContext, TArgs>,
) => Promise<any> | any;
ResolverNextRpCb
export type ResolverNextRpCb<TSource, TContext, TArgs = ArgsMap> = (
next: ResolverRpCb<TSource, TContext, TArgs>,
) => ResolverRpCb<TSource, TContext, TArgs>;
ResolverWrapArgsCb
export type ResolverWrapArgsCb<TArgs = ArgsMap> = (
prevArgs: GraphQLFieldConfigArgumentMap,
) => ComposeFieldConfigArgumentMap<TArgs>;
ResolverWrapTypeCb
export type ResolverWrapTypeCb<TContext, TReturn = any> = (
prevType: GraphQLOutputType,
) => ComposeOutputType<TReturn, TContext>;
ResolveDebugOpts
export type ResolveDebugOpts = {
showHidden?: boolean;
depth?: number;
colors?: boolean;
};
ResolverMiddleware
export type ResolverMiddleware<TSource, TContext, TArgs = ArgsMap> = (
resolve: (
source: TSource,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo,
) => any,
source: TSource,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo,
) => any;