UnionTypeComposer
Class that helps to create UnionTypeComposer
s and provide ability to modify them.
Static methods
static create()
static create<TSrc = any, TCtx = any>(
typeDef: UnionTypeComposerDefinition<TSrc, TCtx>,
schemaComposer: SchemaComposer<TCtx>
): UnionTypeComposer<TSrc, TCtx>
Create UnionTypeComposer
with adding it by name to the SchemaComposer
.
static createTemp()
static createTemp<TSrc = any, TCtx = any>(
typeDef: UnionTypeComposerDefinition<TSrc, TCtx>,
schemaComposer: SchemaComposer<TCtx>
): UnionTypeComposer<TSrc, TCtx>
Create UnionTypeComposer
without adding it to the SchemaComposer
. This method may be useful in plugins, when you need to create type temporary.
Getters
List
List: ListComposer<UnionTypeComposer<TSource, TContext>>;
Get Type wrapped in List modifier
const UserTC = schemaComposer.createUnionTC(`union User = Admin | Client`);
schemaComposer.Query.addFields({
users1: { type: UserTC.List }, // in SDL: users1: [User]
users2: { type: UserTC.NonNull.List }, // in SDL: users2: [User!]
users3: { type: UserTC.NonNull.List.NonNull } // in SDL: users2: [User!]!
});
NonNull
NonNull: NonNullComposer<UnionTypeComposer<TSource, TContext>>;
Get Type wrapped in NonNull modifier
const UserTC = schemaComposer.createUnionTC(`union User = Admin | Client`);
schemaComposer.Query.addFields({
users1: { type: UserTC.List }, // in SDL: users1: [User]
users2: { type: UserTC.NonNull.List }, // in SDL: users2: [User!]
users3: { type: UserTC.NonNull.List.NonNull } // in SDL: users2: [User!]!
});
Properties
schemaComposer
schemaComposer: SchemaComposer<TContext>;
Union Types methods
hasType()
hasType(
name: ObjectTypeComposerDefinition<any, TContext>
): boolean
getTypes()
getTypes(): Array<ObjectTypeComposerThunked<TSource, TContext>>
getTypeComposers()
getTypeComposers(): Array<ObjectTypeComposer<TSource, TContext>>
getTypeNames()
getTypeNames(): string[]
clearTypes()
clearTypes(): this
setTypes()
setTypes(
types: ReadonlyArray<ObjectTypeComposerThunked<TSource, TContext> | ObjectTypeComposerDefinition<any, TContext>>
): this
addType()
addType(
type: ObjectTypeComposerThunked<any, TContext> | ObjectTypeComposerDefinition<any, TContext>
): this
addTypes()
addTypes(
types: ReadonlyArray<ObjectTypeComposerThunked<any, TContext> | ObjectTypeComposerDefinition<any, TContext>>
): this
removeType()
removeType(
nameOrArray: string | string[]
): this
removeOtherTypes()
removeOtherTypes(
nameOrArray: string | string[]
): this
Type methods
getType()
getType(): GraphQLUnionType
getTypePlural()
getTypePlural(): ListComposer<UnionTypeComposer<TSource, TContext>>
getTypeNonNull()
getTypeNonNull(): NonNullComposer<UnionTypeComposer<TSource, TContext>>
getTypeName()
getTypeName(): string
setTypeName()
setTypeName(
name: string
): this
getDescription()
getDescription(): string
setDescription()
setDescription(
description: string
): this
clone()
clone(
newTypeNameOrTC: string | UnionTypeComposer<any, any>
): UnionTypeComposer<any, TContext>
You may clone this type with a new provided name as string. Or you may provide a new TypeComposer which will get all cloned settings from this type.
cloneTo()
cloneTo(
anotherSchemaComposer: SchemaComposer<any>,
cloneMap: Map<any, any>
): UnionTypeComposer<any, any>
Clone this type to another SchemaComposer. Also will be cloned all sub-types.
merge()
merge(
type: GraphQLUnionType | UnionTypeComposer<any, any>
): this
ResolveType methods
getResolveType()
getResolveType(): GraphQLTypeResolver<TSource, TContext> | undefined | null
setResolveType()
setResolveType(
fn: GraphQLTypeResolver<TSource, TContext> | undefined | null
): this
hasTypeResolver()
hasTypeResolver(
type: ObjectTypeComposerThunked<any, TContext> | ObjectTypeComposerDefinition<any, TContext>
): boolean
getTypeResolvers()
getTypeResolvers(): UnionTypeComposerResolversMap<TSource, TContext>
getTypeResolverCheckFn()
getTypeResolverCheckFn(
type: ObjectTypeComposerThunked<any, TContext> | ObjectTypeComposerDefinition<any, TContext>
): UnionTypeComposerResolverCheckFn<any, TContext>
getTypeResolverNames()
getTypeResolverNames(): string[]
getTypeResolverTypes()
getTypeResolverTypes(): Array<ObjectTypeComposerThunked<any, TContext>>
setTypeResolvers()
setTypeResolvers(
typeResolversMap: UnionTypeComposerResolversMapDefinition<TSource, TContext>
): this
addTypeResolver()
addTypeResolver(
type: ObjectTypeComposerDefinition<any, TContext>,
checkFn: UnionTypeComposerResolverCheckFn<TSource, TContext>
): this
removeTypeResolver()
removeTypeResolver(
type: ObjectTypeComposerDefinition<any, TContext>
): this
setTypeResolverFallback()
setTypeResolverFallback(
type: ObjectTypeComposer<any, TContext> | GraphQLObjectType | null
): this
Extensions methods
Extensions
is a property on type/field/arg definitions to pass private extra metadata.
It's used only on the server-side with the Code-First approach,
mostly for 3rd party server middlewares & plugins.
Property extensions
may contain private server metadata of any type (even functions)
and does not available via SDL.
@see https://github.com/graphql/graphql-js/issues/1527
@note
If you need to provide public metadata to clients then use directives
instead.
getExtensions()
getExtensions(): Extensions
setExtensions()
setExtensions(
extensions: Extensions | undefined
): 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
Directive methods
Directives provide the ability to work with public metadata which is available via SDL.
Directives can be used on type/field/arg definitions. The most famous directives are
@deprecated(reason: "...")
and @specifiedBy(url: "...")
which are present in GraphQL spec.
GraphQL spec allows to you add any own directives.
@example type Article @directive1 { name @directive2 comments(limit: Int @directive3) }
@note
If you need private metadata then use extensions
instead.
getDirectives()
getDirectives(): Array<Directive>
setDirectives()
setDirectives(
directives: Array<Directive>
): this
getDirectiveNames()
getDirectiveNames(): string[]
getDirectiveByName()
getDirectiveByName(
directiveName: string
): DirectiveArgs | undefined
Returns arguments of first found directive by name. If directive does not exists then will be returned undefined.
setDirectiveByName()
setDirectiveByName(
directiveName: string,
args: DirectiveArgs
): this
Set arguments of first found directive by name. If directive does not exists then will be created new one.
getDirectiveById()
getDirectiveById(
idx: number
): DirectiveArgs | undefined
Misc methods
getNestedTCs()
getNestedTCs(
opts: {
exclude?: string[] | undefined;
},
passedTypes: Set<NamedTypeComposer<any>>
): Set<NamedTypeComposer<any>>
Returns all types which are used inside the current type
toSDL()
toSDL(
opts: SchemaPrinterOptions & {
deep?: boolean;
exclude?: string[];
}
): string
Prints SDL for current type. Or print with all used types if deep: true
option was provided.
Internal type definitions
UnionTypeComposerDefinition
export type UnionTypeComposerDefinition<TSource, TContext> =
| TypeDefinitionString
| TypeNameString
| UnionTypeComposerAsObjectDefinition<TSource, TContext>
| GraphQLUnionType;
UnionTypeComposerAsObjectDefinition
export type UnionTypeComposerAsObjectDefinition<TSource, TContext> = {
name: string;
types?: Thunk<ReadonlyArray<ObjectTypeComposerDefinition<any, TContext>> | null>;
resolveType?: GraphQLTypeResolver<TSource, TContext> | null;
description?: string | null;
extensions?: Extensions;
directives?: Directive[];
};
UnionTypeComposerResolversMap
export type UnionTypeComposerResolversMap<TSource, TContext> = Map<
ObjectTypeComposerThunked<TSource, TContext>,
UnionTypeComposerResolverCheckFn<TSource, TContext>
>;
UnionTypeComposerResolversMapDefinition
export type UnionTypeComposerResolversMapDefinition<TSource, TContext> =
| Map<
ObjectTypeComposerThunked<any, TContext> | ObjectTypeComposerDefinition<any, TContext>,
UnionTypeComposerResolverCheckFn<TSource, TContext>
>
| Readonly<UnionTypeComposerResolversMap<TSource, TContext>>;
UnionTypeComposerResolverCheckFn
export type UnionTypeComposerResolverCheckFn<TSource, TContext> = (
value: TSource,
context: TContext,
info: GraphQLResolveInfo
) => MaybePromise<boolean | null | undefined>;
UnionTypeComposerThunked
export type UnionTypeComposerThunked<TReturn, TContext> =
| UnionTypeComposer<TReturn, TContext>
| ThunkComposer<UnionTypeComposer<TReturn, TContext>, GraphQLUnionType>;