有人知道如何在使用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方法从未被调用过。感谢您的帮助。
发布于 2020-02-04 17:28:20
回答我自己的问题。与此同时,我设法做到了这一点:
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执行中的所有工具。
发布于 2020-10-01 20:53:37
有一种更简单的方法可以使用spring boot添加工具:
@Configuration
public class InstrumentationConfiguration {
@Bean
public Instrumentation someFieldCheckingInstrumentation() {
return new FieldValidationInstrumentation(env -> {
// ...
});
}
}Spring boot将收集所有实现Instrumentation的bean(参见GraphQLWebAutoConfiguration)。
https://stackoverflow.com/questions/59988269
复制相似问题