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
Current SchemaComposer
instance which is used for storing types created by Resolver
.
Resolver.schemaComposer: SchemaComposer;
Output type methods
getType()
getType(): GraphQLOutputType;
getTypeComposer()
getTypeComposer(): TypeComposer<TContext>;
setType()
setType(
gqType: ComposeOutputType<TContext>
): Resolver;
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;
getArgs()
getArgs(): ComposeFieldConfigArgumentMap;
getArgNames()
getArgNames(): Array<string>;
setArgs()
setArgs(
args: ComposeFieldConfigArgumentMap
): Resolver;
setArg()
setArg(
argName: string,
argConfig: ComposeArgumentConfig
): Resolver;
extendArg()
extendArg(
argName: string,
partialArgConfig: any
): Resolver;
addArgs()
addArgs(
newArgs: ComposeFieldConfigArgumentMap
): Resolver;
removeArg()
removeArg(
argNameOrArray: string | Array<string>
): Resolver;
removeOtherArgs()
removeOtherArgs(
argNameOrArray: string | Array<string>
): Resolver;
reorderArgs()
reorderArgs(
names: Array<string>
): Resolver;
cloneArg()
cloneArg(
argName: string,
newTypeName: string
): Resolver;
isRequired()
isRequired(
argName: string
): boolean;
makeRequired()
makeRequired(
argNameOrArray: string | Array<string>
): Resolver;
makeOptional()
makeOptional(
argNameOrArray: string | Array<string>
): Resolver;
addFilterArg()
addFilterArg(
opts: ResolverFilterArgConfig<TSource, TContext>
): Resolver<TSource, TContext>;
addSortArg()
addSortArg(
opts: ResolverSortArgConfig<TSource, TContext>
): Resolver<TSource, TContext>;
Resolve methods
getResolve()
getResolve(): ResolverRpCb<TSource, TContext>;
setResolve()
setResolve(
resolve: ResolverRpCb<TSource, TContext>
): Resolver<TSource, TContext>;
Wrap methods
withMiddlewares()
withMiddlewares(
middlewares: Array<ResolverMiddleware,
): Resolver;
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(
cb: ResolverWrapCb<TSource, TContext> | null,
newResolverOpts: ResolverOpts<TSource, TContext> | null
): Resolver<TSource, TContext>;
wrapResolve()
wrapResolve(
cb: ResolverNextRpCb<TSource, TContext>,
wrapperName?: string
): Resolver<TSource, TContext>;
wrapArgs()
wrapArgs(
cb: ResolverWrapArgsCb,
wrapperName?: string
): Resolver<TSource, TContext>;
wrapCloneArg()
wrapCloneArg(
argName: string,
newTypeName: string
): Resolver<TSource, TContext>;
wrapType()
wrapType(
cb: ResolverWrapTypeCb,
wrapperName?: string
): Resolver<TSource, TContext>;
Misc methods
getFieldConfig()
getFieldConfig(
opts?: { projection?: ProjectionType }
): GraphQLFieldConfig<TSource, TContext>;
getKind()
getKind(): ResolverKinds | null;
setKind()
setKind(
kind: string
): Resolver;
getDescription()
getDescription(): string | null;
setDescription()
setDescription(
description: string
): Resolver;
get()
get(
path: string | Array<string>
): any;
clone()
clone(
opts?: ResolverOpts<TSource, TContext>
): Resolver<TSource, TContext>;
Debug methods
getNestedName()
getNestedName(): string;
toString()
toString(
colors?: boolean
): string;
setDisplayName()
setDisplayName(
name: string
): Resolver;
toDebugStructure()
toDebugStructure(
colors?: boolean
): Object;
debugExecTime()
debugExecTime(): Resolver<TSource, TContext>;
debugParams()
debugParams(
filterPaths: (string | Array<string>) | null,
opts?: ResolveDebugOpts
): Resolver<TSource, TContext>;
debugPayload()
debugPayload(
filterPaths: (string | Array<string>) | null,
opts?: ResolveDebugOpts
): Resolver<TSource, TContext>;
debug()
debug(
filterDotPaths?: {
params?: (string | Array<string>),
payload?: (string | Array<string>)
},
opts?: ResolveDebugOpts
): Resolver<TSource, TContext>;
Internal type definitions
Flowtype definitions which are used in this class.
ResolveParams<TSource, TContext>
type ResolveParams<TSource, TContext> = {
source: TSource,
args: { [argName: string]: any },
context: TContext,
info: GraphQLResolveInfo,
projection: $Shape<ProjectionType>,
[opt: string]: any,
};
ResolverKinds
type ResolverKinds = 'query' | 'mutation' | 'subscription';
ResolverFilterArgFn<TSource, TContext>
type ResolverFilterArgFn<TSource, TContext> = (
query: any,
value: any,
resolveParams: ResolveParams<TSource, TContext>
) => any;
ResolverFilterArgConfig<TSource, TContext>
type ResolverFilterArgConfig<TSource, TContext> = {
+name: string,
+type: ComposeArgumentType,
+description?: ?string,
+query?: ResolverFilterArgFn<TSource, TContext>,
+filterTypeNameFallback?: string,
+defaultValue?: any,
};
ResolverSortArgFn<TSource, TContext>
type ResolverSortArgFn<TSource, TContext> = (
resolveParams: ResolveParams<TSource, TContext>
) => mixed;
ResolverSortArgConfig<TSource, TContext>
type ResolverSortArgConfig<TSource, TContext> = {
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>
| string
| number
| boolean
| Array<any>,
deprecationReason?: ?string,
description?: ?string,
};
ResolverOpts<TSource, TContext>
type ResolverOpts<TSource, TContext> = {|
type?: ComposeOutputType<TContext>,
resolve?: ResolverRpCb<TSource, TContext>,
args?: ComposeFieldConfigArgumentMap,
name?: string,
displayName?: string,
kind?: ResolverKinds,
description?: string,
parent?: Resolver<TSource, TContext>,
|};
ResolverWrapCb<TSource, TContext>
type ResolverWrapCb<TSource, TContext> = (
newResolver: Resolver<TSource, TContext>,
prevResolver: Resolver<TSource, TContext>
) => Resolver<TSource, TContext>;
ResolverRpCb<TSource, TContext>
type ResolverRpCb<TSource, TContext> = (
resolveParams: $Shape<ResolveParams<TSource, TContext>>
) => Promise<any> | any;
ResolverNextRpCb<TSource, TContext>
type ResolverNextRpCb<TSource, TContext> = (
next: ResolverRpCb<TSource, TContext>
) => ResolverRpCb<TSource, TContext>;
ResolverWrapArgsCb
type ResolverWrapArgsCb = (
prevArgs: GraphQLFieldConfigArgumentMap
) => ComposeFieldConfigArgumentMap;
ResolverWrapTypeCb
type ResolverWrapTypeCb<TContext> = (
prevType: GraphQLOutputType
) => ComposeOutputType<TContext>;
ResolveDebugOpts
type ResolveDebugOpts = {
showHidden?: boolean,
depth?: number,
colors?: boolean,
};