public GraphQLSchemaBuild rebuildDefinitions()

in data-audit/data-audit-common/src/main/java/org/kie/kogito/app/audit/graphql/GraphQLSchemaManager.java [73:132]


    public GraphQLSchemaBuild rebuildDefinitions(DataAuditContext dataAuditContext, Map<String, String> additionalDefinitions) {
        LOGGER.debug("Rebuilding graphQL definitions");
        RuntimeWiring.Builder runtimeWiringBuilder = newRuntimeWiring();

        runtimeWiringBuilder.scalar(ExtendedScalars.GraphQLBigInteger);
        runtimeWiringBuilder.scalar(ExtendedScalars.GraphQLLong);
        runtimeWiringBuilder.scalar(ExtendedScalars.Date);
        runtimeWiringBuilder.scalar(ExtendedScalars.DateTime);
        runtimeWiringBuilder.scalar(ExtendedScalars.Json);

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        classLoader = (classLoader != null) ? classLoader : this.getClass().getClassLoader();

        TypeDefinitionRegistry typeDefinitionRegistry = new TypeDefinitionRegistry();

        List<String> graphqlSchemas = new ArrayList<>();
        graphqlSchemas.addAll(
                List.of("META-INF/data-audit-types.graphqls",
                        "META-INF/data-audit-job-query.graphqls",
                        "META-INF/data-audit-process-query.graphqls",
                        "META-INF/data-audit-usertask-query.graphqls"));

        ServiceLoader.load(GraphQLSchemaQueryProvider.class, classLoader).forEach(queryProvider -> {
            graphqlSchemas.addAll(List.of(queryProvider.graphQLQueryExtension()));
            for (GraphQLSchemaQuery query : queryProvider.queries(dataAuditContext)) {
                runtimeWiringBuilder.type("Query", builder -> builder.dataFetcher(query.name(), query::fetch));
            }
        });

        List<InputStream> data = new ArrayList<>();
        data.addAll(graphqlSchemas.stream().map(this::toInputStream).toList());
        data.addAll(additionalDefinitions.values().stream().map(String::getBytes).map(ByteArrayInputStream::new).toList());

        // now we have all of definitions
        List<FieldDefinition> queryDefinitions = new ArrayList<>();
        for (InputStream graphQLSchema : data) {
            TypeDefinitionRegistry newTypes = readDefinitionRegistry(graphQLSchema);

            // for allowing extension of the schema we need to merge this object manually
            // we remove it from the new Types and aggregate in temporal list so we can add this at the end
            // of extension processing
            Optional<ObjectTypeDefinition> newDefinitions = newTypes.getType("Query", ObjectTypeDefinition.class);
            if (newDefinitions.isPresent()) {
                queryDefinitions.addAll(newDefinitions.get().getFieldDefinitions());
                newTypes.remove(newDefinitions.get());
            }
            typeDefinitionRegistry.merge(newTypes);
        }

        RuntimeWiring runtimeWiring = runtimeWiringBuilder.build();

        SchemaGenerator schemaGenerator = new SchemaGenerator();
        // we merge the query object
        typeDefinitionRegistry.add(ObjectTypeDefinition.newObjectTypeDefinition().name("Query").fieldDefinitions(queryDefinitions).build());
        GraphQLSchema newGraphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
        GraphQL newGraphQL = GraphQL.newGraphQL(newGraphQLSchema).build();

        LOGGER.debug("Succesfuly rebuilding graphQL definitions");
        return new GraphQLSchemaBuild(newGraphQLSchema, newGraphQL, additionalDefinitions);
    }