Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions src/main/java/graphql/schema/idl/ImmutableTypeDefinitionRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package graphql.schema.idl;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import graphql.GraphQLError;
import graphql.PublicApi;
import graphql.language.DirectiveDefinition;
import graphql.language.EnumTypeExtensionDefinition;
import graphql.language.InputObjectTypeExtensionDefinition;
import graphql.language.InterfaceTypeExtensionDefinition;
import graphql.language.ObjectTypeExtensionDefinition;
import graphql.language.SDLDefinition;
import graphql.language.ScalarTypeDefinition;
import graphql.language.ScalarTypeExtensionDefinition;
import graphql.language.SchemaExtensionDefinition;
import graphql.language.TypeDefinition;
import graphql.language.UnionTypeExtensionDefinition;
import graphql.schema.idl.errors.SchemaProblem;
import org.jspecify.annotations.NullMarked;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.google.common.collect.ImmutableMap.copyOf;

/**
* A {@link ImmutableTypeDefinitionRegistry} contains an immutable set of type definitions that come from compiling
* a graphql schema definition file via {@link SchemaParser#parse(String)} and is more performant because it
* uses {@link ImmutableMap} structures.
*/
@SuppressWarnings("rawtypes")
@PublicApi
@NullMarked
public class ImmutableTypeDefinitionRegistry extends TypeDefinitionRegistry {
ImmutableTypeDefinitionRegistry(TypeDefinitionRegistry registry) {
super(
copyOf(registry.objectTypeExtensions),
copyOf(registry.interfaceTypeExtensions),
copyOf(registry.unionTypeExtensions),
copyOf(registry.enumTypeExtensions),
copyOf(registry.scalarTypeExtensions),
copyOf(registry.inputObjectTypeExtensions),
copyOf(registry.types),
copyOf(registry.scalars()), // has an extra side effect
copyOf(registry.directiveDefinitions),
ImmutableList.copyOf(registry.schemaExtensionDefinitions),
registry.schema,
registry.schemaParseOrder
);
}

private UnsupportedOperationException unsupportedOperationException() {
return new UnsupportedOperationException("The TypeDefinitionRegistry is in read only mode");
}

@Override
public TypeDefinitionRegistry merge(TypeDefinitionRegistry typeRegistry) throws SchemaProblem {
throw unsupportedOperationException();
}

@Override
public Optional<GraphQLError> addAll(Collection<SDLDefinition> definitions) {
throw unsupportedOperationException();
}

@Override
public Optional<GraphQLError> add(SDLDefinition definition) {
throw unsupportedOperationException();
}

@Override
public void remove(SDLDefinition definition) {
throw unsupportedOperationException();
}

@Override
public void remove(String key, SDLDefinition definition) {
throw unsupportedOperationException();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the mutative methods are unsupported


@Override
public Map<String, TypeDefinition> types() {
return types;
}

@Override
public Map<String, ScalarTypeDefinition> scalars() {
return scalarTypes;
}

@Override
public Map<String, List<ObjectTypeExtensionDefinition>> objectTypeExtensions() {
return objectTypeExtensions;
}

@Override
public Map<String, List<InterfaceTypeExtensionDefinition>> interfaceTypeExtensions() {
return interfaceTypeExtensions;
}

@Override
public Map<String, List<UnionTypeExtensionDefinition>> unionTypeExtensions() {
return unionTypeExtensions;
}

@Override
public Map<String, List<EnumTypeExtensionDefinition>> enumTypeExtensions() {
return enumTypeExtensions;
}

@Override
public Map<String, List<ScalarTypeExtensionDefinition>> scalarTypeExtensions() {
return scalarTypeExtensions;
}

@Override
public Map<String, List<InputObjectTypeExtensionDefinition>> inputObjectTypeExtensions() {
return inputObjectTypeExtensions;
}

@Override
public List<SchemaExtensionDefinition> getSchemaExtensionDefinitions() {
return schemaExtensionDefinitions;
}

@Override
public Map<String, DirectiveDefinition> getDirectiveDefinitions() {
return directiveDefinitions;
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No copies made for these read only methods

10 changes: 6 additions & 4 deletions src/main/java/graphql/schema/idl/SchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,19 @@ public GraphQLSchema makeExecutableSchema(Options options, TypeDefinitionRegistr

schemaGeneratorHelper.addDirectivesIncludedByDefault(typeRegistryCopy);

List<GraphQLError> errors = typeChecker.checkTypeRegistry(typeRegistryCopy, wiring);
// by making it read only all the traversal and checks run faster
ImmutableTypeDefinitionRegistry fasterImmutableRegistry = typeRegistryCopy.readOnly();
List<GraphQLError> errors = typeChecker.checkTypeRegistry(fasterImmutableRegistry, wiring);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the performance gains will be made

if (!errors.isEmpty()) {
throw new SchemaProblem(errors);
}

Map<String, OperationTypeDefinition> operationTypeDefinitions = SchemaExtensionsChecker.gatherOperationDefs(typeRegistry);
Map<String, OperationTypeDefinition> operationTypeDefinitions = SchemaExtensionsChecker.gatherOperationDefs(fasterImmutableRegistry);

return makeExecutableSchemaImpl(typeRegistryCopy, wiring, operationTypeDefinitions, options);
return makeExecutableSchemaImpl(fasterImmutableRegistry, wiring, operationTypeDefinitions, options);
}

private GraphQLSchema makeExecutableSchemaImpl(TypeDefinitionRegistry typeRegistry,
private GraphQLSchema makeExecutableSchemaImpl(ImmutableTypeDefinitionRegistry typeRegistry,
RuntimeWiring wiring,
Map<String, OperationTypeDefinition> operationTypeDefinitions,
Options options) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/graphql/schema/idl/SchemaGeneratorHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class SchemaGeneratorHelper {
* it gives us helper functions
*/
static class BuildContext {
private final TypeDefinitionRegistry typeRegistry;
private final ImmutableTypeDefinitionRegistry typeRegistry;
private final RuntimeWiring wiring;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this use the type just to be more explicit but I didnt change it every where it gets used

private final Deque<String> typeStack = new ArrayDeque<>();

Expand All @@ -123,7 +123,7 @@ static class BuildContext {
public final SchemaGenerator.Options options;
public boolean directiveWiringRequired;

BuildContext(TypeDefinitionRegistry typeRegistry, RuntimeWiring wiring, Map<String, OperationTypeDefinition> operationTypeDefinitions, SchemaGenerator.Options options) {
BuildContext(ImmutableTypeDefinitionRegistry typeRegistry, RuntimeWiring wiring, Map<String, OperationTypeDefinition> operationTypeDefinitions, SchemaGenerator.Options options) {
this.typeRegistry = typeRegistry;
this.wiring = wiring;
this.codeRegistry = GraphQLCodeRegistry.newCodeRegistry(wiring.getCodeRegistry());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/schema/idl/SchemaTypeChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
@Internal
public class SchemaTypeChecker {

public List<GraphQLError> checkTypeRegistry(TypeDefinitionRegistry typeRegistry, RuntimeWiring wiring) throws SchemaProblem {
public List<GraphQLError> checkTypeRegistry(ImmutableTypeDefinitionRegistry typeRegistry, RuntimeWiring wiring) throws SchemaProblem {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this use the type just to be more explicit but I didnt change it every where it gets used

List<GraphQLError> errors = new ArrayList<>();
checkForMissingTypes(errors, typeRegistry);

Expand Down
76 changes: 63 additions & 13 deletions src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,70 @@
@NullMarked
public class TypeDefinitionRegistry implements Serializable {

private final Map<String, List<ObjectTypeExtensionDefinition>> objectTypeExtensions = new LinkedHashMap<>();
private final Map<String, List<InterfaceTypeExtensionDefinition>> interfaceTypeExtensions = new LinkedHashMap<>();
private final Map<String, List<UnionTypeExtensionDefinition>> unionTypeExtensions = new LinkedHashMap<>();
private final Map<String, List<EnumTypeExtensionDefinition>> enumTypeExtensions = new LinkedHashMap<>();
private final Map<String, List<ScalarTypeExtensionDefinition>> scalarTypeExtensions = new LinkedHashMap<>();
private final Map<String, List<InputObjectTypeExtensionDefinition>> inputObjectTypeExtensions = new LinkedHashMap<>();

private final Map<String, TypeDefinition> types = new LinkedHashMap<>();
private final Map<String, ScalarTypeDefinition> scalarTypes = new LinkedHashMap<>();
private final Map<String, DirectiveDefinition> directiveDefinitions = new LinkedHashMap<>();
private @Nullable SchemaDefinition schema;
private final List<SchemaExtensionDefinition> schemaExtensionDefinitions = new ArrayList<>();
private final SchemaParseOrder schemaParseOrder = new SchemaParseOrder();
protected final Map<String, List<ObjectTypeExtensionDefinition>> objectTypeExtensions;
protected final Map<String, List<InterfaceTypeExtensionDefinition>> interfaceTypeExtensions;
protected final Map<String, List<UnionTypeExtensionDefinition>> unionTypeExtensions;
protected final Map<String, List<EnumTypeExtensionDefinition>> enumTypeExtensions;
protected final Map<String, List<ScalarTypeExtensionDefinition>> scalarTypeExtensions;
protected final Map<String, List<InputObjectTypeExtensionDefinition>> inputObjectTypeExtensions;

protected final Map<String, TypeDefinition> types;
protected final Map<String, ScalarTypeDefinition> scalarTypes;
protected final Map<String, DirectiveDefinition> directiveDefinitions;
protected @Nullable SchemaDefinition schema;
protected final List<SchemaExtensionDefinition> schemaExtensionDefinitions;
protected final SchemaParseOrder schemaParseOrder;

public TypeDefinitionRegistry() {
objectTypeExtensions = new LinkedHashMap<>();
interfaceTypeExtensions = new LinkedHashMap<>();
unionTypeExtensions = new LinkedHashMap<>();
enumTypeExtensions = new LinkedHashMap<>();
scalarTypeExtensions = new LinkedHashMap<>();
inputObjectTypeExtensions = new LinkedHashMap<>();
types = new LinkedHashMap<>();
scalarTypes = new LinkedHashMap<>();
directiveDefinitions = new LinkedHashMap<>();
schemaExtensionDefinitions = new ArrayList<>();
schemaParseOrder = new SchemaParseOrder();
}

protected TypeDefinitionRegistry(Map<String, List<ObjectTypeExtensionDefinition>> objectTypeExtensions,
Map<String, List<InterfaceTypeExtensionDefinition>> interfaceTypeExtensions,
Map<String, List<UnionTypeExtensionDefinition>> unionTypeExtensions,
Map<String, List<EnumTypeExtensionDefinition>> enumTypeExtensions,
Map<String, List<ScalarTypeExtensionDefinition>> scalarTypeExtensions,
Map<String, List<InputObjectTypeExtensionDefinition>> inputObjectTypeExtensions,
Map<String, TypeDefinition> types,
Map<String, ScalarTypeDefinition> scalarTypes,
Map<String, DirectiveDefinition> directiveDefinitions,
List<SchemaExtensionDefinition> schemaExtensionDefinitions,
@Nullable SchemaDefinition schema,
SchemaParseOrder schemaParseOrder) {
this.objectTypeExtensions = objectTypeExtensions;
this.interfaceTypeExtensions = interfaceTypeExtensions;
this.unionTypeExtensions = unionTypeExtensions;
this.enumTypeExtensions = enumTypeExtensions;
this.scalarTypeExtensions = scalarTypeExtensions;
this.inputObjectTypeExtensions = inputObjectTypeExtensions;
this.types = types;
this.scalarTypes = scalarTypes;
this.directiveDefinitions = directiveDefinitions;
this.schemaExtensionDefinitions = schemaExtensionDefinitions;
this.schema = schema;
this.schemaParseOrder = schemaParseOrder;
}

/**
* @return an immutable view of this {@link TypeDefinitionRegistry} that is more performant
* when in read only mode.
*/
public ImmutableTypeDefinitionRegistry readOnly() {
if (this instanceof ImmutableTypeDefinitionRegistry) {
return (ImmutableTypeDefinitionRegistry) this;
}
return new ImmutableTypeDefinitionRegistry(this);
}

/**
* @return the order in which {@link SDLDefinition}s were parsed
Expand Down
Loading