Class GraphQLSchema
See https://graphql.org/learn/schema/#type-language for more details
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classstatic final classA high-performance schema builder that avoids full-schema traversals performed byGraphQLSchema.Builder.build(). -
Constructor Summary
ConstructorsConstructorDescriptionGraphQLSchema(GraphQLSchema existingSchema, GraphQLCodeRegistry codeRegistry, com.google.common.collect.ImmutableMap<String, GraphQLNamedType> typeMap, com.google.common.collect.ImmutableMap<String, com.google.common.collect.ImmutableList<GraphQLObjectType>> interfaceNameToObjectTypes) -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsType(String typeName) Returns true if the schema contains a type with the specified nameReturns the set of "additional types" that were provided when building the schema.This returns all the top levelGraphQLNamedSchemaElementnamed types and directives in the schemaThis returns a map of non-repeatable and repeatable directives that have been explicitly applied to the schema object.Deprecated.This returns all theGraphQLNamedTypenamed types in th schema@Nullable SchemaDefinition@Nullable String@Nullable GraphQLDirectivegetDirective(String directiveName) Returns a named directive that (for legacy reasons) will be only in the set of non-repeatable directivesThis returns the list of directives definitions that are associated with this schema object including built in ones.@Nullable GraphQLFieldDefinitiongetFieldDefinition(FieldCoordinates fieldCoordinates) Returns aGraphQLFieldDefinitionas the specified co-ordinates or null if it does not exist@Nullable List<GraphQLObjectType> This will return the list ofGraphQLObjectTypetypes that implement the given interface type.@Nullable GraphQLObjectType@Nullable GraphQLObjectTypegetObjectType(String typeName) Called to return a namedGraphQLObjectTypefrom the schemagetSchemaAppliedDirective(String directiveName) This returns the named directive that have been explicitly applied to the schema object.This returns the list of directives that have been explicitly applied to the schema object.getSchemaAppliedDirectives(String directiveName) getSchemaDirective(String directiveName) Deprecated.Use theGraphQLAppliedDirectivemethods insteadDeprecated.Use theGraphQLAppliedDirectivemethods insteadDeprecated.Use theGraphQLAppliedDirectivemethods insteadgetSchemaDirectives(String directiveName) Deprecated.Use theGraphQLAppliedDirectivemethods instead@Nullable GraphQLObjectType@Nullable GraphQLTypeGets the named type from the schema or null if it's not present<T extends GraphQLType>
@Nullable TGets the named type from the schema or null if it's not present.<T extends GraphQLType>
List<T> getTypes(Collection<String> typeNames) All types with the provided names.booleanisPossibleType(GraphQLNamedType abstractType, GraphQLObjectType concreteType) Returns true if a specified concrete type is a possible type of a provided abstract type.booleanbooleanstatic GraphQLSchema.Builderstatic GraphQLSchema.BuildernewSchema(GraphQLSchema existingSchema) This allows you to build a schema from an existing schema.transform(Consumer<GraphQLSchema.Builder> builderConsumer) This helps you transform the current GraphQLSchema object into another one by starting a builder with all the current values and allows you to transform it how you want.transformWithoutTypes(Consumer<GraphQLSchema.BuilderWithoutTypes> builderConsumer) This helps you transform the current GraphQLSchema object into another one by using a builder that only allows you to change simple values and does not involve changing the complex schema type graph.
-
Constructor Details
-
GraphQLSchema
public GraphQLSchema(GraphQLSchema existingSchema, GraphQLCodeRegistry codeRegistry, com.google.common.collect.ImmutableMap<String, GraphQLNamedType> typeMap, com.google.common.collect.ImmutableMap<String, com.google.common.collect.ImmutableList<GraphQLObjectType>> interfaceNameToObjectTypes) -
GraphQLSchema
-
-
Method Details
-
getCodeRegistry
-
getIntrospectionSchemaFieldDefinition
- Returns:
- the special system field called "__schema"
-
getIntrospectionTypeFieldDefinition
- Returns:
- the special system field called "__type"
-
getIntrospectionTypenameFieldDefinition
- Returns:
- the special system field called "__typename"
-
getIntrospectionSchemaType
-
getAdditionalTypes
Returns the set of "additional types" that were provided when building the schema.During schema construction, types are discovered by traversing the schema from multiple roots:
- Root operation types (Query, Mutation, Subscription)
- Directive argument types
- Introspection types
- Types explicitly added via
GraphQLSchema.Builder.additionalType(GraphQLNamedType)
Additional types are types that are not reachable via any of the automatic traversal paths but still need to be part of the schema. The most common use case is for interface implementations that are not directly referenced elsewhere.
Types that do NOT need to be added as additional types:
- Types reachable from Query, Mutation, or Subscription fields
- Types used as directive arguments (these are discovered via directive traversal)
When additional types ARE typically needed:
- Interface implementations: When an interface is used as a field's return type, implementing object types are not automatically discovered because interfaces do not reference their implementors. These need to be added so they can be resolved at runtime and appear in introspection.
- SDL-defined schemas: When building from SDL, the
SchemaGeneratorautomatically detects types not connected to any root and adds them as additional types. - Programmatic schemas with type references: When using
GraphQLTypeReferenceto break circular dependencies, the actual type implementations may need to be provided as additional types.
Example - Interface implementation not directly referenced:
// Given this schema: // type Query { node: Node } // interface Node { id: ID! } // type User implements Node { id: ID!, name: String } // // User is not directly referenced from Query, so it needs to be added: GraphQLSchema.newSchema() .query(queryType) .additionalType(GraphQLObjectType.newObject().name("User")...) .build();Note: There are no restrictions on what types can be added via this mechanism. Types that are already reachable from other roots can also be added without causing errors - they will simply be present in both the type map (via traversal) and this set. After schema construction, use
getTypeMap()orgetAllTypesAsList()to get all types in the schema regardless of how they were discovered.Note on FastBuilder: When a schema is constructed using
GraphQLSchema.FastBuilder, this method returns ALL types in the schema except the root operation types (Query, Mutation, Subscription). This differs from schemas built with the standardGraphQLSchema.Builder, which returns only types not reachable from the root types. This semantic difference does not affect schema traversal or correctness, as both approaches ensure all types are properly discoverable.- Returns:
- an immutable set of types that were explicitly added as additional types
- See Also:
-
getType
Gets the named type from the schema or null if it's not present- Parameters:
typeName- the name of the type to retrieve- Returns:
- the type
-
getTypes
All types with the provided names. throwsAssertExceptionwhen a type name could not be resolved- Type Parameters:
T- for two- Parameters:
typeNames- the type names to get- Returns:
- The List of resolved types.
-
getTypeAs
Gets the named type from the schema or null if it's not present.Warning - you are inviting class cast errors if the types are not what you expect.
- Type Parameters:
T- for two- Parameters:
typeName- the name of the type to retrieve- Returns:
- the type cast to the target type.
-
containsType
Returns true if the schema contains a type with the specified name- Parameters:
typeName- the name of the type to check- Returns:
- true if there is a type with the specified name
-
getObjectType
Called to return a namedGraphQLObjectTypefrom the schema- Parameters:
typeName- the name of the type- Returns:
- a graphql object type or null if there is one
- Throws:
GraphQLException- if the type is NOT an object type
-
getFieldDefinition
Returns aGraphQLFieldDefinitionas the specified co-ordinates or null if it does not exist- Parameters:
fieldCoordinates- the field co-ordinates- Returns:
- the field or null if it does not exist
-
getTypeMap
- Returns:
- all the named types in the scheme as a map from name to named type
-
getAllTypesAsList
This returns all theGraphQLNamedTypenamed types in th schema- Returns:
- all the
GraphQLNamedTypetypes in the schema
-
getAllElementsAsList
This returns all the top levelGraphQLNamedSchemaElementnamed types and directives in the schema- Returns:
- all the top level
GraphQLNamedSchemaElementtypes and directives in the schema
-
getImplementations
This will return the list ofGraphQLObjectTypetypes that implement the given interface type.- Parameters:
type- interface type to obtain implementations of.- Returns:
- list of types implementing provided interface
-
isPossibleType
Returns true if a specified concrete type is a possible type of a provided abstract type. If the provided abstract type is: - an interface, it checks whether the concrete type is one of its implementations. - a union, it checks whether the concrete type is one of its possible types.- Parameters:
abstractType- abstract type either interface or unionconcreteType- concrete type- Returns:
- true if possible type, false otherwise.
-
getQueryType
- Returns:
- the Query type of the schema
-
getMutationType
- Returns:
- the Mutation type of the schema of null if there is not one
-
getSubscriptionType
- Returns:
- the Subscription type of the schema of null if there is not one
-
getDirectives
This returns the list of directives definitions that are associated with this schema object including built in ones.- Returns:
- a list of directives
-
getDirectivesByName
- Returns:
- a map of non-repeatable directives by directive name
-
getDirective
Returns a named directive that (for legacy reasons) will be only in the set of non-repeatable directives- Parameters:
directiveName- the name of the directive to retrieve- Returns:
- the directive or null if there is not one with that name
-
getSchemaDirectives
Deprecated.Use theGraphQLAppliedDirectivemethods insteadThis returns the list of directives that have been explicitly applied to the schema object. Note thatgetDirectives()will return directives for all schema elements, whereas this is just for the schema element itself- Returns:
- a list of directives
-
getSchemaDirectiveByName
Deprecated.Use theGraphQLAppliedDirectivemethods insteadThis returns a map of non-repeatable directives that have been explicitly applied to the schema object. Note thatgetDirectives()will return directives for all schema elements, whereas this is just for the schema element itself- Returns:
- a map of directives
-
getAllSchemaDirectivesByName
@Deprecated(since="2022-02-24") public Map<String,List<GraphQLDirective>> getAllSchemaDirectivesByName()Deprecated.Use theGraphQLAppliedDirectivemethods insteadThis returns a map of non-repeatable and repeatable directives that have been explicitly applied to the schema object. Note thatgetDirectives()will return directives for all schema elements, whereas this is just for the schema element itself- Returns:
- a map of directives
-
getSchemaDirective
Deprecated.Use theGraphQLAppliedDirectivemethods insteadThis returns the named directive that have been explicitly applied to the schema object. Note thatGraphQLDirectiveContainer.getDirective(String)will return directives for all schema elements, whereas this is just for the schema element itself- Parameters:
directiveName- the name of the directive- Returns:
- a named directive
-
getSchemaDirectives
@Deprecated(since="2022-02-24") public List<GraphQLDirective> getSchemaDirectives(String directiveName) Deprecated.Use theGraphQLAppliedDirectivemethods insteadThis returns the named directives that have been explicitly applied to the schema object.- Parameters:
directiveName- the name of the directive- Returns:
- A list of repeated applied directives
-
getSchemaAppliedDirectives
This returns the list of directives that have been explicitly applied to the schema object. Note thatgetDirectives()will return directives for all schema elements, whereas this is just for the schema element itself- Returns:
- a list of directives
-
getAllSchemaAppliedDirectivesByName
This returns a map of non-repeatable and repeatable directives that have been explicitly applied to the schema object. Note thatgetDirectives()will return directives for all schema elements, whereas this is just for the schema element itself- Returns:
- a map of all schema directives by directive name
-
getSchemaAppliedDirective
This returns the named directive that have been explicitly applied to the schema object. Note thatGraphQLDirectiveContainer.getDirective(String)will return directives for all schema elements, whereas this is just for the schema element itself- Parameters:
directiveName- the name of the directive- Returns:
- a named directive
-
getSchemaAppliedDirectives
-
getDefinition
-
getExtensionDefinitions
-
isSupportingMutations
public boolean isSupportingMutations() -
isSupportingSubscriptions
public boolean isSupportingSubscriptions() -
getDescription
-
transform
This helps you transform the current GraphQLSchema object into another one by starting a builder with all the current values and allows you to transform it how you want.- Parameters:
builderConsumer- the consumer code that will be given a builder to transform- Returns:
- a new GraphQLSchema object based on calling built on that builder
-
transformWithoutTypes
public GraphQLSchema transformWithoutTypes(Consumer<GraphQLSchema.BuilderWithoutTypes> builderConsumer) This helps you transform the current GraphQLSchema object into another one by using a builder that only allows you to change simple values and does not involve changing the complex schema type graph.- Parameters:
builderConsumer- the consumer code that will be given a builder to transform- Returns:
- a new GraphQLSchema object based on calling built on that builder
-
newSchema
- Returns:
- a new schema builder
-
newSchema
This allows you to build a schema from an existing schema. It copies everything from the existing schema and then allows you to replace them.- Parameters:
existingSchema- the existing schema- Returns:
- a new schema builder
-
GraphQLAppliedDirectivemethods instead