SchemaComposer
SchemaComposer
is a class which helps to create GraphQLSchema
.
Getters
Query
Query: ObjectTypeComposer<any, TContext>;
Returns ObjectTypeComposer
of Query
root type.
import { schemaComposer } from 'graphql-compose';
schemaComposer.Query.addFields({ field1: 'String' });
Mutation
Mutation: ObjectTypeComposer<any, TContext>;
Returns ObjectTypeComposer
of Mutation
root type.
import { schemaComposer } from 'graphql-compose';
schemaComposer.Mutation.addFields({ field1: 'String' });
Subscription
Subscription: ObjectTypeComposer<any, TContext>;
Returns ObjectTypeComposer
of Subscription
root type.
import { schemaComposer } from 'graphql-compose';
schemaComposer.Subscription.addFields({ field1: 'String' });
Properties
typeMapper
typeMapper: TypeMapper<TContext>;
Methods
buildSchema()
buildSchema(
extraConfig: ExtraSchemaConfig
): GraphQLSchema
Create GraphQLSchema
instance from defined types.
This instance can be provided to express-graphql
, apollo-server
, graphql-yoga
etc.
addSchemaMustHaveType()
addSchemaMustHaveType(
type: AnyType<TContext>
): this
When using Interfaces you may have such Types which are hidden under Interface.resolveType method. In such cases you should add these types explicitly. Cause buildSchema()
will take only real used types and types which added via addSchemaMustHaveType()
method.
removeEmptyTypes()
removeEmptyTypes(
tc: ObjectTypeComposer<any, TContext>,
passedTypes: Set<string>
): void
Deeply traverse fields in Query, Mutation, Subscription & sub-objects where will be removed all fields with empty object types (without sub-fields).
clone()
clone(): SchemaComposer<any>
Clone schema with deep cloning of all its types. Except Scalar types which will be the same for both schemas.
merge()
merge(
schema: GraphQLSchema | SchemaComposer<any>
): this
Load all types from GraphQLSchema and merge with current SchemaComposer's types.
import { schemaComposer } from 'graphql-compose';
schemaComposer.merge(someSchema1);
schemaComposer.merge(someSchema2);
schemaComposer.merge(someSchema3);
schemaComposer.getOTC('User').removeField('password');
const newSchema = schemaComposer.buildSchema();
getDescription()
getDescription(): string | undefined
setDescription()
setDescription(
description: string | undefined
): this
Like graphql-tools methods
addTypeDefs()
addTypeDefs(
typeDefs: string
): TypeStorage<string, NamedTypeComposer<any>>
Add types to Schema via SDL string. Returns a Map of parsed types.
const schemaComposer = new SchemaComposer();
schemaComposer.addTypeDefs(`
type Post {
id: Int!
title: String
votes: Int
}
enum Sort {
ASC
DESC
}
`);
After that your added types will be available for referencing via string, eg.
ObjectTypeComposer.create({
name: 'Author',
fields: {
posts: {
type: '[Post!]',
args: {
sort: 'Sort'
},
resolve: () => {}
}
}
});
addResolveMethods()
addResolveMethods(
typesFieldsResolve: GraphQLToolsResolveMethods<TContext>
): void
Define resolve
methods for Types in graphql-tools
manner.
declare function addResolveMethods(typesFieldsResolve: {
[typeName: string]: {
[fieldName: string]: (
source: any,
args: Object,
context: TContext,
info: GraphQLResolveInfo
) => any;
};
}): void;
More details can be found in issue #142.
Type methods
createObjectTC()
createObjectTC<TSource = any>(
typeDef: ObjectTypeComposerDefinition<TSource, TContext>
): ObjectTypeComposer<TSource, TContext>
createInputTC()
createInputTC(
typeDef: InputTypeComposerDefinition
): InputTypeComposer<TContext>
createEnumTC()
createEnumTC(
typeDef: EnumTypeComposerDefinition
): EnumTypeComposer<TContext>
createInterfaceTC()
createInterfaceTC<TSource = any>(
typeDef: InterfaceTypeComposerDefinition<TSource, TContext>
): InterfaceTypeComposer<TSource, TContext>
createUnionTC()
createUnionTC<TSource = any>(
typeDef: UnionTypeComposerDefinition<TSource, TContext>
): UnionTypeComposer<TSource, TContext>
createScalarTC()
createScalarTC<TInternal = any, TExternal = any>(
typeDef: ScalarTypeComposerDefinition<TInternal, TExternal>
): ScalarTypeComposer<TContext>
createResolver()
createResolver<TSource = any, TArgs = any>(
opts: ResolverDefinition<TSource, TContext, TArgs>
): Resolver<any, TContext>
createTC()
createTC(
typeOrSDL: ComposeOutputTypeDefinition<any> | ComposeInputTypeDefinition
): NamedTypeComposer<TContext>
Creates or return existed TypeComposer from SDL or object. If you call this method again with same params should be returned the same TypeComposer instance.
createTempTC()
createTempTC(
typeOrSDL: ComposeOutputTypeDefinition<any> | ComposeInputTypeDefinition
): NamedTypeComposer<TContext>
Creates TypeComposer from SDL or object without adding it to the type storage.
getOrCreateTC()
getOrCreateTC(
typeName: string,
onCreate: (tc: ObjectTypeComposer<any, TContext>) => any
): ObjectTypeComposer<any, TContext>
getOrCreateOTC()
getOrCreateOTC(
typeName: string,
onCreate: (tc: ObjectTypeComposer<any, TContext>) => any
): ObjectTypeComposer<any, TContext>
getOrCreateITC()
getOrCreateITC(
typeName: string,
onCreate: (tc: InputTypeComposer<TContext>) => any
): InputTypeComposer<TContext>
getOrCreateETC()
getOrCreateETC(
typeName: string,
onCreate: (tc: EnumTypeComposer<TContext>) => any
): EnumTypeComposer<TContext>
getOrCreateIFTC()
getOrCreateIFTC(
typeName: string,
onCreate: (tc: InterfaceTypeComposer<any, TContext>) => any
): InterfaceTypeComposer<any, TContext>
getOrCreateUTC()
getOrCreateUTC(
typeName: string,
onCreate: (tc: UnionTypeComposer<any, TContext>) => any
): UnionTypeComposer<any, TContext>
getOrCreateSTC()
getOrCreateSTC(
typeName: string,
onCreate: (tc: ScalarTypeComposer<TContext>) => any
): ScalarTypeComposer<TContext>
getOTC()
getOTC(
typeName: unknown
): ObjectTypeComposer<any, TContext>
getITC()
getITC(
typeName: unknown
): InputTypeComposer<TContext>
getETC()
getETC(
typeName: unknown
): EnumTypeComposer<TContext>
getIFTC()
getIFTC(
typeName: unknown
): InterfaceTypeComposer<any, TContext>
getUTC()
getUTC(
typeName: unknown
): UnionTypeComposer<any, TContext>
getSTC()
getSTC(
typeName: unknown
): ScalarTypeComposer<TContext>
getAnyTC()
getAnyTC(
typeOrName: string | AnyType<any> | GraphQLType
): NamedTypeComposer<TContext>
addAsComposer()
addAsComposer(
typeOrSDL: ComposeOutputTypeDefinition<any> | ComposeInputTypeDefinition
): string
isObjectType()
isObjectType(
type: string | AnyType<any> | GraphQLType
): boolean
isInputObjectType()
isInputObjectType(
type: string | AnyType<any> | GraphQLType
): boolean
isScalarType()
isScalarType(
type: string | AnyType<any> | GraphQLType
): boolean
isEnumType()
isEnumType(
type: string | AnyType<any> | GraphQLType
): boolean
isInterfaceType()
isInterfaceType(
type: string | AnyType<any> | GraphQLType
): boolean
isUnionType()
isUnionType(
type: string | AnyType<any> | GraphQLType
): boolean
Storage methods
clear()
clear(): void
add()
add(
typeOrSDL: ComposeOutputTypeDefinition<any> | ComposeInputTypeDefinition
): string
set()
set(
key: unknown,
value: NamedTypeComposer<TContext>
): this
Directive methods
addDirective()
addDirective(
directive: GraphQLDirective
): this
removeDirective()
removeDirective(
directive: GraphQLDirective
): this
getDirectives()
getDirectives(): Array<GraphQLDirective>
getDirective()
getDirective(
name: string
): GraphQLDirective
hasDirective()
hasDirective(
directive: string | GraphQLDirective
): boolean
Misc methods
toString()
toString(): string
Returns a string representation of an object.
toJSON()
toJSON(): string
inspect()
inspect(): string
getTypeSDL()
getTypeSDL(
typeName: string,
opts: SchemaPrinterOptions & {
deep?: boolean;
exclude?: string[];
}
): string
Prints SDL for any type in schema by its name.
Can print all used sub-types if provided deep: true
option.
Also you may omit some sub-types via exclude: string[]
option.
toSDL()
toSDL(
options: SchemaComposerPrinterOptions
): string
Return schema as a SDL string. This SDL can be used with graphql-tools and Apollo Federation.
getResolveMethods()
getResolveMethods(
opts: {
exclude?: string[] | null;
}
): GraphQLToolsResolveMethods<TContext>
Returns a map of resolvers for each relevant GraphQL Object Type.
This map of Resolvers can be used with graphql-tools and Apollo Federation.
Internal type definitions
ExtraSchemaConfig
type ExtraSchemaConfig = {
description?: string | null;
types?: GraphQLNamedType[] | null;
directives?: GraphQLDirective[] | null;
extensions?: any | null;
astNode?: SchemaDefinitionNode | null;
extensionASTNodes?: ReadonlyArray<SchemaExtensionNode> | null;
/** You may pass all unused types from type registry to GraphQL schema if set this option to `true` */
keepUnusedTypes?: boolean | null;
};
GraphQLToolsResolveMethods
export type GraphQLToolsResolveMethods<TContext> = {
[typeName: string]:
| {
[fieldName: string]:
| ((
source: any,
args: Record<string, any>,
context: TContext,
info: GraphQLResolveInfo
) => any)
| string
| number
| Record<string, any>;
}
| GraphQLNamedType
| GraphQLScalarTypeConfig<any, any>;
};