如何从Java应用程序调用GraphQL端点
我正在尝试调用外部GraphQL端点,而不是由我控制,我在internet上所能找到的只是如何使用JavaSpringBoot设置后端GraphQL端点。如何从Java应用程序调用GraphQL端点?Netflix DGS有springboot客户端库 参考: 及如何从Java应用程序调用GraphQL端点,java,graphql,graphql-java,Java,Graphql,Graphql Java,我正在尝试调用外部GraphQL端点,而不是由我控制,我在internet上所能找到的只是如何使用JavaSpringBoot设置后端GraphQL端点。如何从Java应用程序调用GraphQL端点?Netflix DGS有springboot客户端库 参考: 及 我想您只是将查询作为文本发送到端点。感谢您的分享,当我添加graphql dgs spring boot starter此依赖项,然后尝试创建DefaultGraphQLClient的实例时,它表示无法解析符号。你能帮我解决我这里缺
我想您只是将查询作为文本发送到端点。感谢您的分享,当我添加graphql dgs spring boot starter此依赖项,然后尝试创建DefaultGraphQLClient的实例时,它表示无法解析符号。你能帮我解决我这里缺少的问题吗?将com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter依赖项添加到Gradle或Maven配置中,并像导入任何其他java一样导入该类。参考号:。如果您能将上述内容标记为有用,并且选择正确的选项作为正确答案,我将不胜感激。现在,我收到了以下错误:“创建名为“schema”的bean时出错,该bean在类路径资源[com/netflix/graphql/dgs/autoconfig/DgsAutoConfiguration.class]:创建bean时出现意外异常;嵌套异常为java.lang.TypeNotPresentException:Type graphql.schema.GraphQLCodeRegistry not Presentation不存在“”。这在几分钟前还有效…现在我开始出现这个错误
private RestTemplate dgsRestTemplate;
private static final String URL = "http://someserver/graphql";
private static final String QUERY = "{\n" +
" ticks(first: %d, after:%d){\n" +
" edges {\n" +
" node {\n" +
" route {\n" +
" name\n" +
" grade\n" +
" pitches\n" +
" location\n" +
" }\n" +
" \n" +
" userStars\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
public List<TicksConnection> getData() {
DefaultGraphQLClient graphQLClient = new DefaultGraphQLClient(URL);
GraphQLResponse response = graphQLClient.executeQuery(query, new HashMap<>(), (url, headers, body) -> {
/**
* The requestHeaders providers headers typically required to call a GraphQL endpoint, including the Accept and Content-Type headers.
* To use RestTemplate, the requestHeaders need to be transformed into Spring's HttpHeaders.
*/
HttpHeaders requestHeaders = new HttpHeaders();
headers.forEach(requestHeaders::put);
/**
* Use RestTemplate to call the GraphQL service.
* The response type should simply be String, because the parsing will be done by the GraphQLClient.
*/
ResponseEntity<String> exchange = dgsRestTemplate.exchange(url, HttpMethod.POST, new HttpEntity(body, requestHeaders), String.class);
/**
* Return a HttpResponse, which contains the HTTP status code and response body (as a String).
* The way to get these depend on the HTTP client.
*/
return new HttpResponse(exchange.getStatusCodeValue(), exchange.getBody());
});
TicksConnection ticks = graphQLResponse.extractValueAsObject("ticks", TicksConnection.class);
return ticks;
}