简体   繁体   English

Java 在单元测试中验证 graphQL 查询语法

[英]Java validate graphQL query syntax in unit test

When writing a GraphQL-client that builds query strings manually, is there a way to write unit tests that checks that the syntax of a graphQL query is valid?在编写手动构建查询字符串的 GraphQL 客户端时,有没有办法编写单元测试来检查 graphQL 查询的语法是否有效? Like using some official parser and checking it does not find syntax errors?就像使用一些官方解析器并检查它没有发现语法错误?

Usage of libraries is allowed in the answer, (though obviously if it's a library to not use raw strings to write queries, then the question becomes moot).答案中允许使用库(尽管很明显,如果它是一个不使用原始字符串编写查询的库,那么问题就变得没有意义了)。

Seems this might work, including graphql-java library:似乎这可能有效,包括 graphql-java 库:

<!-- https://mvnrepository.com/artifact/com.graphql-java/graphql-java -->
<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java</artifactId>
    <version>2020-05-19T06-33-01-57ce5cf</version>
</dependency>

The parse the query.解析查询。

graphql.parser.Parser graphQLParser = new Parser();
// assert no exception
graphQLParser.parseDocument(queryString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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