Spring Boot GraphQL 客户端:传递对象列表的完整指南

spring boot graphql 客户端:传递对象列表的完整指南

本文档旨在指导开发者如何在 Spring Boot GraphQL 客户端中传递对象列表。我们将探讨如何构建 GraphQL 查询,并使用 `GraphQLTemplate` 发送包含对象列表的请求,从而实现高效的数据交互。

在构建微服务架构时,GraphQL 作为一种强大的 API 查询语言,被广泛应用于数据聚合和交互。当我们需要从 GraphQL 服务端获取或传递对象列表时,客户端的实现方式就显得尤为重要。本文将重点介绍如何在 Spring Boot 环境下,使用 GraphQLTemplate 构建 GraphQL 客户端,并传递对象列表作为请求参数。

GraphQL Schema 定义

首先,我们需要明确 GraphQL 服务端定义的 Schema。假设我们有一个 Person 类型,其定义如下:

type Person {    firstName: String    middleName: String    lastName: String    birthDt: String}type Query {    getPersonsByIds(personIds: [BigInteger]): [Person]}

在这个 Schema 中,getPersonsByIds 查询接受一个 BigInteger 类型的列表 personIds 作为参数,并返回一个 Person 类型的列表。

构建 GraphQL 客户端

接下来,我们将使用 GraphQLTemplate 构建客户端,并发送包含 personIds 列表的请求。

import com.github.americanexpress.nodes.graphql.GraphQLRequestEntity;import com.github.americanexpress.nodes.graphql.GraphQLTemplate;import org.springframework.stereotype.Service;import java.math.BigInteger;import java.net.MalformedURLException;import java.util.Arrays;import java.util.List;@Servicepublic class PersonService {    private final GraphQLTemplate graphQLTemplate = new GraphQLTemplate();    private final String url = "http://localhost:8084/graphql";    public List getPersonsByIds(List personIds) {        GraphQLRequestEntity requestEntity;        try {            requestEntity = GraphQLRequestEntity.Builder()                .url(url)                .requestMethod(GraphQLTemplate.GraphQLMethod.QUERY)                .request("query($personIds: [BigInteger]) {n" +                    "  getPersonsByIds(personIds : $personIds ) {n" +                    "    firstNamen" +                    "    middleNamen" +                    "    lastNamen" +                    "    birthDtn" +                    "  }n" +                    "}"                )                .variables(new Variable("personIds", personIds))                .build();        } catch (MalformedURLException e) {            throw new RuntimeException(e);        }        return graphQLTemplate.query(requestEntity, ResponseGetPersonsByIds.class).getResponse().getGetPersonsByIds();    }    // 内部类,用于封装变量    private static class Variable {        private final String name;        private final T value;        public Variable(String name, T value) {            this.name = name;            this.value = value;        }        public String getName() {            return name;        }        public T getValue() {            return value;        }    }    // 假设的 ResponseGetPersonsByIds 类,需要根据实际情况定义    private static class ResponseGetPersonsByIds {        private Response response;        public Response getResponse() {            return response;        }        public void setResponse(Response response) {            this.response = response;        }        private static class Response {            private List getPersonsByIds;            public List getGetPersonsByIds() {                return getPersonsByIds;            }            public void setGetPersonsByIds(List getPersonsByIds) {                this.getPersonsByIds = getPersonsByIds;            }        }    }    // 假设的 Person 类,需要根据实际情况定义    private static class Person {        private String firstName;        private String middleName;        private String lastName;        private String birthDt;        // Getters and setters        public String getFirstName() {            return firstName;        }        public void setFirstName(String firstName) {            this.firstName = firstName;        }        public String getMiddleName() {            return middleName;        }        public void setMiddleName(String middleName) {            this.middleName = middleName;        }        public String getLastName() {            return lastName;        }        public void setLastName(String lastName) {            this.lastName = lastName;        }        public String getBirthDt() {            return birthDt;        }        public void setBirthDt(String birthDt) {            this.birthDt = birthDt;        }    }}

代码解释:

GraphQL 查询: 定义了 GraphQL 查询字符串,其中 $personIds 是一个变量,用于传递 BigInteger 类型的列表。

知我AI·PC客户端 知我AI·PC客户端

离线运行 AI 大模型,构建你的私有个人知识库,对话式提取文件知识,保证个人文件数据安全

知我AI·PC客户端 0 查看详情 知我AI·PC客户端

GraphQLRequestEntity: 使用 GraphQLRequestEntity.Builder 构建请求实体,设置 URL、请求方法和查询字符串。

Variables: 使用 variables 方法传递参数。关键在于将 personIds 列表作为 personIds 变量的值传递给 GraphQL 查询。 这里创建了一个内部类Variable,目的是为了能够更方便地传递变量的名称和值。

graphQLTemplate.query(): 执行查询,并使用 ResponseGetPersonsByIds.class 将响应映射到 Java 对象。 ResponseGetPersonsByIds 需要根据实际的 GraphQL 响应结构进行定义。

调用示例:

@Autowiredprivate PersonService personService;public void testGetPersonsByIds() {    List ids = Arrays.asList(new BigInteger("2477142261427744786"), new BigInteger("2477142261427744787"));    List persons = personService.getPersonsByIds(ids);    System.out.println(persons);}

注意事项:

确保 ResponseGetPersonsByIds 类能够正确映射 GraphQL 响应。根据实际情况调整 URL 和查询字符串。处理可能出现的 MalformedURLException 异常。com.github.americanexpress:nodes:0.5.0 库可能不是最新的,建议查看是否有更新的版本。

总结

通过本文,我们学习了如何在 Spring Boot GraphQL 客户端中使用 GraphQLTemplate 传递对象列表。关键在于正确构建 GraphQL 查询,并使用 variables 方法将列表作为参数传递给查询。这种方法可以有效地实现客户端与服务端之间的数据交互,提高开发效率。记住,需要根据实际的 GraphQL Schema 和响应结构调整代码,才能确保程序的正确运行。

以上就是Spring Boot GraphQL 客户端:传递对象列表的完整指南的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/299491.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
即梦的收费标准是什么_即梦收费标准解析
上一篇 2025年11月5日 00:17:18
如何使用ThinkPHP6实现单页面应用程序
下一篇 2025年11月5日 00:17:26

相关推荐

发表回复

登录后才能评论
关注微信