简体   繁体   中英

How to skip validation rules in GraphQL Spring Boot application

I have a SpringBoot + GraphQL application. 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.

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

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.

I was able to solve this by creating GraphQLServletContextBuilder bean like below. Other beans like GraphQLContextBuilder did not help.

    @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));
            }
        };
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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