繁体   English   中英

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

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

我有一个 SpringBoot + GraphQL 应用程序。 我正在尝试升级到最新版本 (graphql-spring-boot-starter 11.1.0 -> 13.0.1),它将graphql-java从 16.2 -> 19.2 更改。

我有看起来像的架构

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
}

我的查询有这样的模式:

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

在过去的几年里,这在 16.2 及更早版本上一直有效,但是对于更新版本,我收到的错误看起来像

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

除了更改架构之外,还有什么方法可以修复它吗? 因为我在很多地方都遵循这种命名模式,有几种现在很难改变的类型。

或者,我正在尝试跳过 v18.0 中引入的验证规则,但我无法找到创建什么 bean(以及如何)来覆盖GraphQLContext以传入特定谓词以禁用该检查。

我能够通过创建如下所示的GraphQLServletContextBuilder bean 来解决这个问题。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM