简体   繁体   English

如何使用 graphql-spring-boot 向 GraphQL Java 添加检测?

[英]How to add instrumentation to GraphQL Java with graphql-spring-boot?

does anybody know how I can add instrumentation to a GraphQL execution when using graphql-spring-boot ( https://github.com/graphql-java-kickstart/graphql-spring-boot ) ?有人知道在使用graphql-spring-boot ( https://github.com/graphql-java-kickstart/graphql-spring-boot ) 时如何向 GraphQL 执行添加检测吗? I know how this is possible with plain-vanilla graphql-java:https://www.graphql-java.com/documentation/v13/instrumentation/我知道使用普通的 graphql-java 是如何实现的:https ://www.graphql-java.com/documentation/v13/instrumentation/

However, I don't know how to do this when graphql-spring-boot is used and takes control over the execution.但是,当使用 graphql-spring-boot 并控制执行时,我不知道如何执行此操作。 Due to lack of documentation I tried it simply this way:由于缺乏文档,我只是这样尝试的:

@Service
public class GraphQLInstrumentationProvider implements InstrumentationProvider {
    @Override
    public Instrumentation getInstrumentation() {
        return SimpleInstrumentation.INSTANCE;
    }
}

But the method getInstrumentation on my InstrumentationProvider bean is (as expected) never called.但是我的 InstrumentationProvider bean 上的 getInstrumentation 方法(如预期)从未被调用过。 Any help appreciated.任何帮助表示赞赏。

Answering my own question.回答我自己的问题。 In the meantime I managed to do it this way:与此同时,我设法这样做:

final class RequestLoggingInstrumentation extends SimpleInstrumentation {

    private static final Logger logger = LoggerFactory.getLogger(RequestLoggingInstrumentation.class);

    @Override
    public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) {
        long startMillis = System.currentTimeMillis();
        var executionId = parameters.getExecutionInput().getExecutionId();

        if (logger.isInfoEnabled()) {
            logger.info("GraphQL execution {} started", executionId);

            var query = parameters.getQuery();
            logger.info("[{}] query: {}", executionId, query);
            if (parameters.getVariables() != null && !parameters.getVariables().isEmpty()) {
                logger.info("[{}] variables: {}", executionId, parameters.getVariables());
            }
        }

        return new SimpleInstrumentationContext<>() {
            @Override
            public void onCompleted(ExecutionResult executionResult, Throwable t) {
                if (logger.isInfoEnabled()) {
                    long endMillis = System.currentTimeMillis();

                    if (t != null) {
                        logger.info("GraphQL execution {} failed: {}", executionId, t.getMessage(), t);
                    } else {
                        var resultMap = executionResult.toSpecification();
                        var resultJSON = ObjectMapper.pojoToJSON(resultMap).replace("\n", "\\n");
                        logger.info("[{}] completed in {}ms", executionId, endMillis - startMillis);
                        logger.info("[{}] result: {}", executionId, resultJSON);
                    }
                }
            }
        };
    }
}

@Service
class InstrumentationService {

    private final ContextFactory contextFactory;

    InstrumentationService(ContextFactory contextFactory) {
        this.contextFactory = contextFactory;
    }

    /**
     * Return all instrumentations as a bean.
     * The result will be used in class {@link com.oembedler.moon.graphql.boot.GraphQLWebAutoConfiguration}.
     */
    @Bean
    List<Instrumentation> instrumentations() {
        // Note: Due to a bug in GraphQLWebAutoConfiguration, the returned list has to be modifiable (it will be sorted)
        return new ArrayList<>(
                List.of(new RequestLoggingInstrumentation()));
    }
}

It helped me to have a look into the class GraphQLWebAutoConfiguration .它帮助我了解了GraphQLWebAutoConfiguration类。 There I found out that the framework expects a bean of type List<Instrumentation> , which contains all the instrumentations that will be added to the GraphQL execution.在那里我发现框架需要一个List<Instrumentation>类型的 bean,它包含将添加到 GraphQL 执行的所有检测。

There is a simpler way to add instrumentation with spring boot:有一种更简单的方法可以使用 spring boot 添加检测:

@Configuration
public class InstrumentationConfiguration {
    @Bean
    public Instrumentation someFieldCheckingInstrumentation() {
        return new FieldValidationInstrumentation(env -> {
            // ... 
        });
    }
}

Spring boot will collect all beans which implement Instrumentation (see GraphQLWebAutoConfiguration ). Spring boot 将收集所有实现Instrumentation bean(参见GraphQLWebAutoConfiguration )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 graphql-spring-boot上传二进制文件 - graphql-spring-boot upload binary java.lang.NoClassDefFoundError: graphql/execution/instrumentation/SimpleInstrumentation(GraphQL 和 Spring 引导) - java.lang.NoClassDefFoundError: graphql/execution/instrumentation/SimpleInstrumentation (GraphQL and Spring Boot) GraphQL - 如何捕获“内部” Apollo/GraphQL 错误 - Java Spring Boot - GraphQL - How to catch "Internal" Apollo/GraphQL errors - Java Spring Boot 用于 Spring Boot 的 GraphQL Java 客户端 - GraphQL Java client for Spring Boot How to execute Java calls to GraphQL in a Spring Boot + GraphQL Java Tools' context? - How to execute Java calls to GraphQL in a Spring Boot + GraphQL Java Tools' context? graphql-java - 如何在 Spring Boot 中使用订阅? - graphql-java - How to use subscriptions with spring boot? Graphql spring boot 客户端 - Graphql spring boot client GraphQL 和 Spring Boot 2.0.3 - GraphQL and Spring Boot 2.0.3 spring 引导包含 com.graphql-java 和 graphql-java-spring-boot-starter-webmvc - spring boot containing com.graphql-java and graphql-java-spring-boot-starter-webmvc 如何从 java Z91F1F487C7FB768642B3EB98F683BF5 应用程序调用 GraphQL api? 是否有任何注释支持 graphQL 查询格式? - How to invoke GraphQL api from a java spring-boot application? Is there any annotation which supports graphQL query formation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM