簡體   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