When you start using Spring Boot GraphQL repositories, pagination often works beautifully right out of the box. You can filter data, fetch specific fields, and even handle joins without much hassle. But as soon as you try to sort the paginated results, things suddenly stop being so straightforward. You might notice that no matter how you tweak your GraphQL query, the data comes back unsorted — or worse, in the default order determined by your database. The reason is simple: GraphQL doesn’t automatically map sorting parameters from the client to the Spring Data pagination layer unless you explicitly define how sorting should behave in your schema and backend logic.
This can be confusing, especially when you’re already using @GraphQlRepository and JpaRepository together. You expect sorting to just work — like it does with a normal REST endpoint using Pageable and Sort. But GraphQL queries don’t have any awareness of Spring’s sorting model unless you make that connection yourself. So, to fix this, we need to bridge that gap manually.
Now, we’ll see how to implement sorting in paginated GraphQL queries when using @GraphQlRepository. You’ll learn how to define a sorting input in your schema, map it in your service layer, and wire it all together so your clients can sort results dynamically — just like they would in a well-behaved REST API.
How to Sort @GraphQlRepository Paginated Results in Spring Boot?
In this section, we’re going to make sorting work alongside pagination in a Spring Boot GraphQL setup. We’ll modify the GraphQL schema, adjust the backend logic, and ensure your repository methods can handle custom sort orders.
Step 1: Update Your GraphQL Schema to Include Sorting Input
Your existing schema only supports filtering and pagination. To sort results, you need to define a custom input type for sorting fields. For example:
input SortInput {
field: String!
direction: SortDirection!
}
enum SortDirection {
ASC
DESC
}
SQLThen modify your query definition to accept this input:
type Query {
users(
first: Int,
after: String,
last: Int,
before: String,
user: UserInput,
sort: [SortInput]
): UserConnection
}
SQLThis allows clients to send queries like:
query {
users(
user: { firstname: "Max" },
first: 20,
sort: [{ field: "lastname", direction: DESC }]
) {
edges {
node {
id
username
firstname
lastname
}
}
}
}
SQLStep 2: Handle Sorting in Your Data Fetcher or Service Layer
Spring Data JPA supports sorting through the Sort and PageRequest classes. You’ll just need to map your incoming GraphQL SortInput to a Sort object.
Example service layer code:
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
public Page<User> getUsers(UserInput input, int first, List<SortInput> sortInputs) {
Sort sort = Sort.unsorted();
if (sortInputs != null && !sortInputs.isEmpty()) {
List<Sort.Order> orders = sortInputs.stream()
.map(s -> new Sort.Order(
s.getDirection().equalsIgnoreCase("DESC") ? Sort.Direction.DESC : Sort.Direction.ASC,
s.getField()
))
.toList();
sort = Sort.by(orders);
}
PageRequest pageRequest = PageRequest.of(0, first, sort);
return userRepository.findAll(pageRequest);
}
SQLHere, SortInput corresponds to the GraphQL input. You map the GraphQL sorting fields to Spring’s Sort.Order before passing it into your PageRequest.
Step 3: Repository Definition
No changes are required in your repository itself since it already extends JpaRepository and JpaSpecificationExecutor.
@GraphQlRepository
public interface UserRepository extends BaseRepository<User, Long> {
}
The magic happens in how you call it from your service using findAll(Pageable pageable) or a custom specification query.
Step 4: Bind the Sorting Logic to GraphQL Resolver
If you’re using a manual @SchemaMapping or @QueryMapping, wire your service call there:
@QueryMapping
public Connection<User> users(
@Argument UserInput user,
@Argument Integer first,
@Argument List<SortInput> sort
) {
Page<User> page = userService.getUsers(user, first, sort);
return new SimpleConnection<>(page.getContent(), page.hasNext(), page.hasPrevious());
}
SQLThis way, the sorting works transparently with your pagination and filter logic.
Final Output Example
GraphQL Query:
query {
users(
first: 10,
sort: [{ field: "lastname", direction: ASC }]
) {
edges {
node {
id
username
lastname
}
}
}
}
SQLSQL Generated:
select * from users order by lastname asc limit 10;
Conclusion
Sorting with @GraphQlRepository and pagination in Spring Boot isn’t enabled automatically — you must explicitly define a sorting input type in your GraphQL schema and map it to Spring Data’s Sort and PageRequest. Once done, sorting works seamlessly across paginated queries, filters, and joins.