[英]How to add instrumentation to GraphQL Java with graphql-spring-boot?
有人知道在使用graphql-spring-boot
( https://github.com/graphql-java-kickstart/graphql-spring-boot ) 時如何向 GraphQL 執行添加檢測嗎? 我知道使用普通的 graphql-java 是如何實現的:https ://www.graphql-java.com/documentation/v13/instrumentation/
但是,當使用 graphql-spring-boot 並控制執行時,我不知道如何執行此操作。 由於缺乏文檔,我只是這樣嘗試的:
@Service
public class GraphQLInstrumentationProvider implements InstrumentationProvider {
@Override
public Instrumentation getInstrumentation() {
return SimpleInstrumentation.INSTANCE;
}
}
但是我的 InstrumentationProvider bean 上的 getInstrumentation 方法(如預期)從未被調用過。 任何幫助表示贊賞。
回答我自己的問題。 與此同時,我設法這樣做:
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()));
}
}
它幫助我了解了GraphQLWebAutoConfiguration
類。 在那里我發現框架需要一個List<Instrumentation>
類型的 bean,它包含將添加到 GraphQL 執行的所有檢測。
有一種更簡單的方法可以使用 spring boot 添加檢測:
@Configuration
public class InstrumentationConfiguration {
@Bean
public Instrumentation someFieldCheckingInstrumentation() {
return new FieldValidationInstrumentation(env -> {
// ...
});
}
}
Spring boot 將收集所有實現Instrumentation
bean(參見GraphQLWebAutoConfiguration
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.