首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用graphql-spring-boot向GraphQL Java添加插装?

如何使用graphql-spring-boot向GraphQL Java添加插装?
EN

Stack Overflow用户
提问于 2020-01-30 22:30:02
回答 2查看 1K关注 0票数 3

有人知道如何在使用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并控制执行时,我不知道该怎么做。由于缺乏文档,我简单地尝试了一下:

代码语言:javascript
复制
@Service
public class GraphQLInstrumentationProvider implements InstrumentationProvider {
    @Override
    public Instrumentation getInstrumentation() {
        return SimpleInstrumentation.INSTANCE;
    }
}

但是(不出所料)我的InstrumentationProvider bean上的getInstrumentation方法从未被调用过。感谢您的帮助。

EN

回答 2

Stack Overflow用户

发布于 2020-02-04 17:28:20

回答我自己的问题。与此同时,我设法做到了这一点:

代码语言:javascript
复制
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执行中的所有工具。

票数 2
EN

Stack Overflow用户

发布于 2020-10-01 20:53:37

有一种更简单的方法可以使用spring boot添加工具:

代码语言:javascript
复制
@Configuration
public class InstrumentationConfiguration {
    @Bean
    public Instrumentation someFieldCheckingInstrumentation() {
        return new FieldValidationInstrumentation(env -> {
            // ... 
        });
    }
}

Spring boot将收集所有实现Instrumentation的bean(参见GraphQLWebAutoConfiguration)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59988269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档