SchemaComposer
SchemaComposer
is a class which helps to create GraphQLSchema
.
Getters
Query
Returns TypeComposer
of Query
root type.
import { schemaComposer } from 'graphql-compose';
schemaComposer.Query.addFields({ ... });
Mutation
Returns TypeComposer
of Mutation
root type.
import { schemaComposer } from 'graphql-compose';
schemaComposer.Mutation.addFields({ ... });
Subscription
Returns TypeComposer
of Subscription
root type.
import { schemaComposer } from 'graphql-compose';
schemaComposer.Subscription.addFields({ ... });
TypeComposer
const MeTC = schemaComposer.TypeComposer.create(`type Me { name: String! }`);
InputTypeComposer
const MeITC = schemaComposer.InputTypeComposer.create(`input MeInput { name: String! }`);
ScalarTypeComposer
const UIntSTC = schemaComposer.ScalarTypeComposer.create(`scalar UInt`);
EnumTypeComposer
const SortETC = schemaComposer.EnumTypeComposer.create(`enum Sort { ASC, DESC }`);
InterfaceTypeComposer
const HumanIFTC = schemaComposer.InterfaceTypeComposer.create(`interface Human { name: String! }`);
UnionTypeComposer
const PayloadUTC = schemaComposer.UnionTypeComposer.create(`union Payload = Data | Error`);
Resolver
const fetchResolver = new schemaComposer.Resolver({ ... });
TypeMapper
const GraphQLTypeMe = schemaComposer.TypeMapper.createType(`type Me { name: String! }`);
Methods
buildSchema()
Create EnumTypeComposer
with adding it by name to the SchemaComposer
. This type became avaliable in SDL by its name.
type ExtraSchemaConfig = {
types?: GraphQLNamedType[] | null,
directives?: GraphQLDirective[] | null,
astNode?: SchemaDefinitionNode | null,
};
buildSchema(extraConfig?: ExtraSchemaConfig): GraphQLSchema
addTypeDefs()
Add types to Schema via SDL string. Returns a Map of parsed types.
addTypeDefs(typeDefs: string): TypeStorage<GraphQLNamedType>;
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.
TypeComposer.create({
name: 'Author',
fields: {
posts: {
type: '[Post!]',
args: {
sort: 'Sort',
},
resolve: () => { ... },
}
}
});
addResolveMethods()
Define resolve
methods for Types in graphql-tools
manner.
addResolveMethods({
[typeName: string]: {
[fieldName: string]: (
source: any,
args: Object,
context: TContext,
info: GraphQLResolveInfo
) => any,
},
}): void
schemaComposer.addResolveMethods({
Query: {
posts: () => Post.find(),
},
Post: {
votes: (post) => Vote.getFor(post.id),
},
});
More details can be found in issue #142.
addSchemaMustHaveType()
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.
addSchemaMustHaveType(type: MustHaveTypes<TContext>): this;
createObjectTC()
createObjectTC(
typeDef: TypeComposerDefinition<TContext>
): TypeComposer<TContext>
// alias for createObjectTC
createTC(
typeDef: TypeComposerDefinition<TContext>
): TypeComposer<TContext>
createInputTC()
createInputTC(
typeDef: InputTypeComposerDefinition
): InputTypeComposer
createScalarTC()
createScalarTC(
typeDef: ScalarTypeComposerDefinition
): ScalarTypeComposer
createEnumTC()
createEnumTC(
typeDef: EnumTypeComposerDefinition
): EnumTypeComposer
createInterfaceTC()
createInterfaceTC(
typeDef: InterfaceTypeComposerDefinition<TContext>
): InterfaceTypeComposer<TContext>
createUnionTC()
createUnionTC(
typeDef: UnionTypeComposerDefinition<TContext>
): UnionTypeComposer<TContext>
getOrCreateTC()
getOrCreateTC(
typeName: string,
onCreate?: (TypeComposer) => any
): TypeComposer
getOrCreateITC()
getOrCreateITC(
typeName: string,
onCreate?: (InputTypeComposer) => any
): InputTypeComposer
getOrCreateSTC()
getOrCreateSTC(
typeName: string,
onCreate?: (ScalarTypeComposer) => any
): ScalarTypeComposer
getOrCreateETC()
getOrCreateETC(
typeName: string,
onCreate?: (EnumTypeComposer) => any
): EnumTypeComposer
getOrCreateIFTC()
getOrCreateIFTC(
typeName: string,
onCreate?: (iftc: InterfaceTypeComposer<TContext>) => any
): InterfaceTypeComposer<TContext>
getOrCreateUTC()
getOrCreateUTC(
typeName: string,
onCreate?: (iftc: UnionTypeComposer<TContext>) => any
): UnionTypeComposer<TContext>
getAnyTC()
getAnyTC(
typeName: any,
):
| TypeComposer
| InputTypeComposer
| EnumTypeComposer
| InterfaceTypeComposer
| UnionTypeComposer
| ScalarTypeComposer;
add()
add(
typeOrSDL: any
): string | null;
addAsComposer()
addAsComposer(
typeOrSDL: any
): string;
Storage methods
hasInstance()
hasInstance(
typeName: string,
ClassObj: typeof ComposeType
): boolean
getOrSet()
getOrSet(
typeName: string,
typeOrThunk: ComposeType | (() => ComposeType)
): V<TContext>
getTC()
getTC(
typeName: string
): TypeComposer<TContext>
getITC()
getITC(
typeName: string
): InputTypeComposer
getSTC()
getSTC(
typeName: string
): ScalarTypeComposer
getETC()
getETC(
typeName: string
): EnumTypeComposer
getIFTC()
getIFTC(
typeName: string
): InterfaceTypeComposer<TContext>
getUTC()
getUTC(
typeName: string
): UnionTypeComposer<TContext>
Map methods
clear()
Remove all types from Schema
clear(): void
delete()
delete(typeName: string): boolean
entries()
entries(): Iterator<[string, ComposeType]>
forEach()
forEach(
callbackfn: (
value: ComposeType,
index: string,
map: Map<string, ComposeType>
) => mixed,
thisArg?: any
): void
get()
get(typeName: string): ComposeType
has()
has(typeName: string): boolean
keys()
keys(): Iterator<string>
set()
set(
typeName: string,
value: ComposeType
): TypeStorage<TContext>
values()
values(): Iterator<ComposeType>
Directives methods
addDirective()
addDirective(
directive: GraphQLDirective
): this;
removeDirective()
removeDirective(
directive: GraphQLDirective
): this;
getDirectives()
getDirectives(): GraphQLDirective[];
hasDirective()
hasDirective(
directive: string | GraphQLDirective
): boolean;
Internal type definitions
Flowtype definitions which are used in this class.
ExtraSchemaConfig
type ExtraSchemaConfig = {
types?: ?Array<GraphQLNamedType>,
directives?: ?Array<GraphQLDirective>,
astNode?: ?SchemaDefinitionNode,
};
MustHaveTypes
type MustHaveTypes<TContext> =
| TypeComposer<TContext>
| InputTypeComposer
| ScalarTypeComposer
| EnumTypeComposer
| InterfaceTypeComposer<TContext>
| UnionTypeComposer<TContext>
| GraphQLNamedType;