-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Added a read only copy of a type registry for performance reasons #4021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| } | ||
|
|
||
| @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; | ||
| } | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No copies made for these read only methods |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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<>(); | ||
|
|
||
|
|
@@ -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()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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