首页 > 解决方案 > 使用 graphql API 时出现无效的语法错误

问题描述

通过邮递员访问 Graphql API 时,我也遇到了一些问题。

http://testEnvrironment/restcall/getReports

帖子正文

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;
}
}

休息端点将是

@POST
@Path(value = "/getReports") 
@Consumes
(value = MediaType.APPLICATION_JSON) 
@Produces
(value = MediaType.APPLICATION_JSON) ResponseEntity<Object> getReports( String query);

请尝试在这里提供帮助,因为我完全被这个问题所困扰。

我已将以下 jar 用于此 POC

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 时,我一直收到“无效语法”错误。请帮助解决此问题。

问候库沙格拉

标签: graphqlgraphql-javaexpress-graphqlgraphql-subscriptions

解决方案


推荐阅读