GraphQl Java-使用SIRCELACATION从valivationError提取字段
假设我有一个简单的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论