Package graphql.schema
Class GraphQLSchema.FastBuilder
java.lang.Object
graphql.schema.GraphQLSchema.FastBuilder
- Enclosing class:
GraphQLSchema
A high-performance schema builder that avoids full-schema traversals performed by
GraphQLSchema.Builder.build(). This builder is significantly faster (5x+) and
allocates significantly less memory than the standard Builder. It is intended for
constructing large schemas (500+ types), especially deeply nested ones.
When to use FastBuilder
- Building large schemas where construction time and memory are measurable concerns
- All types are known upfront and can be added explicitly via
addType(graphql.schema.GraphQLNamedType)oraddTypes(java.util.Collection<? extends graphql.schema.GraphQLNamedType>) - The code registry is complete and available when FastBuilder is constructed
- Schema has been previously validated (e.g., in a build pipeline) and validation can be skipped
When NOT to use FastBuilder
- Type discovery required: If you rely on automatic type discovery by traversing from
root types (Query/Mutation/Subscription), use the standard
GraphQLSchema.Builderinstead. FastBuilder requires ALL types to be explicitly added. - Type reuse across schemas: FastBuilder mutates type objects during
build()to resolveGraphQLTypeReferences. The same type instances cannot be used to build multiple schemas. Create fresh type instances for each schema if needed. - Dynamic schema construction: FastBuilder does not support clearing or resetting state. Each FastBuilder instance should be used exactly once.
- Schema transformation: For transforming existing schemas, use
GraphQLSchema.transform(Consumer)orGraphQLSchema.Builderinstead.
Key differences from standard Builder
- No automatic type discovery: You must add ALL types explicitly, including interface implementations that would normally be discovered via traversal.
- Type mutation:
GraphQLTypeReferenceinstances in added types are replaced in-place with actual types during build. This mutates the original type objects. - additionalTypes semantic:
GraphQLSchema.getAdditionalTypes()returns ALL non-root types (not just "detached" types as with standard Builder). - Validation off by default: Enable with
withValidation(boolean)if needed.
Example usage
GraphQLObjectType queryType = ...;
GraphQLObjectType mutationType = ...;
Set<GraphQLNamedType> allTypes = ...; // All types including interface implementations
Set<GraphQLDirective> directives = ...;
GraphQLSchema schema = new GraphQLSchema.FastBuilder(
GraphQLCodeRegistry.newCodeRegistry(),
queryType,
mutationType,
null) // no subscription
.addTypes(allTypes)
.additionalDirectives(directives)
.withValidation(true) // optional, off by default
.build();
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionFastBuilder(GraphQLCodeRegistry.Builder codeRegistryBuilder, GraphQLObjectType queryType, @Nullable GraphQLObjectType mutationType, @Nullable GraphQLObjectType subscriptionType) Creates a new FastBuilder with the given code registry builder and root types. -
Method Summary
Modifier and TypeMethodDescriptionadditionalDirective(GraphQLDirective directive) Adds a directive definition to the schema.additionalDirectives(Collection<? extends GraphQLDirective> directives) Adds multiple directive definitions to the schema.addType(GraphQLNamedType type) Adds a named type to the schema.addTypes(Collection<? extends GraphQLNamedType> types) Adds multiple named types to the schema.build()Builds the GraphQL schema.Sets the schema definition (AST).description(String description) Sets the schema description.Sets the schema extension definitions (AST).Sets the introspection schema type.Adds a schema-level applied directive.withSchemaAppliedDirectives(Collection<? extends GraphQLAppliedDirective> appliedList) Adds multiple schema-level applied directives.withSchemaDirective(GraphQLDirective directive) Adds a schema-level directive (deprecated, use applied directives).withSchemaDirectives(Collection<? extends GraphQLDirective> directives) Adds multiple schema-level directives.withValidation(boolean enabled) Enables or disables schema validation.
-
Constructor Details
-
FastBuilder
public FastBuilder(GraphQLCodeRegistry.Builder codeRegistryBuilder, GraphQLObjectType queryType, @Nullable GraphQLObjectType mutationType, @Nullable GraphQLObjectType subscriptionType) Creates a new FastBuilder with the given code registry builder and root types.- Parameters:
codeRegistryBuilder- the code registry builder (required)queryType- the query type (required)mutationType- the mutation type (optional, may be null)subscriptionType- the subscription type (optional, may be null)
-
-
Method Details
-
addType
Adds a named type to the schema. All non-root types added via this method will be included inGraphQLSchema.getAdditionalTypes().Warning: The type object will be mutated during
build()if it containsGraphQLTypeReferenceinstances. Do not reuse the same type instance across multiple FastBuilder instances.- Parameters:
type- the named type to add- Returns:
- this builder for chaining
-
addTypes
Adds multiple named types to the schema. All non-root types added via this method will be included inGraphQLSchema.getAdditionalTypes().- Parameters:
types- the named types to add- Returns:
- this builder for chaining
-
additionalDirective
Adds a directive definition to the schema.- Parameters:
directive- the directive to add- Returns:
- this builder for chaining
-
additionalDirectives
public GraphQLSchema.FastBuilder additionalDirectives(Collection<? extends GraphQLDirective> directives) Adds multiple directive definitions to the schema.- Parameters:
directives- the directives to add- Returns:
- this builder for chaining
-
withSchemaDirective
Adds a schema-level directive (deprecated, use applied directives).- Parameters:
directive- the directive to add- Returns:
- this builder for chaining
-
withSchemaDirectives
public GraphQLSchema.FastBuilder withSchemaDirectives(Collection<? extends GraphQLDirective> directives) Adds multiple schema-level directives.- Parameters:
directives- the directives to add- Returns:
- this builder for chaining
-
withSchemaAppliedDirective
Adds a schema-level applied directive.- Parameters:
applied- the applied directive to add- Returns:
- this builder for chaining
-
withSchemaAppliedDirectives
public GraphQLSchema.FastBuilder withSchemaAppliedDirectives(Collection<? extends GraphQLAppliedDirective> appliedList) Adds multiple schema-level applied directives.- Parameters:
appliedList- the applied directives to add- Returns:
- this builder for chaining
-
definition
Sets the schema definition (AST).- Parameters:
def- the schema definition- Returns:
- this builder for chaining
-
extensionDefinitions
Sets the schema extension definitions (AST).- Parameters:
defs- the extension definitions- Returns:
- this builder for chaining
-
description
Sets the schema description.- Parameters:
description- the description- Returns:
- this builder for chaining
-
introspectionSchemaType
Sets the introspection schema type.- Parameters:
type- the introspection schema type- Returns:
- this builder for chaining
-
withValidation
Enables or disables schema validation.- Parameters:
enabled- true to enable validation, false to disable- Returns:
- this builder for chaining
-
build
Builds the GraphQL schema.Warning: This method mutates the type and directive objects that were added to this builder. Any
GraphQLTypeReferenceinstances within those objects are replaced in-place with the actual resolved types. After calling this method, the added types should not be reused with another FastBuilder.- Returns:
- the built schema
- Throws:
InvalidSchemaException- if validation is enabled and the schema is invalidAssertException- if a type reference cannot be resolved or if an interface/union type is missing a type resolver
-