[英]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.