InterfaceTypeComposer
Class that helps to create GraphQLInterfaceType
s and provide ability to modify them.
Static methods
static create()
static create<TSrc = any, TCtx = any>(
typeDef: InterfaceTypeComposerDefinition<TSrc, TCtx>,
schemaComposer: SchemaComposer<TCtx>
): InterfaceTypeComposer<TSrc, TCtx>
Create InterfaceTypeComposer
with adding it by name to the SchemaComposer
.
static createTemp()
static createTemp<TSrc = any, TCtx = any>(
typeDef: InterfaceTypeComposerDefinition<TSrc, TCtx>,
schemaComposer: SchemaComposer<TCtx>
): InterfaceTypeComposer<TSrc, TCtx>
Create InterfaceTypeComposer
without adding it to the SchemaComposer
. This method may be usefull in plugins, when you need to create type temporary.
Getters
List
List: ListComposer<InterfaceTypeComposer<TSource, TContext>>;
Get Type wrapped in List modifier
const UserTC = schemaComposer.createInterfaceTC(
`interface UserIface { name: String }`
);
schemaComposer.Query.addFields({
users1: { type: UserTC.List }, // in SDL: users1: [UserIface]
users2: { type: UserTC.NonNull.List }, // in SDL: users2: [UserIface!]
users3: { type: UserTC.NonNull.List.NonNull } // in SDL: users2: [UserIface!]!
});
NonNull
NonNull: NonNullComposer<InterfaceTypeComposer<TSource, TContext>>;
Get Type wrapped in NonNull modifier
const UserTC = schemaComposer.createInterfaceTC(
`interface UserIface { name: String }`
);
schemaComposer.Query.addFields({
users1: { type: UserTC.List }, // in SDL: users1: [UserIface]
users2: { type: UserTC.NonNull.List }, // in SDL: users2: [UserIface!]!
users3: { type: UserTC.NonNull.List.NonNull } // in SDL: users2: [UserIface!]!
});
Properties
schemaComposer
schemaComposer: SchemaComposer<TContext>;
Field methods
getFields()
getFields(): ObjectTypeComposerFieldConfigMap<TSource, TContext>
getFieldNames()
getFieldNames(): string[]
getField()
getField(
fieldName: string
): ObjectTypeComposerFieldConfig<TSource, TContext>
hasField()
hasField(
fieldName: string
): boolean
setFields()
setFields(
fields: ObjectTypeComposerFieldConfigMapDefinition<TSource, TContext>
): this
setField()
setField(
fieldName: string,
fieldConfig: ObjectTypeComposerFieldConfigDefinition<TSource, TContext>
): this
addFields()
addFields(
newFields: ObjectTypeComposerFieldConfigMapDefinition<TSource, TContext>
): this
Add new fields or replace existed in a GraphQL type
removeField()
removeField(
fieldNameOrArray: string | string[]
): this
Remove fields from type by name or array of names. You also may pass name in dot-notation, in such case will be removed nested field.
removeField('field1'); // remove 1 field
removeField(['field1', 'field2']); // remove 2 fields
removeField('field1.subField1'); // remove 1 nested field
removeOtherFields()
removeOtherFields(
fieldNameOrArray: string | string[]
): this
reorderFields()
reorderFields(
names: string[]
): this
extendField()
extendField(
fieldName: string,
partialFieldConfig: Partial<ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext>>
): 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 = any>(
fieldName: string
): ObjectTypeComposerArgumentConfigMap<TArgs>
getFieldArgNames()
getFieldArgNames(
fieldName: string
): string[]
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(): GraphQLInterfaceType
getTypePlural()
getTypePlural(): ListComposer<InterfaceTypeComposer<TSource, TContext>>
getTypeNonNull()
getTypeNonNull(): NonNullComposer<InterfaceTypeComposer<TSource, TContext>>
getTypeName()
getTypeName(): string
setTypeName()
setTypeName(
name: string
): this
getDescription()
getDescription(): string
setDescription()
setDescription(
description: string
): this
clone()
clone(
newTypeNameOrTC: string | InterfaceTypeComposer<any, any>
): InterfaceTypeComposer<TSource, 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>
): InterfaceTypeComposer<any, any>
Clone this type to another SchemaComposer. Also will be cloned all sub-types.
merge()
merge(
type: GraphQLInterfaceType | GraphQLObjectType | InterfaceTypeComposer<any, any> | ObjectTypeComposer<any, any>
): this
InputType methods
getInputType()
getInputType(): GraphQLInputObjectType
hasInputTypeComposer()
hasInputTypeComposer(): boolean
setInputTypeComposer()
setInputTypeComposer(
itc: InputTypeComposer<TContext>
): this
getInputTypeComposer()
getInputTypeComposer(
opts: ToInputTypeOpts
): InputTypeComposer<TContext>
getITC()
getITC(
opts: ToInputTypeOpts
): InputTypeComposer<TContext>
An alias for getInputTypeComposer()
removeInputTypeComposer()
removeInputTypeComposer(): this
ResolveType methods
getResolveType()
getResolveType(): GraphQLTypeResolver<TSource, TContext> | undefined | null
setResolveType()
setResolveType(
fn: GraphQLTypeResolver<TSource, TContext> | undefined | null
): this
hasTypeResolver()
hasTypeResolver(
type: ObjectTypeComposer<any, TContext> | GraphQLObjectType
): boolean
getTypeResolvers()
getTypeResolvers(): InterfaceTypeComposerResolversMap<TContext>
getTypeResolverCheckFn()
getTypeResolverCheckFn(
type: ObjectTypeComposer<any, TContext> | GraphQLObjectType
): InterfaceTypeComposerResolverCheckFn<TSource, TContext>
getTypeResolverNames()
getTypeResolverNames(): string[]
getTypeResolverTypes()
getTypeResolverTypes(): GraphQLObjectType[]
setTypeResolvers()
setTypeResolvers(
typeResolversMap: InterfaceTypeComposerResolversMap<TContext>
): this
addTypeResolver()
addTypeResolver<TSrc>(
type: ObjectTypeComposer<TSrc, TContext> | GraphQLObjectType,
checkFn: InterfaceTypeComposerResolverCheckFn<TSrc, TContext>
): this
removeTypeResolver()
removeTypeResolver(
type: ObjectTypeComposer<any, TContext> | GraphQLObjectType
): this
setTypeResolverFallback()
setTypeResolverFallback(
type: ObjectTypeComposer<any, TContext> | GraphQLObjectType | null
): this
Sub-Interface methods
getInterfaces()
getInterfaces(): Array<InterfaceTypeComposerThunked<TSource, TContext>>
getInterfacesTypes()
getInterfacesTypes(): Array<GraphQLInterfaceType>
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>>
): this
removeInterface()
removeInterface(
iface: InterfaceTypeComposerDefinition<any, TContext>
): 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
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
): unknown
hasFieldExtension()
hasFieldExtension(
fieldName: string,
extensionName: string
): boolean
setFieldExtension()
setFieldExtension(
fieldName: string,
extensionName: string,
value: unknown
): 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
): unknown
hasFieldArgExtension()
hasFieldArgExtension(
fieldName: string,
argName: string,
extensionName: string
): boolean
setFieldArgExtension()
setFieldArgExtension(
fieldName: string,
argName: string,
extensionName: string,
value: unknown
): this
removeFieldArgExtension()
removeFieldArgExtension(
fieldName: string,
argName: string,
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
getFieldDirectives()
getFieldDirectives(
fieldName: string
): Array<Directive>
setFieldDirectives()
setFieldDirectives(
fieldName: string,
directives: Array<Directive> | undefined
): this
getFieldDirectiveNames()
getFieldDirectiveNames(
fieldName: string
): string[]
getFieldDirectiveByName()
getFieldDirectiveByName(
fieldName: string,
directiveName: string
): DirectiveArgs | undefined
Returns arguments of first found directive by name. If directive does not exists then will be returned undefined.
setFieldDirectiveByName()
setFieldDirectiveByName(
fieldName: string,
directiveName: string,
args: DirectiveArgs
): this
Set arguments of first found directive by name. If directive does not exists then will be created new one.
getFieldDirectiveById()
getFieldDirectiveById(
fieldName: string,
idx: number
): DirectiveArgs | undefined
getFieldArgDirectives()
getFieldArgDirectives(
fieldName: string,
argName: string
): Array<Directive>
setFieldArgDirectives()
setFieldArgDirectives(
fieldName: string,
argName: string,
directives: Array<Directive>
): this
getFieldArgDirectiveNames()
getFieldArgDirectiveNames(
fieldName: string,
argName: string
): string[]
getFieldArgDirectiveByName()
getFieldArgDirectiveByName(
fieldName: string,
argName: string,
directiveName: string
): DirectiveArgs | undefined
Returns arguments of first found directive by name. If directive does not exists then will be returned undefined.
setFieldArgDirectiveByName()
setFieldArgDirectiveByName(
fieldName: string,
argName: string,
directiveName: string,
args: DirectiveArgs
): this
Set arguments of first found directive by name. If directive does not exists then will be created new one.
getFieldArgDirectiveById()
getFieldArgDirectiveById(
fieldName: string,
argName: string,
idx: number
): DirectiveArgs | undefined
Misc methods
get()
get(
path: string | string[]
): TypeInPath<TContext> | void
getNestedTCs()
getNestedTCs(
opts: {
exclude?: string[];
},
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
InterfaceTypeComposerDefinition
export type InterfaceTypeComposerDefinition<TSource, TContext> =
| TypeAsString
| TypeDefinitionString
| InterfaceTypeComposerAsObjectDefinition<TSource, TContext>
| GraphQLInterfaceType
| Readonly<InterfaceTypeComposerThunked<any, TContext>>;
InterfaceTypeComposerAsObjectDefinition
export type InterfaceTypeComposerAsObjectDefinition<TSource, TContext> = {
name: string;
fields?: ObjectTypeComposerFieldConfigMapDefinition<TSource, TContext>;
interfaces?: null | ThunkWithSchemaComposer<
ReadonlyArray<InterfaceTypeComposerDefinition<any, TContext>>,
SchemaComposer<TContext>
>;
resolveType?: null | GraphQLTypeResolver<TSource, TContext>;
description?: null | string;
extensions?: Extensions;
directives?: Directive[];
};
InterfaceTypeComposerResolversMap
export type InterfaceTypeComposerResolversMap<TContext> = Map<
ObjectTypeComposer<any, TContext> | GraphQLObjectType,
InterfaceTypeComposerResolverCheckFn<any, TContext>
>;
InterfaceTypeComposerResolverCheckFn
export type InterfaceTypeComposerResolverCheckFn<TSource, TContext> = (
value: TSource,
context: TContext,
info: GraphQLResolveInfo
) => MaybePromise<boolean | null | void>;
InterfaceTypeComposerThunked
export type InterfaceTypeComposerThunked<TReturn, TContext> =
| InterfaceTypeComposer<TReturn, TContext>
| ThunkComposer<InterfaceTypeComposer<any, any>, GraphQLInterfaceType>;