TypeComposer
Main class that gets GraphQLObjectType
and provide ability to change them.
Static methods
static create()
Create TypeComposer
with adding it by name to the SchemaComposer
.
static create<TCtx>(
opts:
| TypeAsString
| ComposeObjectTypeConfig<any, TCtx>
| GraphQLObjectType
): TypeComposer<TCtx>;
static createTemp()
Create TypeComposer
without adding it to the SchemaComposer
. This method may be usefull in plugins, when you need to create type temporary.
static createTemp<TCtx>(
opts:
| TypeAsString
| ComposeObjectTypeConfig<any, TCtx>
| GraphQLObjectType
): TypeComposer<TCtx>;
Properties
schemaComposer
Current SchemaComposer
instance which is used for storing types created by TypeComposer
.
SomeTC.schemaComposer: SchemaComposer;
Field methods
getFields()
getFields(): GraphQLFieldConfigMap<any, TContext>;
getFieldNames()
getFieldNames(): Array<string>;
setFields()
setFields(
fields:
| ComposeFieldConfigMap<any, TContext>
| GraphQLFieldConfigMap<any, TContext>
): TypeComposer;
hasField()
hasField(
fieldName: string
): boolean;
setField()
setField<TSource, TContext>(
fieldName: string,
fieldConfig: ComposeFieldConfig<TSource, TContext>
): TypeComposer;
addFields()
Add new fields or replace existed in a GraphQL type.
addFields(
newFields: ComposeFieldConfigMap<any, TContext>
): TypeComposer;
addNestedFields()
Add new fields or replace existed (where field name may have dots).
addNestedFields(
newFields: ComposeFieldConfigMap<any, TContext>
): TypeComposer;
getField()
Get fieldConfig by name.
getField(
fieldName: string
): ComposeFieldConfig<any, TContext>;
removeField()
removeField(
fieldNameOrArray: string | Array<string>
): TypeComposer;
removeOtherFields()
removeOtherFields(
fieldNameOrArray: string | Array<string>
): TypeComposer;
extendField()
extendField(
fieldName: string,
parialFieldConfig: ComposeFieldConfig<any, TContext>
): TypeComposer;
reorderFields()
reorderFields(
names: Array<string>
): TypeComposer;
getFieldConfig()
getFieldConfig(
fieldName: string
): GraphQLFieldConfig<any, TContext>;
getFieldType()
getFieldType(
fieldName: string
): GraphQLOutputType;
getFieldTC()
getFieldTC(
fieldName: string
): TypeComposer<TContext>;
isFieldNonNull()
isFieldNonNull(
fieldName: string
): boolean;
makeFieldNonNull()
makeFieldNonNull(
fieldNameOrArray: string | Array<string>
): TypeComposer<TContext>;
makeFieldNullable()
makeFieldNullable(
fieldNameOrArray: string | Array<string>
): TypeComposer<TContext>;
deprecateFields()
deprecateFields(
fields:
| { [fieldName: string]: string }
| Array<string>
| string
): TypeComposer<TContext>;
getFieldArgs()
getFieldArgs(
fieldName: string
): GraphQLFieldConfigArgumentMap;
hasFieldArg()
hasFieldArg(
fieldName: string,
argName: string
): boolean;
getFieldArg()
getFieldArg(
fieldName: string,
argName: string
): GraphQLArgumentConfig;
getFieldArgType()
getFieldArgType(
fieldName: string,
argName: string
): GraphQLInputType;
Type methods
getType()
getType(): GraphQLObjectType;
getTypePlural()
getTypePlural(): GraphQLList<GraphQLObjectType>;
getTypeNonNull()
getTypeNonNull(): GraphQLNonNull<GraphQLObjectType>;
getTypeName()
getTypeName(): string;
setTypeName()
setTypeName(
name: string
): TypeComposer<TContext>;
getDescription()
getDescription(): string;
setDescription()
setDescription(
description: string
): TypeComposer<TContext>;
clone()
clone(
newTypeName: string
): TypeComposer<TContext>;
getIsTypeOf()
getIsTypeOf(): GraphQLIsTypeOfFn<any, TContext> | null | void;
setIsTypeOf()
type GraphQLIsTypeOfFn<TSource, TContext> = (
source: TSource,
context: TContext,
info: GraphQLResolveInfo,
) => MaybePromise<boolean>;
setIsTypeOf(
fn: GraphQLIsTypeOfFn<any, any> | null | void
): this;
InputType methods
getInputType()
getInputType(): GraphQLInputObjectType;
hasInputTypeComposer()
hasInputTypeComposer(): boolean;
setInputTypeComposer()
setInputTypeComposer(
itc: InputTypeComposer
): this;
getInputTypeComposer()
getInputTypeComposer(): InputTypeComposer;
getITC()
An alias for getInputTypeComposer
.
getITC(): InputTypeComposer;
removeInputTypeComposer()
removeInputTypeComposer(): this;
Resolver methods
getResolvers()
getResolvers(): Map<string, Resolver<any, TContext>>;
hasResolver()
hasResolver(
name: string
): boolean;
getResolver()
getResolver(
name: string,
middlewares?: Array<ResolverMiddleware>
): Resolver<any, TContext>;
type ResolverMiddleware = (resolve, source, args, context, info) => any;
You may get existed Resolver with additional wrapping it by middlewares. Eg:
async function authMiddleware(resolve, source, args, context, info) {
if (somehowCheckAuthInContext(context)) {
return resolve(source, args, context, info);
}
throw new Error('You must be authorized');
}
schemaComposer.Query.addFields({
userById: UserTC.getResolver('findById', [authMiddleware]),
userByIds: UserTC.getResolver('findByIds', [authMiddleware]),
});
setResolver()
setResolver(
name: string,
resolver: Resolver<any, TContext>
): TypeComposer;
addResolver()
addResolver(
resolver:
| Resolver<any, TContext>
| {
name: string,
kind: 'query' | 'mutation' | 'subscription',
type: ComposeOutputType<TContext>,
args: ComposeFieldConfigArgumentMap,
resolve: (resolveParams: ResolveParams<TSource, TContext>) => Promise<any> | any,
description?: string,
}
): TypeComposer;
removeResolver()
removeResolver(
resolverName: string
): TypeComposer;
wrapResolver()
wrapResolver(
resolverName: string,
cbResolver: ResolverWrapCb<any, TContext>
): TypeComposer;
wrapResolverAs()
wrapResolverAs(
resolverName: string,
fromResolverName: string,
cbResolver: ResolverWrapCb<any, TContext>
): TypeComposer;
wrapResolverResolve()
wrapResolverResolve(
resolverName: string,
cbNextRp: ResolverNextRpCb<any, TContext>
): TypeComposer;
Interface methods
getInterfaces()
getInterfaces(): Array<InterfaceTypeComposer | GraphQLInterfaceType>;
setInterfaces()
setInterfaces(
interfaces: Array<InterfaceTypeComposer | GraphQLInterfaceType>
): TypeComposer;
hasInterface()
public hasInterface(
iface: string | InterfaceTypeComposer<any, TContext> | GraphQLInterfaceType,
): boolean;
addInterface()
addInterface(
interfaceObj: InterfaceTypeComposer | GraphQLInterfaceType
): TypeComposer;
removeInterface()
removeInterface(
interfaceObj: InterfaceTypeComposer | GraphQLInterfaceType
): TypeComposer;
Misc methods
addRelation()
addRelation(
fieldName: string,
relationOpts: RelationOpts<any, TContext>
): TypeComposer;
getRelations()
getRelations(): RelationThunkMap<any, TContext>;
setRecordIdFn()
setRecordIdFn(
fn: GetRecordIdFn<any, TContext>
): TypeComposer;
hasRecordIdFn()
hasRecordIdFn(): boolean;
getRecordIdFn()
getRecordIdFn(): GetRecordIdFn<any, TContext>;
getRecordId()
Get function that returns record id, from provided object.
getRecordId(
source: any,
args: any,
context: TContext
): string | number;
get()
get(
path:
| string
| Array<string>
): any;
Internal type definitions
Flowtype definitions which are used in this class.
TypeComposerDefinition<TContext> =
| TypeAsString
| ComposeObjectTypeConfig<any, TContext>
| GraphQLObjectType
ComposeObjectTypeConfig<TSource, TContext>
type ComposeObjectTypeConfig<TSource, TContext> = {
name: string,
interfaces?: Thunk<GraphQLInterfaceType[] | null>,
fields?: Thunk<ComposeFieldConfigMap<TSource, TContext>>,
isTypeOf?: GraphQLIsTypeOfFn<TSource, TContext> | null,
description?: string | null,
isIntrospection?: boolean,
};
ComposeFieldConfigMap<TSource, TContext>
type ComposeFieldConfigMap<TSource, TContext> = ObjMap<
ComposeFieldConfig<TSource, TContext>
>;
ComposeFieldConfig<TSource, TContext>
type ComposeFieldConfig<TSource, TContext> =
| ComposeFieldConfigAsObject<TSource, TContext>
| ComposeOutputType<TContext>
| (() => ComposeFieldConfigAsObject<TSource, TContext> | ComposeOutputType<TContext>);
GraphqlFieldConfigExtended<TSource, TContext>
// extended GraphQLFieldConfig
type GraphqlFieldConfigExtended<TSource, TContext> =
GraphQLFieldConfig<TSource, TContext> & { projection?: any };
ComposeFieldConfigAsObject<TSource, TContext>
type ComposeFieldConfigAsObject<TSource, TContext> = {
type: Thunk<ComposeOutputType<TContext>> | GraphQLOutputType,
args?: ComposeFieldConfigArgumentMap,
resolve?: GraphQLFieldResolver<TSource, TContext>,
subscribe?: GraphQLFieldResolver<TSource, TContext>,
deprecationReason?: string | null,
description?: string | null,
astNode?: FieldDefinitionNode | null,
[key: string]: any,
} & { $call?: void };
ComposeOutputType
// extended GraphQLOutputType
type ComposeOutputType<TContext> =
| GraphQLOutputType
| TypeComposer<TContext>
| ScalarTypeComposer
| EnumTypeComposer
| TypeAsString
| Resolver<any, TContext>
| Array<
| GraphQLOutputType
| TypeComposer<TContext>
| ScalarTypeComposer
| EnumTypeComposer
| TypeAsString
| Resolver<any, TContext>
>;
ComposeArgumentType
type ComposeArgumentType =
| GraphQLInputType
| TypeAsString
| InputTypeComposer
| ScalarTypeComposer
| EnumTypeComposer
| Array<
| GraphQLInputType
| TypeAsString
| InputTypeComposer
| ScalarTypeComposer
| EnumTypeComposer
>;
ComposeArgumentConfigAsObject
type ComposeArgumentConfigAsObject = {
type: Thunk<ComposeArgumentType> | GraphQLInputType,
defaultValue?: any,
description?: string | null,
} & { $call?: void };
ComposeArgumentConfig
type ComposeArgumentConfig =
| ComposeArgumentConfigAsObject
| ComposeArgumentType
| (() => ComposeArgumentConfigAsObject | ComposeArgumentType);
type ComposeFieldConfigArgumentMap = {
[argName: string]: ComposeArgumentConfig,
};
RelationThunkMap<TSource, TContext>
type RelationThunkMap<TSource, TContext> = {
[fieldName: string]: Thunk<RelationOpts<TSource, TContext>>,
};
RelationOpts<TSource, TContext>
type RelationOpts<TSource, TContext> =
| RelationOptsWithResolver<TSource, TContext>
| RelationOptsWithFieldConfig<TSource, TContext>;
RelationOptsWithResolver<TSource, TContext>
type RelationOptsWithResolver<TSource, TContext> = {
resolver: Thunk<Resolver<TSource, TContext>>,
prepareArgs?: RelationArgsMapper<TSource, TContext>,
projection?: ProjectionType,
description?: string | null,
deprecationReason?: string | null,
catchErrors?: boolean,
};
RelationOptsWithFieldConfig<TSource, TContext>
type RelationOptsWithFieldConfig<TSource, TContext> =
ComposeFieldConfigAsObject<TSource, TContext> & { resolve: GraphQLFieldResolver<TSource, TContext> };
ArgsType
type ArgsType = { [argName: string]: any };
RelationArgsMapperFn<TSource, TContext>
type RelationArgsMapperFn<TSource, TContext> = (
source: TSource,
args: ArgsType,
context: TContext,
info: GraphQLResolveInfo) => any;
RelationArgsMapper<TSource, TContext>
type RelationArgsMapper<TSource, TContext> = {
[argName: string]: | RelationArgsMapperFn<TSource, TContext>
| null
| void
| string
| number
| any[]
| GenericMap<any>
};