简体   繁体   English

如何在 GraphQL Spring Boot 应用程序中跳过验证规则

[英]How to skip validation rules in GraphQL Spring Boot application

I have a SpringBoot + GraphQL application.我有一个 SpringBoot + GraphQL 应用程序。 I am trying to upgrade to the latest version (graphql-spring-boot-starter 11.1.0 -> 13.0.1) which changes graphql-java from 16.2 -> 19.2.我正在尝试升级到最新版本 (graphql-spring-boot-starter 11.1.0 -> 13.0.1),它将graphql-java从 16.2 -> 19.2 更改。

I have schema that looks like我有看起来像的架构

enum Type {
  TYPE1
  TYPE2
}
interface Generic {
  name: String
  type: Type
}
type Type1 extends Generic {
  name: String
  type: Type
  detail: Type1Detail
}
type Type2 extends Generic {
  name: String
  type: Type
  detail: Type2Detail
}

and my queries have pattern like this:我的查询有这样的模式:

query {
  GetObject {
    name
    type
    ... on Type1 {
      detail
    }
    ... on Type2 {
      detail
    }
  }

This has been working for the past few years on 16.2 and earlier, but with the updated version, I am getting error that looks like在过去的几年里,这在 16.2 及更早版本上一直有效,但是对于更新版本,我收到的错误看起来像

Validation error (FieldsConflict@[...] : detail : returns different types 'Type1Detail' and 'Type2Detail'

Is there any way to fix it other than changing the schema?除了更改架构之外,还有什么方法可以修复它吗? Because I have followed this naming pattern in a lot of places with several types that is hard to change now.因为我在很多地方都遵循这种命名模式,有几种现在很难改变的类型。

Alternately, I was tryingSkipping Validation Rules introduced in v18.0 , but I am not able to find what bean (and how) to create to override the GraphQLContext to pass in the specific predicate to disable that check.或者,我正在尝试跳过 v18.0 中引入的验证规则,但我无法找到创建什么 bean(以及如何)来覆盖GraphQLContext以传入特定谓词以禁用该检查。

I was able to solve this by creating GraphQLServletContextBuilder bean like below.我能够通过创建如下所示的GraphQLServletContextBuilder bean 来解决这个问题。 Other beans like GraphQLContextBuilder did not help. GraphQLContextBuilder等其他 bean 没有帮助。

    @Bean
    GraphQLServletContextBuilder graphQLServletContextBuilder() {
        return new GraphQLServletContextBuilder() {
            final Predicate<Class<?>> predicate =
                aClass -> !aClass.equals(OverlappingFieldsCanBeMerged.class);
            @Override
            public GraphQLKickstartContext build(
                HttpServletRequest httpServletRequest,
                HttpServletResponse httpServletResponse) {
                // I don't know if all 3 are required, but returning
                // null was throwing NPE.
                return GraphQLKickstartContext.of(Map.of(
                    HttpServletRequest.class,
                    httpServletRequest,
                    HttpServletResponse.class,
                    httpServletResponse,
                    "graphql.ParseAndValidate.Predicate",
                    predicate));
            }

            @Override
            public GraphQLKickstartContext build(
                Session session, HandshakeRequest handshakeRequest) {
                // I don't know if all 3 are required, but returning
                // null was throwing NPE.
                return GraphQLKickstartContext.of(Map.of(
                    Session.class,
                    session,
                    HandshakeRequest.class,
                    handshakeRequest,
                    "graphql.ParseAndValidate.Predicate",
                    predicate));
            }

            @Override
            public GraphQLKickstartContext build() {
                return GraphQLKickstartContext.of(
                    Map.of("graphql.ParseAndValidate.Predicate", predicate));
            }
        };
    }

暂无
暂无

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

相关问题 如何处理 Spring Boot 中的 GraphQL 查询验证错误 - How to handle GraphQL query validation error in Spring Boot 如何在 Spring 启动 GraphQL 中实现 javax.validation.ConstraintViolationException 的异常处理程序? - How to implement exception handler for javax.validation.ConstraintViolationException in Spring boot GraphQL? 如何从 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? 如何在 spring 启动/应用程序启动时跳过 mongodb - How to skip mongodb on app startup in spring boot/ Maven/Spring 启动项目——如何跳过@SpringBootTest - Maven / Spring boot project - how to skip @SpringBootTest Spring 引导安全:如何跳过本地主机的登录? - Spring Boot Security: How to skip login for localhost? 如何在 Spring Boot 测试中跳过 TestRestTemplate 的身份验证? - How to skip authentication for TestRestTemplate in Spring Boot Tests? 如何使用 graphql-spring-boot 向 GraphQL Java 添加检测? - How to add instrumentation to GraphQL Java with graphql-spring-boot? 如何从 spring 启动应用程序中的 graphQL 端点中的传入消息中读取请求标头 - How to read request headers from incoming message in a graphQL endpoint in spring boot application GraphQL - 如何捕获“内部” Apollo/GraphQL 错误 - Java Spring Boot - GraphQL - How to catch "Internal" Apollo/GraphQL errors - Java Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM