简体   繁体   English

如何从 java Z91F1F487C7FB768642B3EB98F683BF5 应用程序调用 GraphQL api? 是否有任何注释支持 graphQL 查询格式?

[英]How to invoke GraphQL api from a java spring-boot application? Is there any annotation which supports graphQL query formation?

I am looking for a solution to implement an GraphQL api call from spring-boot application, having query schema as follows:我正在寻找一种解决方案来实现来自 spring-boot 应用程序的 GraphQL api 调用,其查询架构如下:

query{询问{

getDetailsByRefNumber(RefNumbers: "") getDetailsByRefNumber(RefNumbers: "")

{ {

field1,

field2,

field3 

} } } }

Does anyone have any idea how to implement this?有谁知道如何实现这个? Gone through one of the below links, but didn't find any solution通过以下链接之一,但没有找到任何解决方案

Are there any Java based Graphql client to invoke graphql server from java code? 是否有任何基于 Java 的 Graphql 客户端从 Z93F725A07423FE1C8486ZD4 代码调用 graphql 服务器?

You can use "graphql-webclient-spring-boot-starter" library that is available at:您可以使用以下位置提供的“graphql-webclient-spring-boot-starter”库:

https://github.com/graphql-java-kickstart/graphql-spring-webclient https://github.com/graphql-java-kickstart/graphql-spring-webclient

<!-- https://mvnrepository.com/artifact/com.graphql-java-kickstart/graphql-webclient-spring-boot-starter -->
<dependency>
  <groupId>com.graphql-java-kickstart</groupId>
  <artifactId>graphql-webclient-spring-boot-starter</artifactId>
  <version>1.0.0</version>
</dependency>

A sample implementation can be something like this:示例实现可以是这样的:

Assuming that you have MyEntity object:假设您有 MyEntity object:

public class MyEntity
{
    String field1;
    String field2;
    String field3;
    //getter and setters here
}

Also you have two graphql query files under "src/main/resources" folder:在“src/main/resources”文件夹下还有两个 graphql 查询文件:

query1.graphql:查询1.graphql:

#query1.graphql
#this query returns a list of some_detail_entity
query getDetailListByRefNumber($RefNumber: String!){
    some_detail_entity(where: {RefNumber : { _eq: $RefNumber } }) {
        field1
        field2
        field3 
    } 
}

query2.graphql:查询2.graphql:

#query2.graphql
#this query returns a single some_detail_entity
query getDetailByRefNumber($RefNumber: String!){
    some_detail_entity_by_pk(RefNumber : $RefNumber) {
        field1
        field2
        field3 
    } 
}

You can use this snippet to call graphql server to query & fetch some data:您可以使用此代码段调用 graphql 服务器来查询和获取一些数据:

ObjectMapper objectMapper = new ObjectMapper();

WebClient webClient = WebClient.builder()
.baseUrl("https://endpoint-url-of-graphql.com")//url of graphql instance
.defaultHeader("auth-token", "some-cryptic-code-if-required")//if auth header not required, just delete this line
.build();

GraphQLWebClient graphqlClient = GraphQLWebClient.newInstance(webClient, objectMapper);

//if expecting a single entity (not array)
MyObject entity = graphqlClient.post("query2.graphql", 
Map.of("RefNumber", "A7EED900-9BB4-486F-9F7C-2136E61E2278"), MyEntity.class)
            .block();   

            
//if expecting a list of entity (array)
var response = graphqlClient.post(GraphQLRequest.builder().resource("query1.graphql")
        .variables(Map.of("RefNumber", "A7EED900-9BB4-486F-9F7C-2136E61E2278")).build())
                .block();   

List<MyEntity> entityList = response.getFirstList(MyEntity.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM