|
| 1 | +package graphql.schema.idl; |
| 2 | + |
| 3 | +import graphql.Internal; |
| 4 | +import graphql.language.OperationTypeDefinition; |
| 5 | +import graphql.schema.GraphQLCodeRegistry; |
| 6 | +import graphql.schema.GraphQLDirective; |
| 7 | +import graphql.schema.GraphQLObjectType; |
| 8 | +import graphql.schema.GraphQLSchema; |
| 9 | +import graphql.schema.GraphQLType; |
| 10 | + |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +import static graphql.schema.idl.SchemaGeneratorHelper.buildDescription; |
| 15 | + |
| 16 | +/** |
| 17 | + * A schema generator that uses GraphQLSchema.FastBuilder for improved performance. |
| 18 | + * This is intended for benchmarking and performance testing purposes. |
| 19 | + */ |
| 20 | +@Internal |
| 21 | +public class FastSchemaGenerator { |
| 22 | + |
| 23 | + private final SchemaGeneratorHelper schemaGeneratorHelper = new SchemaGeneratorHelper(); |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates an executable schema from a TypeDefinitionRegistry using FastBuilder. |
| 27 | + * This method is optimized for performance and skips validation by default. |
| 28 | + * |
| 29 | + * @param typeRegistry the type definition registry |
| 30 | + * @param wiring the runtime wiring |
| 31 | + * @return an executable schema |
| 32 | + */ |
| 33 | + public GraphQLSchema makeExecutableSchema(TypeDefinitionRegistry typeRegistry, RuntimeWiring wiring) { |
| 34 | + return makeExecutableSchema(SchemaGenerator.Options.defaultOptions(), typeRegistry, wiring); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates an executable schema from a TypeDefinitionRegistry using FastBuilder. |
| 39 | + * |
| 40 | + * @param options the schema generation options |
| 41 | + * @param typeRegistry the type definition registry |
| 42 | + * @param wiring the runtime wiring |
| 43 | + * @return an executable schema |
| 44 | + */ |
| 45 | + public GraphQLSchema makeExecutableSchema(SchemaGenerator.Options options, TypeDefinitionRegistry typeRegistry, RuntimeWiring wiring) { |
| 46 | + // Make a copy and add default directives |
| 47 | + TypeDefinitionRegistry typeRegistryCopy = new TypeDefinitionRegistry(); |
| 48 | + typeRegistryCopy.merge(typeRegistry); |
| 49 | + schemaGeneratorHelper.addDirectivesIncludedByDefault(typeRegistryCopy); |
| 50 | + |
| 51 | + // Use immutable registry for faster operations |
| 52 | + ImmutableTypeDefinitionRegistry fasterImmutableRegistry = typeRegistryCopy.readOnly(); |
| 53 | + |
| 54 | + Map<String, OperationTypeDefinition> operationTypeDefinitions = SchemaExtensionsChecker.gatherOperationDefs(fasterImmutableRegistry); |
| 55 | + |
| 56 | + return makeExecutableSchemaImpl(fasterImmutableRegistry, wiring, operationTypeDefinitions, options); |
| 57 | + } |
| 58 | + |
| 59 | + private GraphQLSchema makeExecutableSchemaImpl(ImmutableTypeDefinitionRegistry typeRegistry, |
| 60 | + RuntimeWiring wiring, |
| 61 | + Map<String, OperationTypeDefinition> operationTypeDefinitions, |
| 62 | + SchemaGenerator.Options options) { |
| 63 | + // Build all types using the standard helper |
| 64 | + SchemaGeneratorHelper.BuildContext buildCtx = new SchemaGeneratorHelper.BuildContext( |
| 65 | + typeRegistry, wiring, operationTypeDefinitions, options); |
| 66 | + |
| 67 | + // Build directives |
| 68 | + Set<GraphQLDirective> additionalDirectives = schemaGeneratorHelper.buildAdditionalDirectiveDefinitions(buildCtx); |
| 69 | + |
| 70 | + // Use a dummy builder to trigger type building (this populates buildCtx) |
| 71 | + GraphQLSchema.Builder tempBuilder = GraphQLSchema.newSchema(); |
| 72 | + schemaGeneratorHelper.buildOperations(buildCtx, tempBuilder); |
| 73 | + |
| 74 | + // Build all additional types |
| 75 | + Set<GraphQLType> additionalTypes = schemaGeneratorHelper.buildAdditionalTypes(buildCtx); |
| 76 | + |
| 77 | + // Set field visibility on code registry |
| 78 | + buildCtx.getCodeRegistry().fieldVisibility(buildCtx.getWiring().getFieldVisibility()); |
| 79 | + |
| 80 | + // Build the code registry |
| 81 | + GraphQLCodeRegistry codeRegistry = buildCtx.getCodeRegistry().build(); |
| 82 | + |
| 83 | + // Extract operation types by name from built types |
| 84 | + Set<GraphQLType> allBuiltTypes = buildCtx.getTypes(); |
| 85 | + |
| 86 | + // Get the actual type names from operationTypeDefinitions, defaulting to standard names |
| 87 | + String queryTypeName = getOperationTypeName(operationTypeDefinitions, "query", "Query"); |
| 88 | + String mutationTypeName = getOperationTypeName(operationTypeDefinitions, "mutation", "Mutation"); |
| 89 | + String subscriptionTypeName = getOperationTypeName(operationTypeDefinitions, "subscription", "Subscription"); |
| 90 | + |
| 91 | + GraphQLObjectType queryType = findOperationType(allBuiltTypes, queryTypeName); |
| 92 | + GraphQLObjectType mutationType = findOperationType(allBuiltTypes, mutationTypeName); |
| 93 | + GraphQLObjectType subscriptionType = findOperationType(allBuiltTypes, subscriptionTypeName); |
| 94 | + |
| 95 | + if (queryType == null) { |
| 96 | + throw new IllegalStateException("Query type '" + queryTypeName + "' is required but was not found"); |
| 97 | + } |
| 98 | + |
| 99 | + // Create FastBuilder |
| 100 | + GraphQLSchema.FastBuilder fastBuilder = new GraphQLSchema.FastBuilder( |
| 101 | + GraphQLCodeRegistry.newCodeRegistry(codeRegistry), |
| 102 | + queryType, |
| 103 | + mutationType, |
| 104 | + subscriptionType); |
| 105 | + |
| 106 | + // Add all built types |
| 107 | + fastBuilder.additionalTypes(allBuiltTypes); |
| 108 | + fastBuilder.additionalTypes(additionalTypes); |
| 109 | + |
| 110 | + // Add all directive definitions |
| 111 | + fastBuilder.additionalDirectives(additionalDirectives); |
| 112 | + |
| 113 | + // Add schema description if present |
| 114 | + typeRegistry.schemaDefinition().ifPresent(schemaDefinition -> { |
| 115 | + String description = buildDescription(buildCtx, schemaDefinition, schemaDefinition.getDescription()); |
| 116 | + fastBuilder.description(description); |
| 117 | + }); |
| 118 | + |
| 119 | + // Add schema definition |
| 120 | + fastBuilder.definition(typeRegistry.schemaDefinition().orElse(null)); |
| 121 | + |
| 122 | + // Disable validation for performance |
| 123 | + fastBuilder.withValidation(false); |
| 124 | + |
| 125 | + return fastBuilder.build(); |
| 126 | + } |
| 127 | + |
| 128 | + private String getOperationTypeName(Map<String, OperationTypeDefinition> operationTypeDefs, |
| 129 | + String operationName, |
| 130 | + String defaultTypeName) { |
| 131 | + OperationTypeDefinition opDef = operationTypeDefs.get(operationName); |
| 132 | + if (opDef != null) { |
| 133 | + return opDef.getTypeName().getName(); |
| 134 | + } |
| 135 | + return defaultTypeName; |
| 136 | + } |
| 137 | + |
| 138 | + private GraphQLObjectType findOperationType(Set<GraphQLType> types, String typeName) { |
| 139 | + for (GraphQLType type : types) { |
| 140 | + if (type instanceof GraphQLObjectType) { |
| 141 | + GraphQLObjectType objectType = (GraphQLObjectType) type; |
| 142 | + if (objectType.getName().equals(typeName)) { |
| 143 | + return objectType; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + return null; |
| 148 | + } |
| 149 | +} |
0 commit comments