SchemaComposer
SchemaComposer
is a class which helps to create GraphQLSchema
.
Properties
typeMapper
typeMapper: TypeMapper<TContext>;
Query
readonly Query: ObjectTypeComposer<any, TContext>;
Returns ObjectTypeComposer
of Query
root type.
import { schemaComposer } from 'graphql-compose';
schemaComposer.Query.addFields({ field1: 'String' });
Mutation
readonly Mutation: ObjectTypeComposer<any, TContext>;
Returns ObjectTypeComposer
of Mutation
root type.
import { schemaComposer } from 'graphql-compose';
schemaComposer.Mutation.addFields({ field1: 'String' });
Subscription
readonly Subscription: ObjectTypeComposer<any, TContext>;
Returns ObjectTypeComposer
of Subscription
root type.
import { schemaComposer } from 'graphql-compose';
schemaComposer.Subscription.addFields({ field1: 'String' });
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).
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);
const schemaComposer.getOTC('User').removeField('password');
const newSchema = schemaComposer.buildSchema();
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 avaliable 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(
typeDef: ScalarTypeComposerDefinition
): ScalarTypeComposer<TContext>
createResolver()
createResolver<TSource = any, TArgs = ArgsMap>(
opts: ResolverDefinition<TSource, TContext, TArgs>
): Resolver<TSource, TContext, TArgs>
createTC()
createTC(
typeOrSDL: any
): 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: any
): NamedTypeComposer<TContext>
Creates TypeComposer from SDL or object without adding it to the type storage.
getOrCreateOTC()
getOrCreateOTC<TSource = any>(
typeName: string,
onCreate: (tc: ObjectTypeComposer<TSource, TContext>) => any
): ObjectTypeComposer<TSource, TContext>
getOrCreateITC()
getOrCreateITC(
typeName: string,
onCreate: (itc: InputTypeComposer<TContext>) => any
): InputTypeComposer<TContext>
getOrCreateETC()
getOrCreateETC(
typeName: string,
onCreate: (etc: EnumTypeComposer<TContext>) => any
): EnumTypeComposer<TContext>
getOrCreateIFTC()
getOrCreateIFTC<TSource = any>(
typeName: string,
onCreate: (iftc: InterfaceTypeComposer<TSource, TContext>) => any
): InterfaceTypeComposer<TSource, TContext>
getOrCreateUTC()
getOrCreateUTC<TSource = any>(
typeName: string,
onCreate: (utc: UnionTypeComposer<TSource, TContext>) => any
): UnionTypeComposer<TSource, TContext>
getOrCreateSTC()
getOrCreateSTC(
typeName: string,
onCreate: (stc: ScalarTypeComposer<TContext>) => any
): ScalarTypeComposer<TContext>
getOTC()
getOTC<TSource = any>(
typeName: any
): ObjectTypeComposer<TSource, TContext>
getITC()
getITC(
typeName: any
): InputTypeComposer<TContext>
getETC()
getETC(
typeName: any
): EnumTypeComposer<TContext>
getIFTC()
getIFTC<TSource = any>(
typeName: any
): InterfaceTypeComposer<TSource, TContext>
getUTC()
getUTC<TSource = any>(
typeName: any
): UnionTypeComposer<TSource, TContext>
getSTC()
getSTC(
typeName: any
): ScalarTypeComposer<TContext>
getAnyTC()
getAnyTC(
typeName: string | AnyType<any> | GraphQLType
): NamedTypeComposer<TContext>
Storage methods
clear()
clear(): void
add()
add(
typeOrSDL: any
): string
delete()
delete(
key: any
): boolean
entries()
entries(): Iterator<[any, NamedTypeComposer<TContext>]>
forEach()
forEach(
callbackfn: (value: NamedTypeComposer<TContext>, index: any, map: Map<any, NamedTypeComposer<TContext>>) => any,
thisArg: any
): void
get()
get(
key: any
): NamedTypeComposer<TContext>
has()
has(
key: any
): boolean
keys()
keys(): Iterator<any>
set()
set(
key: any,
value: AnyType<TContext>
): this
values()
values(): Iterator<NamedTypeComposer<TContext>>
hasInstance()
hasInstance(
key: any,
ClassObj: any
): boolean
getOrSet()
getOrSet(
key: any,
typeOrThunk: NamedTypeComposer<TContext> | (() => NamedTypeComposer<TContext>)
): NamedTypeComposer<TContext>
Directive methods
addDirective()
addDirective(
directive: GraphQLDirective
): this
removeDirective()
removeDirective(
directive: GraphQLDirective
): this
getDirectives()
getDirectives(): GraphQLDirective[]
getDirective()
getDirective(
name: string
): GraphQLDirective
hasDirective()
hasDirective(
directive: string | GraphQLDirective
): boolean
Internal type definitions
ExtraSchemaConfig
type ExtraSchemaConfig = {
types?: GraphQLNamedType[] | null;
directives?: GraphQLDirective[] | null;
astNode?: SchemaDefinitionNode | null;
};
GraphQLToolsResolveMethods
type GraphQLToolsResolveMethods<TContext> = {
[typeName: string]: {
[fieldName: string]: (
source: any,
args: object,
context: TContext,
info: GraphQLResolveInfo
) => any;
};
};