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<TContext>;
args
args: ObjectTypeComposerArgumentConfigMap<any>;
name
name: string;
displayName
displayName: string | undefined;
kind
kind: ResolverKinds | undefined;
description
description: string | undefined;
deprecationReason
deprecationReason: string | null | undefined;
projection
projection: ProjectionType;
parent
parent: Resolver<TSource, TContext, any> | undefined;
extensions
extensions: Extensions | undefined;
directives
directives: Directive[] | undefined;
resolve
resolve: ResolverRpCbPartial<TSource, TContext, TArgs>;
Output type methods
getType()
getType(): GraphQLOutputType
getTypeName()
getTypeName(): string
getTypeComposer()
getTypeComposer(): ComposeNamedOutputType<TContext>
getOTC()
getOTC(): ObjectTypeComposer<TReturn, TContext>
Almost alias for getTypeComposer
, but returns only ObjectTypeComposer.
It will throw an error if resolver has another kind of type.
setType()
setType<TNewReturn>(
typeDef: ThunkWithSchemaComposer<Readonly<ComposeOutputType<TContext>> | ComposeOutputTypeDefinition<TContext> | Readonly<Resolver<any, TContext, any>>, SchemaComposer<TContext>>
): Resolver<TSource, TContext, TArgs, TNewReturn>
Args methods
hasArg()
hasArg(
argName: string
): boolean
getArg()
getArg(
argName: string
): ObjectTypeComposerArgumentConfig
getArgConfig()
getArgConfig(
argName: string
): GraphQLArgumentConfig
getArgType()
getArgType(
argName: string
): GraphQLInputType
getArgTypeName()
getArgTypeName(
fieldName: string
): string
getArgs()
getArgs(): ObjectTypeComposerArgumentConfigMap<TArgs>
getArgNames()
getArgNames(): string[]
setArgs()
setArgs<TNewArgs>(
args: ObjectTypeComposerArgumentConfigMapDefinition<TNewArgs>
): Resolver<TSource, TContext, TNewArgs>
setArg()
setArg(
argName: string,
argConfig: ObjectTypeComposerArgumentConfigDefinition
): this
setArgType()
setArgType(
argName: string,
typeDef: Thunk<ComposeInputTypeDefinition>
): this
extendArg()
extendArg(
argName: string,
partialArgConfig: Partial<ObjectTypeComposerArgumentConfigAsObjectDefinition>
): this
addArgs()
addArgs(
newArgs: ObjectTypeComposerArgumentConfigMapDefinition<any>
): this
removeArg()
removeArg(
argNameOrArray: string | string[]
): this
removeOtherArgs()
removeOtherArgs(
argNameOrArray: string | string[]
): this
reorderArgs()
reorderArgs(
names: string[]
): this
getArgTC()
getArgTC(
argName: string
): ComposeNamedInputType<TContext>
getArgITC()
getArgITC(
argName: string
): InputTypeComposer<TContext>
Alias for getArgTC()
but returns statically checked InputTypeComposer.
If field have other type then error will be thrown.
isArgNonNull()
isArgNonNull(
argName: string
): boolean
makeArgNonNull()
makeArgNonNull(
argNameOrArray: string | string[]
): this
makeRequired()
makeRequired(
argNameOrArray: string | string[]
): this
makeArgNullable()
makeArgNullable(
argNameOrArray: string | string[]
): this
makeOptional()
makeOptional(
argNameOrArray: string | string[]
): this
isArgPlural()
isArgPlural(
argName: string
): boolean
makeArgPlural()
makeArgPlural(
argNameOrArray: string | string[]
): this
makeArgNonPlural()
makeArgNonPlural(
argNameOrArray: string | string[]
): this
cloneArg()
cloneArg(
argName: string,
newTypeName: string
): this
addFilterArg()
addFilterArg(
opts: ResolverFilterArgConfigDefinition<TSource, TContext, TArgs>
): this
addSortArg()
addSortArg(
opts: ResolverSortArgConfig<TSource, TContext, TArgs>
): this
Resolve methods
getResolve()
getResolve(): ResolverRpCb<TSource, TContext, TArgs>
setResolve()
setResolve(
resolve: ResolverRpCb<TSource, TContext, TArgs>
): this
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, TNewArgs>(
cb: ResolverWrapCb<TNewSource, TSource, TContext, TNewArgs, TArgs> | null,
newResolverOpts: Partial<ResolverDefinition<TNewSource, TContext, TNewArgs>>
): Resolver<TNewSource, TContext, TNewArgs>
wrapResolve()
wrapResolve(
cb: ResolverNextRpCb<TSource, TContext, TArgs>,
wrapperName: string
): Resolver<TSource, TContext, TArgs>
wrapArgs()
wrapArgs<TNewArgs>(
cb: (prevArgs: GraphQLFieldConfigArgumentMap) => ObjectTypeComposerArgumentConfigMapDefinition<TArgs>,
wrapperName: string
): Resolver<TSource, TContext, TNewArgs>
wrapCloneArg()
wrapCloneArg(
argName: string,
newTypeName: string
): Resolver<TSource, TContext, TArgs>
wrapType()
wrapType(
cb: (prevType: ComposeOutputType<TContext>) => ComposeOutputTypeDefinition<TContext>,
wrapperName: string
): Resolver<TSource, TContext, TArgs>
Misc methods
getFieldConfig()
getFieldConfig(
opts: {
projection?: ProjectionType;
}
): GraphQLFieldConfig<TSource, TContext, TArgs>
getFieldResolver()
getFieldResolver(
opts: {
projection?: ProjectionType;
}
): GraphQLFieldResolver<TSource, TContext, TArgs>
getKind()
getKind(): ResolverKinds | void
setKind()
setKind(
kind: string
): this
getDescription()
getDescription(): string | undefined
setDescription()
setDescription(
description: string | undefined
): this
getDeprecationReason()
getDeprecationReason(): string | undefined | null
setDeprecationReason()
setDeprecationReason(
reason: string | undefined
): this
get()
get(
path: string | string[]
): TypeInPath<TContext> | void
clone()
clone<TNewSource, TNewArgs>(
opts: Partial<ResolverDefinition<TNewSource, TContext, TNewArgs>>
): Resolver<TNewSource, TContext, TNewArgs>
Clone this Resolver with overriding of some options.
Internally it just copies all properties.
But for args
and projection
it recreates objects with the same type & values (it allows to add or remove properties without affection old Resolver).
cloneTo()
cloneTo(
anotherSchemaComposer: SchemaComposer<any>,
cloneMap: Map<any, any>
): Resolver<any, any, any>
Clone this resolver to another SchemaComposer. Also will be cloned all sub-types.
Extensions methods
getExtensions()
getExtensions(): Extensions
setExtensions()
setExtensions(
extensions: Extensions
): this
extendExtensions()
extendExtensions(
extensions: Extensions
): this
clearExtensions()
clearExtensions(): this
getExtension()
getExtension(
extensionName: string
): unknown
hasExtension()
hasExtension(
extensionName: string
): boolean
setExtension()
setExtension(
extensionName: string,
value: unknown
): this
removeExtension()
removeExtension(
extensionName: string
): this
Debug methods
getNestedName()
getNestedName(): string
toString()
toString(): string
setDisplayName()
setDisplayName(
name: string
): this
toDebugStructure()
toDebugStructure(): Record<string, any>
debugExecTime()
debugExecTime(): Resolver<TSource, TContext, TArgs>
debugParams()
debugParams(
filterPaths: string | string[] | undefined | null,
opts: ResolverDebugOpts
): Resolver<TSource, TContext, TArgs>
debugPayload()
debugPayload(
filterPaths: string | string[] | undefined | null,
opts: ResolverDebugOpts
): Resolver<TSource, TContext, TArgs>
debug()
debug(
filterDotPaths: {
params?: string | string[] | undefined | null;
payload?: string | string[] | undefined | null;
},
opts: ResolverDebugOpts
): Resolver<TSource, TContext, TArgs>
Internal type definitions
ResolverKinds
export type ResolverKinds = 'query' | 'mutation' | 'subscription';
ResolverDefinition
export type ResolverDefinition<TSource, TContext, TArgs = any> = {
type?: ThunkWithSchemaComposer<
| Readonly<ComposeOutputType<TContext>>
| ComposeOutputTypeDefinition<TContext>
| Readonly<Resolver<any, TContext, any>>,
SchemaComposer<TContext>
>;
resolve?: ResolverRpCb<TSource, TContext, TArgs>;
args?: ObjectTypeComposerArgumentConfigMapDefinition<TArgs>;
name?: string;
displayName?: string;
kind?: ResolverKinds;
description?: string;
deprecationReason?: string | null;
projection?: ProjectionType;
parent?: Resolver<any, TContext, any>;
extensions?: Extensions;
directives?: Directive[];
};
ResolverResolveParams
export type ResolverResolveParams<TSource, TContext, TArgs = any> = {
source: TSource;
args: TArgs;
context: TContext;
info: GraphQLResolveInfo;
projection: Partial<ProjectionType>;
[opt: string]: any;
};
ResolverFilterArgFn
export type ResolverFilterArgFn<TSource, TContext, TArgs = any> = (
query: any,
value: any,
resolveParams: ResolverResolveParams<TSource, TContext, TArgs>
) => any;
ResolverFilterArgConfigDefinition
export type ResolverFilterArgConfigDefinition<TSource, TContext, TArgs = any> = {
name: string;
type: ComposeInputTypeDefinition;
description?: string | null | void;
query?: ResolverFilterArgFn<TSource, TContext, TArgs>;
filterTypeNameFallback?: string;
defaultValue?: any;
};
ResolverSortArgFn
export type ResolverSortArgFn<TSource, TContext, TArgs = any> = (
resolveParams: ResolverResolveParams<TSource, TContext, TArgs>
) => any;
ResolverSortArgConfig
export type ResolverSortArgConfig<TSource, TContext, TArgs = any> = {
name: string;
sortTypeNameFallback?: string;
value:
| { [key: string]: any }
| ResolverSortArgFn<TSource, TContext, TArgs>
| string
| number
| boolean
| any[];
deprecationReason?: string | null;
description?: string | null;
};
ResolverWrapCb
export type ResolverWrapCb<TNewSource, TPrevSource, TContext, TNewArgs = any, TPrevArgs = any> = (
newResolver: Resolver<TNewSource, TContext, TNewArgs>,
prevResolver: Resolver<TPrevSource, TContext, TPrevArgs>
) => Resolver<TNewSource, TContext, TNewArgs>;
ResolverRpCb
export type ResolverRpCb<TSource, TContext, TArgs = any> = (
resolveParams: ResolverResolveParams<TSource, TContext, TArgs>
) => Promise<any> | any;
ResolverRpCbPartial
export type ResolverRpCbPartial<TSource, TContext, TArgs = any> = (
resolveParams: Partial<ResolverResolveParams<TSource, TContext, TArgs>>
) => Promise<any> | any;
ResolverNextRpCb
export type ResolverNextRpCb<TSource, TContext, TArgs = any> = (
next: ResolverRpCb<TSource, TContext, TArgs>
) => ResolverRpCb<TSource, TContext, TArgs>;
ResolverDebugOpts
export type ResolverDebugOpts = {
showHidden?: boolean;
depth?: number;
colors?: boolean;
};
ResolverMiddleware
export type ResolverMiddleware<TSource, TContext, TArgs = any> = (
resolve: (source: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo) => any,
source: TSource,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo
) => any;