如何解决使用 graphql API 时出现无效的语法错误
我在通过邮递员访问 Graphql API 时也遇到了一些问题。
http://testEnvrironment/restcall/getReports
POST 正文
getReports(id:15){
isApproved
}
}
sample.graphqls 文件的条目是
schema{
query: Query
}
type Query{
getReports(id: Long): PublishedReportInternal
}
type PublishedReportInternal{
sourceReport:ObjectRef
isApproved:Boolean
ignoreOutputFormatId:Boolean
}
type ObjectRef{
id : Long
qualifier : String
}
@Override
public ResponseEntity<Object> getReports(String query) {
ExecutionResult execute = graphQLService.getGraphQL().execute(query);
System.out.println("query::"+query);
System.out.println("execute result...."+execute);
return new ResponseEntity<>(execute,HttpStatus.OK);
}
@Service
public class CustomDataFetcher implements DataFetcher<PublishedReportInternal> {
@Inject
PublishRunReportInternalRestService service;
@Override
public PublishedReportInternal get(DataFetchingEnvironment environment) {
System.out.println("Entered into DataFetcher class...");
String reportId=environment.getArgument("id");
System.out.println("reportId is ::"+reportId);
if(!StringUtils.isEmpty(reportId)) {
return service.getReportById(new Long(reportId));
}
else {
return null;
}
}
}
下面是GraphQLService的实现
@Service
public class GraphQLService {
@Value("classpath:sample.graphqls")
private Resource schemaResource;
private GraphQL graphQL;
@Inject
private CustomDataFetcher customDataFetcher;
@PostConstruct
private void loadSchema() throws IOException {
// get the schema
File schemaFile = schemaResource.getFile();
System.out.println(schemaFile.getAbsolutePath());
// parse schema
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(schemaFile);
RuntimeWiring wiring = buildRuntimeWiring();
GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(typeRegistry,wiring);
graphQL = GraphQL.newGraphQL(schema).build();
}
private RuntimeWiring buildRuntimeWiring() {
return RuntimeWiring.newRuntimeWiring().type("Query",typeWiring -> typeWiring
.dataFetcher("getReports",customDataFetcher)).build();
}
public GraphQL getGraphQL() {
return graphQL;
}
}
Rest 端点将是
@POST
@Path(value = "/getReports")
@Consumes
(value = MediaType.APPLICATION_JSON)
@Produces
(value = MediaType.APPLICATION_JSON) ResponseEntity<Object> getReports( String query);
请尝试在这里提供帮助,因为我完全被这个问题困住了。
我在此 POC 中使用了以下 jar
compile files('libs/new/graphiql-spring-boot-starter-5.0.2.jar')
compile files('libs/new/graphql-java-9.2.jar')
compile files('libs/new/graphql-java-servlet-6.1.2.jar')
compile files('libs/new/graphql-java-tools-5.2.4.jar')
compile files('libs/new/graphql-spring-boot-autoconfigure-5.0.2.jar')
compile files('libs/new/graphql-spring-boot-starter-5.0.2.jar')
compile files('libs/new/java-dataloader-2.0.2.jar')
compile files('libs/new/antlr4-runtime-4.7.1.jar')
compile files('libs/new/reactive-streams-1.0.3.jar')
以上代码片段包含我用来执行此 POC 的所有信息,但在执行此 POC 时,我始终收到“无效语法”错误。请帮助解决此问题。
问候 库沙格拉
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。