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

com.spring.boxes.dollar.support.graphql.GraphqlPaths Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.dollar.support.graphql;

import com.spring.boxes.dollar.support.graphql.instrument.NestFutureTask;
import com.spring.boxes.dollar.support.graphql.instrument.NestInstrumentationState;
import graphql.analysis.QueryVisitorFieldEnvironment;
import graphql.execution.ResultPath;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;

@Slf4j
public class GraphqlPaths {

    public static final String PATH_SEPARATOR = "#";

    // 处理子任务,如果子任务有多个子任务回溯处理
    public static void completeSubTask(NestFutureTask futureTask) {
        if (Objects.isNull(futureTask) || CollectionUtils.isNotEmpty(futureTask.getSubTaskList())) {
            return;
        }
        for (NestFutureTask subTask : futureTask.getSubTaskList()) {
            if (isInListPath(subTask) && !subTask.getFuture().isDone()) {
                subTask.getFuture().complete(subTask.getTempResult());
            }
            completeSubTask(subTask);
        }
    }

    // 当前任务是否嵌套在list路径中
    public static boolean isInListPath(NestFutureTask task) {
        if (Objects.isNull(task.getParent())) {
            // Root Query
            return false;
        }
        NestFutureTask curr = task.getParent();
        while (curr != null) {
            if (curr.isList()) {
                return true;
            }
            curr = curr.getParent();
        }
        return false;
    }

    // 获取当前Filed路径粘连的Fetcher
    public static NestFutureTask getPathAdjoinFuture(NestInstrumentationState scheduleState, String fieldPath) {
        // 每个结点对应的Fetcher子任务
        Map> taskByPath = scheduleState.getTaskByPath();
        if (MapUtils.isEmpty(taskByPath)) {
            return null;
        }
        // 从根节点到当前节点的路径
        if (!taskByPath.containsKey(fieldPath)) {
            return null;
        }
        return taskByPath.get(fieldPath);
    }

    public static String pathForTraverse(QueryVisitorFieldEnvironment environment) {
        StringBuilder sb = new StringBuilder();
        QueryVisitorFieldEnvironment tmpEnv = environment;
        while (tmpEnv != null) {
            String pathSeg = tmpEnv.getField().getResultKey();
            if (sb.length() == 0) {
                sb.append(pathSeg);
            } else {
                sb.insert(0, pathSeg + PATH_SEPARATOR);
            }
            tmpEnv = tmpEnv.getParentEnvironment();
        }
        return sb.toString();
    }

    public static String visitPath(QueryVisitorFieldEnvironment environment) {
        if (Objects.isNull(environment)) {
            return StringUtils.EMPTY;
        }
        if (Objects.isNull(environment.getParentEnvironment())) {
            return environment.getField().getResultKey();
        }
        return visitPath(environment.getParentEnvironment()) + PATH_SEPARATOR + environment.getField().getResultKey();
    }

    public static String getNowFieldPath(final ResultPath stepInfo) {
        StringBuilder sb = new StringBuilder();
        ResultPath tmpEnv = stepInfo;
        while (tmpEnv != null) {
            if (!tmpEnv.isNamedSegment()) {
                tmpEnv = tmpEnv.getParent();
                continue;
            }
            String segmentName = tmpEnv.getSegmentName();
            if (segmentName == null || segmentName.length() == 0) {
                tmpEnv = tmpEnv.getParent();
                continue;
            }
            if (sb.length() == 0) {
                sb.append(segmentName);
            } else {
                sb.insert(0, segmentName + PATH_SEPARATOR);
            }
            tmpEnv = tmpEnv.getParent();
        }
        return sb.toString();
    }

}