ObjectTypeComposer
Main class that gets GraphQLObjectType
and provide ability to change them.
Static methods
static create()
static create<TSrc = any, TCtx = any>(
typeDef: ObjectTypeComposerDefinition<TSrc, TCtx>,
schemaComposer: SchemaComposer<TCtx>
): ObjectTypeComposer<TSrc, TCtx>
Create ObjectTypeComposer
with adding it by name to the SchemaComposer
.
static createTemp()
static createTemp<TSrc = any, TCtx = any>(
typeDef: ObjectTypeComposerDefinition<TSrc, TCtx>,
schemaComposer: SchemaComposer<TCtx>
): ObjectTypeComposer<TSrc, TCtx>
Create ObjectTypeComposer
without adding it to the SchemaComposer
. This method may be usefull in plugins, when you need to create type temporary.
Properties
schemaComposer
schemaComposer: SchemaComposer<TContext>;
Field methods
getFields()
getFields(): ObjectTypeComposerFieldConfigMap<TSource, TContext>
getFieldNames()
getFieldNames(): string[]
getField()
getField<TArgs = ArgsMap>(
fieldName: string
): ObjectTypeComposerFieldConfig<TSource, TContext, TArgs>
hasField()
hasField(
fieldName: string
): boolean
setFields()
setFields(
fields: ObjectTypeComposerFieldConfigMapDefinition<TSource, TContext>
): this
setField()
setField<TArgs = ArgsMap>(
fieldName: string,
fieldConfig: Thunk<Readonly<ComposeOutputType<TContext>> | ObjectTypeComposerFieldConfigDefinition<TSource, TContext, ArgsMap>>
): this
addFields()
addFields(
newFields: ObjectTypeComposerFieldConfigMapDefinition<TSource, TContext>
): this
Add new fields or replace existed in a GraphQL type
addNestedFields()
addNestedFields(
newFields: ObjectTypeComposerFieldConfigMapDefinition<TSource, TContext>
): this
Add new fields or replace existed (where field name may have dots)
removeField()
removeField(
fieldNameOrArray: string | string[]
): this
removeOtherFields()
removeOtherFields(
fieldNameOrArray: string | string[]
): this
reorderFields()
reorderFields(
names: string[]
): this
extendField()
extendField(
fieldName: string,
partialFieldConfig: Partial<ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext, ArgsMap>>
): this
getFieldConfig()
getFieldConfig(
fieldName: string
): GraphQLFieldConfig<TSource, TContext>
getFieldType()
getFieldType(
fieldName: string
): GraphQLOutputType
getFieldTypeName()
getFieldTypeName(
fieldName: string
): string
getFieldTC()
getFieldTC(
fieldName: string
): ComposeNamedOutputType<TContext>
Automatically unwrap from List, NonNull, ThunkComposer It's important! Cause greatly helps to modify fields types in a real code without manual unwrap writing.
If you need to work with wrappers, you may use the following code:
TC.getField().type
// returns real wrapped TypeComposerTC.isFieldNonNull()
// checks is field NonNull or notTC.makeFieldNonNull()
// for wrapping in NonNullComposerTC.makeFieldNullable()
// for unwrapping from NonNullComposerTC.isFieldPlural()
// checks is field wrapped in ListComposer or notTC.makeFieldPlural()
// for wrapping in ListComposerTC.makeFieldNonPlural()
// for unwrapping from ListComposer
getFieldOTC()
getFieldOTC(
fieldName: string
): ObjectTypeComposer<TSource, TContext>
Alias for getFieldTC()
but returns statically checked ObjectTypeComposer.
If field have other type then error will be thrown.
isFieldNonNull()
isFieldNonNull(
fieldName: string
): boolean
makeFieldNonNull()
makeFieldNonNull(
fieldNameOrArray: string | string[]
): this
makeFieldNullable()
makeFieldNullable(
fieldNameOrArray: string | string[]
): this
isFieldPlural()
isFieldPlural(
fieldName: string
): boolean
makeFieldPlural()
makeFieldPlural(
fieldNameOrArray: string | string[]
): this
makeFieldNonPlural()
makeFieldNonPlural(
fieldNameOrArray: string | string[]
): this
deprecateFields()
deprecateFields(
fields: {
[fieldName: string]: string;
} | string[] | string
): this
getFieldArgs()
getFieldArgs<TArgs = ArgsMap>(
fieldName: string
): ObjectTypeComposerArgumentConfigMap<TArgs>
hasFieldArg()
hasFieldArg(
fieldName: string,
argName: string
): boolean
getFieldArg()
getFieldArg(
fieldName: string,
argName: string
): ObjectTypeComposerArgumentConfig
getFieldArgType()
getFieldArgType(
fieldName: string,
argName: string
): GraphQLInputType
getFieldArgTypeName()
getFieldArgTypeName(
fieldName: string,
argName: string
): string
getFieldArgTC()
getFieldArgTC(
fieldName: string,
argName: string
): ComposeNamedInputType<TContext>
Automatically unwrap from List, NonNull, ThunkComposer It's important! Cause greatly helps to modify args types in a real code without manual unwrap writing.
If you need to work with wrappers, you may use the following code:
isFieldArgPlural()
โ checks is arg wrapped in ListComposer or not
makeFieldArgPlural()
โ for arg wrapping in ListComposer
makeFieldArgNonPlural()
โ for arg unwrapping from ListComposer
isFieldArgNonNull()
โ checks is arg wrapped in NonNullComposer or not
makeFieldArgNonNull()
โ for arg wrapping in NonNullComposer
makeFieldArgNullable()
โ for arg unwrapping from NonNullComposer
getFieldArgITC()
getFieldArgITC(
fieldName: string,
argName: string
): InputTypeComposer<TContext>
Alias for getFieldArgTC()
but returns statically checked InputTypeComposer.
If field have other type then error will be thrown.
setFieldArgs()
setFieldArgs(
fieldName: string,
args: ObjectTypeComposerArgumentConfigMapDefinition<any>
): this
addFieldArgs()
addFieldArgs(
fieldName: string,
newArgs: ObjectTypeComposerArgumentConfigMapDefinition<any>
): this
setFieldArg()
setFieldArg(
fieldName: string,
argName: string,
argConfig: ObjectTypeComposerArgumentConfigDefinition
): this
isFieldArgPlural()
isFieldArgPlural(
fieldName: string,
argName: string
): boolean
makeFieldArgPlural()
makeFieldArgPlural(
fieldName: string,
argNameOrArray: string | string[]
): this
makeFieldArgNonPlural()
makeFieldArgNonPlural(
fieldName: string,
argNameOrArray: string | string[]
): this
isFieldArgNonNull()
isFieldArgNonNull(
fieldName: string,
argName: string
): boolean
makeFieldArgNonNull()
makeFieldArgNonNull(
fieldName: string,
argNameOrArray: string | string[]
): this
makeFieldArgNullable()
makeFieldArgNullable(
fieldName: string,
argNameOrArray: string | string[]
): this
Type methods
getType()
getType(): GraphQLObjectType
getTypePlural()
getTypePlural(): ListComposer<this>
getTypeNonNull()
getTypeNonNull(): NonNullComposer<this>
getTypeName()
getTypeName(): string
setTypeName()
setTypeName(
name: string
): this
getDescription()
getDescription(): string
setDescription()
setDescription(
description: string
): this
clone()
clone<TCloneSource = TSource>(
newTypeNameOrTC: string | ObjectTypeComposer<any, any>
): ObjectTypeComposer<TCloneSource, TContext>
You may clone this type with a new provided name as string. Or you may provide a new TypeComposer which will get all clonned settings from this type.
getIsTypeOf()
getIsTypeOf(): GraphQLIsTypeOfFn<TSource, TContext> | null | void
setIsTypeOf()
setIsTypeOf(
fn: GraphQLIsTypeOfFn<any, any> | null | void
): this
merge()
merge(
type: GraphQLObjectType | GraphQLInterfaceType | ObjectTypeComposer<any, any> | InterfaceTypeComposer<any, any>
): this
Merge fields and interfaces from provided GraphQLObjectType
, or ObjectTypeComposer
.
Also you may provide GraphQLInterfaceType
or InterfaceTypeComposer
for adding fields.
InputType methods
getInputType()
getInputType(): GraphQLInputObjectType
hasInputTypeComposer()
hasInputTypeComposer(): boolean
setInputTypeComposer()
setInputTypeComposer(
itc: InputTypeComposer<TContext>
): this
getInputTypeComposer()
getInputTypeComposer(): InputTypeComposer<TContext>
getITC()
getITC(): InputTypeComposer<TContext>
removeInputTypeComposer()
removeInputTypeComposer(): this
Resolver methods
getResolvers()
getResolvers(): Map<string, Resolver<any, TContext, any>>
hasResolver()
hasResolver(
name: string
): boolean
getResolver()
getResolver<TResolverSource = any, TArgs = ArgsMap>(
name: string,
// type ResolverMiddleware = (resolve, source, args, context, info) => any;
middlewares: Array<ResolverMiddleware<TResolverSource, TContext, TArgs>>
): Resolver<TResolverSource, TContext, TArgs>
Returns existed Resolver by name.
Resolver may be additionally wrapped 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<TResolverSource = any, TArgs = ArgsMap>(
name: string,
resolver: Resolver<TResolverSource, TContext, TArgs>
): this
addResolver()
addResolver<TResolverSource = any, TArgs = ArgsMap>(
opts: Resolver<TResolverSource, TContext, TArgs> | ResolverDefinition<TResolverSource, TContext, TArgs>
): this
removeResolver()
removeResolver(
resolverName: string
): this
wrapResolver()
wrapResolver<TResolverSource = any, TArgs = ArgsMap>(
resolverName: string,
cbResolver: ResolverWrapCb<TResolverSource, TSource, TContext, TArgs>
): this
wrapResolverAs()
wrapResolverAs<TResolverSource = any, TArgs = ArgsMap>(
resolverName: string,
fromResolverName: string,
cbResolver: ResolverWrapCb<TResolverSource, TSource, TContext, TArgs>
): this
wrapResolverResolve()
wrapResolverResolve<TResolverSource = any, TArgs = ArgsMap>(
resolverName: string,
cbNextRp: ResolverNextRpCb<TResolverSource, TContext, TArgs>
): this
Interface methods
getInterfaces()
getInterfaces(): Array<InterfaceTypeComposerThunked<TSource, TContext>>
setInterfaces()
setInterfaces(
interfaces: ReadonlyArray<InterfaceTypeComposerDefinition<any, TContext>>
): this
hasInterface()
hasInterface(
iface: InterfaceTypeComposerDefinition<any, TContext>
): boolean
addInterface()
addInterface(
iface: InterfaceTypeComposerDefinition<any, TContext> | InterfaceTypeComposerThunked<any, TContext>
): this
addInterfaces()
addInterfaces(
ifaces: ReadonlyArray<InterfaceTypeComposerDefinition<any, TContext> | InterfaceTypeComposerThunked<any, TContext>>
): ObjectTypeComposer<TSource, TContext>
removeInterface()
removeInterface(
iface: InterfaceTypeComposerDefinition<any, TContext>
): this
Extensions methods
getExtensions()
getExtensions(): Extensions
setExtensions()
setExtensions(
extensions: Extensions
): this
extendExtensions()
extendExtensions(
extensions: Extensions
): this
clearExtensions()
clearExtensions(): this
getExtension()
getExtension(
extensionName: string
): any
hasExtension()
hasExtension(
extensionName: string
): boolean
setExtension()
setExtension(
extensionName: string,
value: any
): this
removeExtension()
removeExtension(
extensionName: string
): this
getFieldExtensions()
getFieldExtensions(
fieldName: string
): Extensions
setFieldExtensions()
setFieldExtensions(
fieldName: string,
extensions: Extensions
): this
extendFieldExtensions()
extendFieldExtensions(
fieldName: string,
extensions: Extensions
): this
clearFieldExtensions()
clearFieldExtensions(
fieldName: string
): this
getFieldExtension()
getFieldExtension(
fieldName: string,
extensionName: string
): any
hasFieldExtension()
hasFieldExtension(
fieldName: string,
extensionName: string
): boolean
setFieldExtension()
setFieldExtension(
fieldName: string,
extensionName: string,
value: any
): this
removeFieldExtension()
removeFieldExtension(
fieldName: string,
extensionName: string
): this
getFieldArgExtensions()
getFieldArgExtensions(
fieldName: string,
argName: string
): Extensions
setFieldArgExtensions()
setFieldArgExtensions(
fieldName: string,
argName: string,
extensions: Extensions
): this
extendFieldArgExtensions()
extendFieldArgExtensions(
fieldName: string,
argName: string,
extensions: Extensions
): this
clearFieldArgExtensions()
clearFieldArgExtensions(
fieldName: string,
argName: string
): this
getFieldArgExtension()
getFieldArgExtension(
fieldName: string,
argName: string,
extensionName: string
): any
hasFieldArgExtension()
hasFieldArgExtension(
fieldName: string,
argName: string,
extensionName: string
): boolean
setFieldArgExtension()
setFieldArgExtension(
fieldName: string,
argName: string,
extensionName: string,
value: any
): this
removeFieldArgExtension()
removeFieldArgExtension(
fieldName: string,
argName: string,
extensionName: string
): this
Directive methods
Directive methods are usefull if you declare your schemas via SDL.
Users who actively use graphql-tools
can open new abilities for writing
your own directive handlers.
If you create your schemas via config objects, then probably you
no need in directives
. Instead directives better to use extensions
.
getDirectives()
getDirectives(): ExtensionsDirective[]
getDirectiveNames()
getDirectiveNames(): string[]
getDirectiveByName()
getDirectiveByName(
directiveName: string
): DirectiveArgs | void
getDirectiveById()
getDirectiveById(
idx: number
): DirectiveArgs | void
getFieldDirectives()
getFieldDirectives(
fieldName: string
): ExtensionsDirective[]
getFieldDirectiveNames()
getFieldDirectiveNames(
fieldName: string
): string[]
getFieldDirectiveByName()
getFieldDirectiveByName(
fieldName: string,
directiveName: string
): DirectiveArgs | void
getFieldDirectiveById()
getFieldDirectiveById(
fieldName: string,
idx: number
): DirectiveArgs | void
getFieldArgDirectives()
getFieldArgDirectives(
fieldName: string,
argName: string
): ExtensionsDirective[]
getFieldArgDirectiveNames()
getFieldArgDirectiveNames(
fieldName: string,
argName: string
): string[]
getFieldArgDirectiveByName()
getFieldArgDirectiveByName(
fieldName: string,
argName: string,
directiveName: string
): DirectiveArgs | void
getFieldArgDirectiveById()
getFieldArgDirectiveById(
fieldName: string,
argName: string,
idx: number
): DirectiveArgs | void
Misc methods
addRelation()
addRelation<TRelationSource = any, TArgs = ArgsMap>(
fieldName: string,
ObjectTypeComposerRelationOpts: Readonly<ObjectTypeComposerRelationOpts<TRelationSource, TSource, TContext, TArgs>>
): this
getRelations()
getRelations(): ObjectTypeComposerRelationThunkMap<any, TContext>
setRecordIdFn()
setRecordIdFn(
fn: ObjectTypeComposerGetRecordIdFn<TSource, TContext>
): this
hasRecordIdFn()
hasRecordIdFn(): boolean
getRecordIdFn()
getRecordIdFn(): ObjectTypeComposerGetRecordIdFn<TSource, TContext>
getRecordId()
getRecordId(
source: TSource,
args: ArgsMap,
context: TContext
): string | number
Get function that returns record id, from provided object.
get()
get(
path: string | string[]
): TypeInPath<TContext> | void
Internal type definitions
ObjectTypeComposerDefinition
export type ObjectTypeComposerDefinition<TSource, TContext> =
| TypeAsString
| TypeDefinitionString
| ObjectTypeComposerAsObjectDefinition<TSource, TContext>
| Readonly<ObjectTypeComposer<TSource, TContext>>
| Readonly<GraphQLObjectType>;
ObjectTypeComposerAsObjectDefinition
export type ObjectTypeComposerAsObjectDefinition<TSource, TContext> = {
name: string;
interfaces?: null | Thunk<ReadonlyArray<InterfaceTypeComposerDefinition<any, TContext>>>;
fields?: ObjectTypeComposerFieldConfigMapDefinition<TSource, TContext>;
isTypeOf?: null | GraphQLIsTypeOfFn<TSource, TContext>;
description?: string | null;
isIntrospection?: boolean;
extensions?: Extensions;
};
ObjectTypeComposerFieldConfigMap
export type ObjectTypeComposerFieldConfigMap<TSource, TContext> = ObjMap<
ObjectTypeComposerFieldConfig<TSource, TContext>
>;
ObjectTypeComposerFieldConfigMapDefinition
export type ObjectTypeComposerFieldConfigMapDefinition<TSource, TContext> = ObjMapReadOnly<
Thunk<ObjectTypeComposerFieldConfigDefinition<TSource, TContext>>
>;
ObjectTypeComposerFieldConfigDefinition
export type ObjectTypeComposerFieldConfigDefinition<TSource, TContext, TArgs = ArgsMap> =
| ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext, TArgs>
| ComposeOutputTypeDefinition<TContext>
| Readonly<Resolver<any, TContext, any>>
| Readonly<ComposeOutputType<TContext>>;
ObjectTypeComposerFieldConfigAsObjectDefinition
export type ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext, TArgs = ArgsMap> = {
type: Thunk<ComposeOutputTypeDefinition<TContext> | Readonly<Resolver<any, TContext, any>>>;
args?: ObjectTypeComposerArgumentConfigMapDefinition<TArgs>;
resolve?: GraphQLFieldResolver<TSource, TContext, TArgs>;
subscribe?: GraphQLFieldResolver<TSource, TContext>;
deprecationReason?: string | null;
description?: string | null;
extensions?: Extensions;
[key: string]: any;
};
ObjectTypeComposerFieldConfig
export type ObjectTypeComposerFieldConfig<TSource, TContext, TArgs = ArgsMap> = {
type: ComposeOutputType<TContext>;
args?: ObjectTypeComposerArgumentConfigMap<TArgs>;
resolve?: GraphQLFieldResolver<TSource, TContext, TArgs>;
subscribe?: GraphQLFieldResolver<TSource, TContext>;
deprecationReason?: string | null;
description?: string | null;
astNode?: FieldDefinitionNode | null;
extensions?: Extensions;
[key: string]: any;
};
ArgsMap
// Compose Args -----------------------------
export type ArgsMap = { [argName: string]: any };
ObjectTypeComposerArgumentConfigMap
export type ObjectTypeComposerArgumentConfigMap<TArgs = ArgsMap> = {
[argName in keyof TArgs]: ObjectTypeComposerArgumentConfig
};
ObjectTypeComposerArgumentConfigMapDefinition
export type ObjectTypeComposerArgumentConfigMapDefinition<TArgs = ArgsMap> = {
[argName in keyof TArgs]: Thunk<ObjectTypeComposerArgumentConfigDefinition>
};
ObjectTypeComposerArgumentConfigAsObjectDefinition
export type ObjectTypeComposerArgumentConfigAsObjectDefinition = {
type: Thunk<ComposeInputTypeDefinition>;
defaultValue?: any;
description?: string | null;
extensions?: Extensions;
[key: string]: any;
};
ObjectTypeComposerArgumentConfig
export type ObjectTypeComposerArgumentConfig = {
type: ComposeInputType;
defaultValue?: any;
description?: string | null;
astNode?: InputValueDefinitionNode | null;
extensions?: Extensions;
[key: string]: any;
};
ObjectTypeComposerArgumentConfigDefinition
export type ObjectTypeComposerArgumentConfigDefinition =
| ObjectTypeComposerArgumentConfigAsObjectDefinition
| ComposeInputTypeDefinition;
ObjectTypeComposerRelationThunkMap
// RELATION -----------------------------
export type ObjectTypeComposerRelationThunkMap<TSource, TContext> = {
[fieldName: string]: Thunk<ObjectTypeComposerRelationOpts<any, TSource, TContext, ArgsMap>>;
};
ObjectTypeComposerRelationOpts
export type ObjectTypeComposerRelationOpts<TRelationSource, TSource, TContext, TArgs = ArgsMap> =
| ObjectTypeComposerRelationOptsWithResolver<TRelationSource, TSource, TContext, TArgs>
| ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext, TArgs>;
ObjectTypeComposerRelationOptsWithResolver
export type ObjectTypeComposerRelationOptsWithResolver<
TRelationSource,
TSource,
TContext,
TArgs = ArgsMap
> = {
resolver: Thunk<Resolver<TRelationSource, TContext, TArgs>>;
prepareArgs?: ObjectTypeComposerRelationArgsMapper<TSource, TContext, TArgs>;
projection?: ProjectionType;
description?: string | null;
deprecationReason?: string | null;
catchErrors?: boolean;
};
ObjectTypeComposerRelationArgsMapperFn
export type ObjectTypeComposerRelationArgsMapperFn<TSource, TContext, TArgs = ArgsMap> = (
source: TSource,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo
) => any;
ObjectTypeComposerRelationArgsMapper
export type ObjectTypeComposerRelationArgsMapper<TSource, TContext, TArgs = ArgsMap> = {
[argName: string]:
| { [key: string]: any }
| ObjectTypeComposerRelationArgsMapperFn<TSource, TContext, TArgs>
| null
| void
| string
| number
| any[];
};
ObjectTypeComposerGetRecordIdFn
export type ObjectTypeComposerGetRecordIdFn<TSource, TContext> = (
source: TSource,
args?: ArgsMap,
context?: TContext
) => string;
ObjectTypeComposerThunked
export type ObjectTypeComposerThunked<TReturn, TContext> =
| ObjectTypeComposer<TReturn, TContext>
| ThunkComposer<ObjectTypeComposer<TReturn, TContext>, GraphQLObjectType>;