使用 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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...