
本文档旨在指导开发者如何在 Spring Boot GraphQL 客户端中传递对象列表进行查询。我们将探讨如何构建 GraphQL 查询,并使用 `GraphQLTemplate` 发送包含对象列表的请求。通过本文,你将学会如何有效地与 GraphQL 服务交互,并获取所需的数据。
在使用 Spring Boot 构建 GraphQL 客户端时,一个常见的需求是向 GraphQL 服务传递一个对象列表作为查询参数。这在需要根据多个 ID 或其他属性检索数据时非常有用。以下是如何使用 GraphQLTemplate 实现此功能的详细步骤。
1. 定义 GraphQL Schema (服务端)
首先,确保你的 GraphQL 服务端定义了正确的 schema,允许接收对象列表作为参数。例如,如果你的服务端有一个 Person 类型,并且你想通过 ID 列表查询多个 Person 对象,你的 schema 可能如下所示:
type Person {
  firstName: String
  middleName: String
  lastName: String
  birthDt: String
}
type Query {
  getPersonsByIds(personIds: [ID!]): [Person]
}这里的 getPersonsByIds 查询接受一个 personIds 参数,它是一个 ID 类型的非空列表,并返回一个 Person 对象的列表。
2. 构建 GraphQL 查询 (客户端)
在 Spring Boot 客户端,你需要构建一个 GraphQL 查询,其中包含一个变量,该变量将绑定到你的对象列表。例如:
query GetPersonsByIds($personIds: [BigInteger]) {
  getPersonsByIds(personIds: $personIds) {
    firstName
    middleName
    lastName
    birthDt
  }
}注意 $personIds: [BigInteger] 部分,它定义了一个名为 personIds 的变量,该变量的类型是 BigInteger 的列表。 确保服务端声明的ID类型与客户端保持一致,例如服务端ID是String,客户端也需要声明为String。
3. 使用 GraphQLTemplate 发送请求
现在,你可以使用 GraphQLTemplate 发送包含对象列表的请求。以下是一个示例:
import com.github.americanexpress.graphql.spring.GraphQLTemplate;
import com.github.americanexpress.graphql.spring.GraphQLRequestEntity;
import org.springframework.stereotype.Service;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;
import java.math.BigInteger;
@Service
public class PersonService {
    private final GraphQLTemplate graphQLTemplate = new GraphQLTemplate();
    private final String url = "http://localhost:8084/graphql";
    public List<Person> getPersonsByIds(List<BigInteger> personIds) {
        GraphQLRequestEntity requestEntity;
        try {
            requestEntity = GraphQLRequestEntity.Builder()
                .url(url)
                .requestMethod(GraphQLTemplate.GraphQLMethod.QUERY)
                .request("query GetPersonsByIds($personIds: [BigInteger]) {\n" +
                    "  getPersonsByIds(personIds: $personIds) {\n" +
                    "    firstName\n" +
                    "    middleName\n" +
                    "    lastName\n" +
                    "    birthDt\n" +
                    "  }\n" +
                    "}"
                )
                .variables(new Variable<>("personIds", personIds)) // 传递 personIds 列表
                .build();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        return graphQLTemplate.query(requestEntity, ResponseGetPersonsByIds.class).getResponse().getGetPersonsByIds();
    }
    public static class Variable<T> {
        private final String key;
        private final T value;
        public Variable(String key, T value) {
            this.key = key;
            this.value = value;
        }
        public String getKey() {
            return key;
        }
        public T getValue() {
            return value;
        }
    }
    // 假设的 ResponseGetPersonsByIds 类
    public static class ResponseGetPersonsByIds {
        private GetPersonsByIdsResponse response;
        public GetPersonsByIdsResponse getResponse() {
            return response;
        }
        public void setResponse(GetPersonsByIdsResponse response) {
            this.response = response;
        }
    }
    public static class GetPersonsByIdsResponse {
        private List<Person> getPersonsByIds;
        public List<Person> getGetPersonsByIds() {
            return getPersonsByIds;
        }
        public void setGetPersonsByIds(List<Person> getPersonsByIds) {
            this.getPersonsByIds = getPersonsByIds;
        }
    }
    public 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;
        }
    }
    public static void main(String[] args) {
        PersonService personService = new PersonService();
        List<BigInteger> ids = Arrays.asList(new BigInteger("2477142261427744786"), new BigInteger("2477142261427744787"));
        List<Person> persons = personService.getPersonsByIds(ids);
        if (persons != null) {
            persons.forEach(person -> System.out.println(person.getFirstName()));
        } else {
            System.out.println("No persons found.");
        }
    }
}在这个例子中,personIds 是一个 BigInteger 类型的列表,它作为 variables 参数传递给 GraphQLRequestEntity.Builder()。
4. 处理响应
graphQLTemplate.query() 方法返回一个包含响应数据的对象。你需要定义相应的 Java 类来映射 GraphQL 响应。在上面的例子中,ResponseGetPersonsByIds 类用于封装响应数据。
注意事项:
总结:
通过使用 GraphQLTemplate 和正确构建 GraphQL 查询,你可以轻松地将对象列表作为参数传递给 Spring Boot GraphQL 客户端中的查询。 重要的是要确保服务端和客户端之间的类型匹配,并正确处理响应数据。 这种方法可以有效地检索与多个 ID 或其他属性相关的数据。
以上就是GraphQL Spring Boot Client:处理对象列表的查询的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
 
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号