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 | void;
kind
kind: ResolverKinds | void;
description
description: string | void;
parent
parent: Resolver<TSource, TContext, any> | void;
resolve
resolve: (
resolveParams: Partial<ResolverResolveParams<TSource, TContext, TArgs>>
) => Promise<any> | any;
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>(
composeType: Thunk<Readonly<ComposeOutputType<TContext>> | ComposeOutputTypeDefinition<TContext> | Readonly<Resolver<any, TContext, any>>>
): 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<ArgsMap>
): 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>
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 = TSource, TNewArgs = TArgs>(
cb: ResolverWrapCb<TNewSource, TSource, TContext, TNewArgs, TArgs>,
newResolverOpts: Partial<ResolverDefinition<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: (prevArgs: GraphQLFieldConfigArgumentMap) => ObjectTypeComposerArgumentConfigMapDefinition<TArgs>,
wrapperName: string
): Resolver<TSource, TContext, TCArgs>
wrapCloneArg()
wrapCloneArg<TCArgs = TArgs>(
argName: string,
newTypeName: string
): Resolver<TSource, TContext, TCArgs>
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>
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: Partial<ResolverDefinition<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: ResolverDebugOpts
): Resolver<TSource, TContext, TArgs>
debugPayload()
debugPayload(
filterPaths: (string | string[]) | null,
opts: ResolverDebugOpts
): Resolver<TSource, TContext, TArgs>
debug()
debug(
filterDotPaths: {
params?: string | string[];
payload?: string | string[];
},
opts: ResolverDebugOpts
): Resolver<TSource, TContext, TArgs>
Internal type definitions
ResolverKinds
export type ResolverKinds = 'query' | 'mutation' | 'subscription';
ResolverDefinition
export type ResolverDefinition<TSource, TContext, TArgs = ArgsMap> = {
type?: Thunk<
| Readonly<ComposeOutputType<TContext>>
| ComposeOutputTypeDefinition<TContext>
| Readonly<Resolver<any, TContext, any>>
>;
resolve?: ResolverRpCb<TSource, TContext, TArgs>;
args?: ObjectTypeComposerArgumentConfigMapDefinition<TArgs>;
name?: string;
displayName?: string;
kind?: ResolverKinds;
description?: string;
parent?: Resolver<any, TContext, any>;
};
ResolverResolveParams
export type ResolverResolveParams<TSource, TContext, TArgs = ArgsMap> = {
source: TSource;
args: TArgs;
context: TContext;
info: GraphQLResolveInfo;
projection: Partial<ProjectionType>;
[opt: string]: any;
};
ResolverFilterArgFn
export type ResolverFilterArgFn<TSource, TContext, TArgs = ArgsMap> = (
query: any,
value: any,
resolveParams: ResolverResolveParams<TSource, TContext, TArgs>
) => any;
ResolverFilterArgConfigDefinition
export type ResolverFilterArgConfigDefinition<TSource, TContext, TArgs = ArgsMap> = {
name: string;
type: ComposeInputTypeDefinition;
description?: string | null | void;
query?: ResolverFilterArgFn<TSource, TContext, TArgs>;
filterTypeNameFallback?: string;
defaultValue?: any;
};
ResolverSortArgFn
export type ResolverSortArgFn<TSource, TContext, TArgs = ArgsMap> = (
resolveParams: ResolverResolveParams<TSource, TContext, TArgs>
) => any;
ResolverSortArgConfig
export type ResolverSortArgConfig<TSource, TContext, TArgs = ArgsMap> = {
name: string;
sortTypeNameFallback?: string;
// value also can be an `Object`, but flow does not understande union with object and function
// see https://github.com/facebook/flow/issues/1948
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 = 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: ResolverResolveParams<TSource, TContext, TArgs>
) => Promise<any> | any;
ResolverNextRpCb
export type ResolverNextRpCb<TSource, TContext, TArgs = ArgsMap> = (
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 = ArgsMap> = (
resolve: (source: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo) => any,
source: TSource,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo
) => any;