All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.smallrye.graphql.runtime.spi.datafetcher.BlockingHelper Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
package io.quarkus.smallrye.graphql.runtime.spi.datafetcher;

import java.util.concurrent.Callable;

import io.smallrye.graphql.schema.model.Execute;
import io.smallrye.graphql.schema.model.Operation;
import io.vertx.core.Context;
import io.vertx.core.Promise;

public final class BlockingHelper {

    private BlockingHelper() {
    }

    public static boolean blockingShouldExecuteNonBlocking(Operation operation, Context vc) {
        // Rule is that by default this should execute blocking except if marked as non-blocking)
        return operation.getExecute().equals(Execute.NON_BLOCKING);
    }

    public static boolean nonBlockingShouldExecuteBlocking(Operation operation, Context vc) {
        // Rule is that by default this should execute non-blocking except if marked as blocking
        return operation.getExecute().equals(Execute.BLOCKING) && vc.isEventLoopContext();
    }

    @SuppressWarnings("unchecked")
    public static void runBlocking(Context vc, Callable contextualCallable, Promise result) {
        // Here call blocking with context
        vc.executeBlocking(future -> {
            try {
                future.complete(contextualCallable.call());
            } catch (Exception ex) {
                future.fail(ex);
            }
        }, result);
    }
}