GraphQl Java-使用SIRCELACATION从valivationError提取字段

发布于 2025-01-27 14:30:59 字数 1599 浏览 1 评论 0原文

假设我有一个简单的GraphQl架构:

type Query {
  test(requestId: String!): String
}

并且我执行了这样的查询(故意传递一个数字而不是字符串为'requestId'值):

query {
 test(requestId: 5)
}

然后,我有一个Java片段,我可以手动执行查询:

// code

ExecutionInput executionInput = ExecutionInput.newExecutionInput()
  .query(query)
  .variables(variables)
  .build();

ExecutionResult executionResult = graphQL.execute(executionInput);

if (!executionResult.getErrors().isEmpty()) {
  GraphQLError error = executionResult.getErrors().get(0);

  if (error instanceof ValidationError validationError) {
    // Extract the field in error here (maybe with SourceLocations from "validationError" obj)
  }
}

// code

我希望获得验证Erroror ,我明白了,但这是他的内容:

ValidationError{validationErrorType=WrongType, queryPath=[test], message=Validation error of type WrongType: argument 'requestId' with value 'IntValue{value=5}' is not a valid 'String' @ 'test', locations=[SourceLocation{line=2, column=8}], description='argument 'requestId' with value 'IntValue{value=5}' is not a valid 'String''}

validationError.getSourCelocations()。get(0).getSourCename()是null!


是否可以获取未通过验证的字段名称(在这种情况下requestID)?

我能够创建document对象做类似的操作:

Document document = new Parser().parseDocument(query);

也许可以使用sourcelocations使用document来检索”失败的字段”。

我尝试了很多东西,但是没有运气,也许有人以前遇到过同样的事情。

Supposing I have a simple GraphQL schema:

type Query {
  test(requestId: String!): String
}

and I execute a query like this (intentionally passing a Number instead of a String as 'requestId' value):

query {
 test(requestId: 5)
}

Then I have a Java snippet where I execute the query manually:

// code

ExecutionInput executionInput = ExecutionInput.newExecutionInput()
  .query(query)
  .variables(variables)
  .build();

ExecutionResult executionResult = graphQL.execute(executionInput);

if (!executionResult.getErrors().isEmpty()) {
  GraphQLError error = executionResult.getErrors().get(0);

  if (error instanceof ValidationError validationError) {
    // Extract the field in error here (maybe with SourceLocations from "validationError" obj)
  }
}

// code

I expect to get a ValidationError, and I get it but this is his content:

ValidationError{validationErrorType=WrongType, queryPath=[test], message=Validation error of type WrongType: argument 'requestId' with value 'IntValue{value=5}' is not a valid 'String' @ 'test', locations=[SourceLocation{line=2, column=8}], description='argument 'requestId' with value 'IntValue{value=5}' is not a valid 'String''}

the content of validationError.getSourceLocations().get(0).getSourceName() is null!


Is possible to get the name of the field who has not passed the validation (in this case requestId)?

I'm able to create a Document object doing something like this:

Document document = new Parser().parseDocument(query);

Maybe is possible to interpolate SourceLocations with the Document in order to retrieve the "failed field".

I tried a lot of stuff but without luck, maybe someone has faced the same before.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文