SchemaComposer
SchemaComposer
is a class which helps to create GraphQLSchema
.
Properties
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' });
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).
Like graphql-tools methods
addTypeDefs()
addTypeDefs(
typeDefs: string
): TypeStorage<string, GraphQLNamedType>
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: ObjectTypeComposeDefinition<TSource, TContext>
): ObjectTypeComposer<TSource, TContext>
createInputTC()
createInputTC(
typeDef: InputTypeComposeDefinition
): InputTypeComposer<TContext>
createEnumTC()
createEnumTC(
typeDef: EnumTypeComposeDefinition
): EnumTypeComposer<TContext>
createInterfaceTC()
createInterfaceTC<TSource = any>(
typeDef: InterfaceTypeComposeDefinition<TSource, TContext>
): InterfaceTypeComposer<TSource, TContext>
createUnionTC()
createUnionTC<TSource = any>(
typeDef: UnionTypeComposeDefinition<TSource, TContext>
): UnionTypeComposer<TSource, TContext>
createScalarTC()
createScalarTC(
typeDef: ScalarTypeComposeDefinition
): ScalarTypeComposer<TContext>
createResolver()
createResolver<TSource = any, TArgs = ArgsMap>(
opts: ResolverOpts<TSource, TContext, TArgs>
): Resolver<TSource, TContext, TArgs>
createTempTC()
createTempTC(
typeOrSDL: any
):
| ObjectTypeComposer<any, TContext>
| InputTypeComposer<TContext>
| EnumTypeComposer<TContext>
| InterfaceTypeComposer<any, TContext>
| UnionTypeComposer<any, TContext>
| ScalarTypeComposer<TContext>
Creates TypeComposer from SDL 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: any
):
| ObjectTypeComposer<any, TContext>
| InputTypeComposer<TContext>
| EnumTypeComposer<TContext>
| InterfaceTypeComposer<any, TContext>
| UnionTypeComposer<any, TContext>
| ScalarTypeComposer<TContext>
addAsComposer()
addAsComposer(
typeOrSDL: any
): string
Storage methods
clear()
clear(): void
delete()
delete(
key: any
): boolean
entries()
entries(): Iterator<[any, AnyType<TContext>]>
forEach()
forEach(
callbackfn: (value: AnyType<TContext>, index: any, map: Map<any, AnyType<TContext>>) => any,
thisArg: any
): void
get()
get(
key: any
): AnyType<TContext>
has()
has(
key: any
): boolean
keys()
keys(): Iterator<any>
set()
set(
key: any,
value: AnyType<TContext>
): this
values()
values(): Iterator<AnyType<TContext>>
add()
add(
typeOrSDL: any
): string | null
hasInstance()
hasInstance(
key: any,
ClassObj: any
): boolean
getOrSet()
getOrSet(
key: any,
typeOrThunk: AnyType<TContext> | (() => AnyType<TContext>)
): AnyType<TContext>
Directive methods
addDirective()
addDirective(
directive: GraphQLDirective
): this
removeDirective()
removeDirective(
directive: GraphQLDirective
): this
getDirectives()
getDirectives(): GraphQLDirective[]
hasDirective()
hasDirective(
directive: string | GraphQLDirective
): boolean
Misc methods
toString()
toString(): string
toJSON()
toJSON(): string
inspect()
inspect(): string
Internal type definitions
ExtraSchemaConfig
type ExtraSchemaConfig = {
types?: GraphQLNamedType[] | null;
directives?: GraphQLDirective[] | null;
astNode?: SchemaDefinitionNode | null;
};
AnyType
type AnyType<TContext> =
| ObjectTypeComposer<any, TContext>
| InputTypeComposer<TContext>
| EnumTypeComposer<TContext>
| InterfaceTypeComposer<any, TContext>
| UnionTypeComposer<any, TContext>
| ScalarTypeComposer<TContext>
| GraphQLNamedType;
GraphQLToolsResolveMethods
type GraphQLToolsResolveMethods<TContext> = {
[typeName: string]: {
[fieldName: string]: (
source: any,
args: {},
context: TContext,
info: GraphQLResolveInfo,
) => any;
};
};