【问题标题】:How to make a call to a GraphQL endpoint from a Java application [duplicate]如何从 Java 应用程序调用 GraphQL 端点 [重复]
【发布时间】:2021-05-13 07:02:57
【问题描述】:

我正在尝试调用 GraphQL 端点(外部,不受我控制),我在互联网上只能找到如何使用 Java Spring Boot 设置后端 GraphQL 端点。如何从 Java 应用程序调用 GraphQL 端点?

【问题讨论】:

  • 我认为您只是将查询作为文本发送到端点。

标签: java graphql graphql-java


【解决方案1】:

Netflix DGS 有 springboot 客户端库。

参考:https://netflix.github.io/dgs/advanced/java-client/#http-client-wrapper

https://netflix.github.io/dgs/generating-code-from-schema/#generating-client-apis.

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

【讨论】:

  • 感谢分享这个,当我添加 graphql-dgs-spring-boot-starter 这个依赖然后尝试创建 DefaultGraphQLClient 的实例时,它说无法解析符号。你能帮我解决我在这里想念的吗?
  • 将 com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter 依赖项添加到您的 Gradle 或 Maven 配置中,并像导入任何其他 java 一样导入该类。参考:netflix.github.io/dgs/getting-started。感谢您是否可以将以上内容标记为有用 (UP) 以及是否将其正确选择为正确答案。
  • 现在我收到此错误`在类路径资源 [com/netflix/graphql/dgs/autoconfig/DgsAutoConfiguration.class] 中定义名称为“schema”的 bean 创建时出错:bean 创建期间出现意外异常;嵌套异常是 java.lang.TypeNotPresentException:类型 graphql.schema.GraphQLCodeRegistry 不存在`。这是几分钟前工作的......现在我开始收到这个错误
  • 为了客户目的,只需要 graphql-dgs-client 依赖
猜你喜欢
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
  • 2020-07-14
  • 2012-09-27
  • 2018-04-22
  • 2021-09-02
  • 2013-01-28
  • 1970-01-01
相关资源
最近更新 更多