- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前,我抛出 RuntimeException 来返回 GraphQL 验证错误。它的工作效果出奇的好,除了它在我的日志中抛出大量堆栈跟踪的可怕错误之外。
在这里您可以看到我正在检查提交的新用户注册突变,以确保密码彼此匹配并且电子邮件地址尚未被使用。
在 GraphQL SPQR Spring Boot Starter 中执行此操作的正确方法是什么。
@GraphQLMutation (name="register")
public User register(@GraphQLArgument(name="firstname") String firstname, @GraphQLArgument(name="lastname") String lastname, @GraphQLArgument(name="email") String email, @GraphQLArgument(name="msisdn") String msisdn, @GraphQLArgument(name="password") String password, @GraphQLArgument (name="confirmPassword") String confirmPassword) {
if (userRepo.findByEmail(email) != null) {
throw new RuntimeException("User already exists");
}
if (!password.equals(confirmPassword)) {
throw new RuntimeException("Passwords do not match");
}
User newUser = new User();
//...
return userRepo.save(newUser);
}
最佳答案
我不清楚你在问什么......但我假设你想自定义记录的内容。
对于初学者,我建议使用专用的异常类型,例如 ValidationException
,您可以以不同的方式捕获和处理它。
至于日志记录,它可能发生在 grapqh-java 中,因为 SPQR 本身不记录任何内容。默认情况下,graphql-java 使用 SimpleDataFetcherExceptionHandler
,其中 logs the exceptions在字段解析期间捕获。
您现在有几个选择,您可以在 SPQR 中注册一个 ResolverInterceptor
来捕获验证异常并记录您想要的内容,并返回一个包含错误消息的 DataFetcherResult
对于用户来说。由于没有验证异常冒泡到 graphql-java,因此 DataFetcherExceptionHandler
在这种情况下没有任何作用。
它看起来像:
public class ValidationInterceptor implements ResolverInterceptor {
@Override
public Object aroundInvoke(InvocationContext context, Continuation continuation) throws Exception {
try {
return continuation.proceed(context);
} catch (ValidationException e) {
log.warning(e);
return DataFetcherResult.newResult()
.error(GraphqlErrorBuilder
.newError(context.getResolutionEnvironment().dataFetchingEnvironment)
.message(e.getMessage()) //the message for the user
.build());
}
}
}
看看the answer here有关使用 Spring Boot 注册自定义拦截器的说明。
另一个选择是替换 DataFetcherExceptionHandler
graphql-java 使用。为此,您必须自己构造 GraphQL 对象并将其注册为 bean。
@Bean
public GraphQL graphQL(GraphQLSchema schema) {
GraphQL.Builder builder = GraphQL.newGraphQL(schema)
.queryExecutionStrategy(new AsyncExecutionStrategy(customExceptionHandler))
.mutationExecutionStrategy(new AsyncSerialExecutionStrategy(customExceptionHandler));
return builder.build();
}
如果某个地方有一个 Spring 功能可用于托管 bean 的异常处理,我也不会感到惊讶。
关于java - GraphQL-SPQR 中返回错误的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57215323/
有没有办法检索客户端在 GraphQL 查询中请求的字段列表? 假设我有以下类型: type Book { isbn: String title: String genre: String
我用 io.leangen.graphql.spqr库版本0.9.6我需要从 Query 中排除突变根节点进入文档。 我的 GraphQLController.kt看起来像这样: @RestContr
目前,我抛出 RuntimeException 来返回 GraphQL 验证错误。它的工作效果出奇的好,除了它在我的日志中抛出大量堆栈跟踪的可怕错误之外。 在这里您可以看到我正在检查提交的新用户注册突
使用 SQPR 构建 graphQL API。我有大量数据集合,想逐页加载数据。使用 SQPR 执行此操作的推荐方法是什么?从客户端(查询)来看它会是什么样子? 此外,请记住将 Apollo 客户端作
我graphql-spqr,java.util.Date被定义为标量。是否可以覆盖 java.util.Date 的序列化/反序列化以获得日期的不同字符串表示形式? 本文中提到的ScalarStrat
能够扩展现有类型是非常棒的,因为它允许代码模块化和权限分离。我找到了关于如何扩展查询中的对象输出的好例子(见下文),但没有好的方法来扩展给定对象的输入。 为了举例,假设我们有一个类User。 clas
如何使用 Graphql SPQR 库实现 GraphQL 的订阅功能? 最佳答案 一般来说,你只需要用 @GraphQLSubscription 注释一个方法。并确保该方法返回 org.reacti
我有以下带有自定义属性的数据模型: class Foo { private Long id; private Set attributes; } cla
我收到以下信息,但似乎找不到答案。 Error [ValidationError{validationErrorType=FieldUndefined, queryPath=[find_by_id],
我使用 GraphQL SPQR与实体 @Entity public class MyEntity { @Id @GeneratedValue(strategy = Generatio
我想在服务类中返回 JSON 文字 @GraphQLQuery(name = "renderUI", description = "Schema for your form") public Stri
我们正在将 graphql-spqr 和 graphql-spqr-spring-boot-starter 用于一个新项目(使用 Spring DataJPA、hibernate 等)。 我们有这样的
我正在尝试为我的 spring-boot/graphQL/SPQR 项目创建一些身份验证机制。我设法通过 @GraphQLRootContext 注释在单个服务方法上完成它: @GraphQLMuta
我注意到 GraphQL-SPQR 至少有几个可以配置的属性(例如 graphql.spqr.gui.enabled=true)。是否有配置指定使用 Gson 而不是 Jackson 进行对象序列化/
没有标准来表示NaN和 INFINITY Graphql 都不在以及 JSON . 在 JavaScript 中,JSON.stringify(0/0) 和 JSON.stringify(1/0) 将
我正在使用 graphql-spqr-spring-boot-starter库将现有的 Rest API 项目迁移到 GraphQL。我有一段下面的代码来获取当前的 HttpServletReques
我是一名优秀的程序员,十分优秀!