From 49ff3eb0a019d7968d99ad33b0c47f913f53ee07 Mon Sep 17 00:00:00 2001 From: Abhishek Gill Date: Fri, 11 Mar 2022 16:39:11 +0530 Subject: [PATCH] Add server-side pagination capability to GraphQL APIs #20 --- .../shareNwork/resource/AccessRequestResource.java | 11 ++++++++--- .../resource/AllowedDesignationResource.java | 4 +--- .../com/shareNwork/resource/EmployeeResource.java | 7 +++++++ .../com/shareNwork/resource/InvitationResource.java | 8 ++++++++ .../java/com/shareNwork/resource/ProjectResource.java | 8 ++++++++ .../com/shareNwork/resource/SRRequestResource.java | 11 ++++++++--- .../java/com/shareNwork/resource/SharedResource.java | 6 ++++++ .../java/com/shareNwork/resource/SkillResource.java | 8 ++++++++ 8 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/shareNwork/resource/AccessRequestResource.java b/src/main/java/com/shareNwork/resource/AccessRequestResource.java index 584f0ca..be6de82 100644 --- a/src/main/java/com/shareNwork/resource/AccessRequestResource.java +++ b/src/main/java/com/shareNwork/resource/AccessRequestResource.java @@ -2,17 +2,16 @@ import java.util.List; -import javax.transaction.TransactionScoped; import javax.transaction.Transactional; import com.shareNwork.domain.AccessRequest; -import com.shareNwork.domain.Invitation; -import com.shareNwork.domain.InvitationResponse; import com.shareNwork.domain.constants.InvitationStatus; +import io.quarkus.panache.common.Page; import lombok.AllArgsConstructor; import org.eclipse.microprofile.graphql.Description; import org.eclipse.microprofile.graphql.GraphQLApi; import org.eclipse.microprofile.graphql.Mutation; +import org.eclipse.microprofile.graphql.Name; import org.eclipse.microprofile.graphql.Query; @AllArgsConstructor @@ -25,6 +24,12 @@ public List findAllRequests() { return AccessRequest.listAll(); } + @Query("accessRequestsByPage") + @Description("Get AccessRequests with pagination") + public List getAccessRequestsByPage(@Name("pageSize") int pageSize, @Name("pageOffset") int pageOffset) { + return AccessRequest.findAll().page(Page.of(pageOffset, pageSize)).list(); + } + @Mutation @Description("Create a new access request") @Transactional diff --git a/src/main/java/com/shareNwork/resource/AllowedDesignationResource.java b/src/main/java/com/shareNwork/resource/AllowedDesignationResource.java index d3bfd79..5a1b3df 100644 --- a/src/main/java/com/shareNwork/resource/AllowedDesignationResource.java +++ b/src/main/java/com/shareNwork/resource/AllowedDesignationResource.java @@ -1,9 +1,7 @@ package com.shareNwork.resource; import com.shareNwork.domain.AllowedDesignationResponse; -import com.shareNwork.domain.InvitationResponse; import com.shareNwork.repository.AllowedDesignationRepository; -import com.shareNwork.repository.EmployeeRepository; import lombok.AllArgsConstructor; import org.eclipse.microprofile.graphql.Description; import org.eclipse.microprofile.graphql.GraphQLApi; @@ -15,7 +13,7 @@ public class AllowedDesignationResource { private AllowedDesignationRepository allowedDesignationRepository; @Mutation - @Description("Check whether desg8ination is whitelisted") + @Description("Check whether designation is whitelisted") public AllowedDesignationResponse verifyDesignation(String designation) { return this.allowedDesignationRepository.verify(designation); } diff --git a/src/main/java/com/shareNwork/resource/EmployeeResource.java b/src/main/java/com/shareNwork/resource/EmployeeResource.java index ac860d9..8bd6d84 100644 --- a/src/main/java/com/shareNwork/resource/EmployeeResource.java +++ b/src/main/java/com/shareNwork/resource/EmployeeResource.java @@ -3,6 +3,7 @@ import com.shareNwork.domain.Employee; import com.shareNwork.domain.filters.EmployeeFilter; import com.shareNwork.repository.EmployeeRepository; +import io.quarkus.panache.common.Page; import lombok.AllArgsConstructor; import org.eclipse.microprofile.graphql.*; @@ -21,6 +22,12 @@ public List findAll() { return this.employeeRepository.findAll().list(); } + @Query("employeesByPage") + @Description("Get employees with pagination") + public List getEmployeesByPage(@Name("pageSize") int pageSize, @Name("pageOffset") int pageOffset) { + return this.employeeRepository.findAll().page(Page.of(pageOffset, pageSize)).list(); + } + @Query("employeesWithFilter") @Description("Get all Employees using the filters eq, lt,le,gt,ge") public List findWithFilter(@Name("filter") EmployeeFilter filter) { diff --git a/src/main/java/com/shareNwork/resource/InvitationResource.java b/src/main/java/com/shareNwork/resource/InvitationResource.java index 61335fa..802c813 100644 --- a/src/main/java/com/shareNwork/resource/InvitationResource.java +++ b/src/main/java/com/shareNwork/resource/InvitationResource.java @@ -3,10 +3,12 @@ import com.shareNwork.domain.Invitation; import com.shareNwork.domain.InvitationResponse; import com.shareNwork.repository.InvitationRepository; +import io.quarkus.panache.common.Page; import lombok.AllArgsConstructor; import org.eclipse.microprofile.graphql.Description; import org.eclipse.microprofile.graphql.GraphQLApi; import org.eclipse.microprofile.graphql.Mutation; +import org.eclipse.microprofile.graphql.Name; import org.eclipse.microprofile.graphql.Query; import java.util.List; @@ -29,6 +31,12 @@ public Invitation getInvitationById(long id) { return this.invitationRepository.findById(id); } + @Query("invitationsByPage") + @Description("Get Invitations with pagination") + public List getInvitationsByPage(@Name("pageSize") int pageSize, @Name("pageOffset") int pageOffset) { + return this.invitationRepository.findAll().page(Page.of(pageOffset, pageSize)).list(); + } + @Mutation @Description("Create a new token") public InvitationResponse createInvitationToken(Invitation invitation) { diff --git a/src/main/java/com/shareNwork/resource/ProjectResource.java b/src/main/java/com/shareNwork/resource/ProjectResource.java index bb513bc..9ebbc3b 100644 --- a/src/main/java/com/shareNwork/resource/ProjectResource.java +++ b/src/main/java/com/shareNwork/resource/ProjectResource.java @@ -2,10 +2,12 @@ import com.shareNwork.domain.Project; import com.shareNwork.repository.ProjectRepository; +import io.quarkus.panache.common.Page; import lombok.AllArgsConstructor; import org.eclipse.microprofile.graphql.Description; import org.eclipse.microprofile.graphql.GraphQLApi; import org.eclipse.microprofile.graphql.Mutation; +import org.eclipse.microprofile.graphql.Name; import org.eclipse.microprofile.graphql.Query; import java.text.ParseException; @@ -23,6 +25,12 @@ public List findAll() { return this.projectRepository.findAll().list(); } + @Query("projectsByPage") + @Description("Get projects with pagination") + public List getProjectsByPage(@Name("pageSize") int pageSize, @Name("pageOffset") int pageOffset) { + return this.projectRepository.findAll().page(Page.of(pageOffset, pageSize)).list(); + } + @Mutation @Description("Create a new Employee") public Project createProject(Project project) throws ParseException { diff --git a/src/main/java/com/shareNwork/resource/SRRequestResource.java b/src/main/java/com/shareNwork/resource/SRRequestResource.java index 4b40f36..4add6d2 100644 --- a/src/main/java/com/shareNwork/resource/SRRequestResource.java +++ b/src/main/java/com/shareNwork/resource/SRRequestResource.java @@ -2,19 +2,18 @@ import com.shareNwork.domain.ResourceRequest; import com.shareNwork.domain.ResourceRequestSkillsProficiency; -import com.shareNwork.domain.SharedResource; import com.shareNwork.repository.ResourceRequestRepository; -import com.shareNwork.repository.SharedResourceRepository; +import io.quarkus.panache.common.Page; import lombok.AllArgsConstructor; import org.eclipse.microprofile.graphql.Description; import org.eclipse.microprofile.graphql.GraphQLApi; import org.eclipse.microprofile.graphql.Mutation; +import org.eclipse.microprofile.graphql.Name; import org.eclipse.microprofile.graphql.Query; import javax.transaction.Transactional; import java.text.ParseException; import java.util.List; -import java.util.Set; @AllArgsConstructor @GraphQLApi @@ -29,6 +28,12 @@ public List getAllSharedResourceRequest() { return resourceRequestRepository.listAll(); } + @Query("sharedResourceRequestsByPage") + @Description("Get SharedResourceRequest with pagination") + public List getAccessRequestsByPage(@Name("pageSize") int pageSize, @Name("pageOffset") int pageOffset) { + return this.resourceRequestRepository.findAll().page(Page.of(pageOffset, pageSize)).list(); + } + @Query("getSkillsByRequestId") @Description("Get required skills of request Id") @Transactional diff --git a/src/main/java/com/shareNwork/resource/SharedResource.java b/src/main/java/com/shareNwork/resource/SharedResource.java index 98fd400..56b8fd5 100644 --- a/src/main/java/com/shareNwork/resource/SharedResource.java +++ b/src/main/java/com/shareNwork/resource/SharedResource.java @@ -4,6 +4,7 @@ import com.shareNwork.domain.EmployeeSkillProficiency; import com.shareNwork.domain.filters.EmployeeFilter; import com.shareNwork.repository.SharedResourceRepository; +import io.quarkus.panache.common.Page; import lombok.AllArgsConstructor; import org.eclipse.microprofile.graphql.*; @@ -24,6 +25,11 @@ public List getAllSharedResource() { return this.sharedResourceRepository.listAll(); } + @Query("sharedResourcesByPage") + @Description("Get SharedResource with pagination") + public List getSharedResourcesByPage(@Name("pageSize") int pageSize, @Name("pageOffset") int pageOffset) { + return this.sharedResourceRepository.findAll().page(Page.of(pageOffset, pageSize)).list(); + } @Query("sharedResourceById") @Description("Get an SR by id") public com.shareNwork.domain.SharedResource getSRById(@Name("id") Long id) { diff --git a/src/main/java/com/shareNwork/resource/SkillResource.java b/src/main/java/com/shareNwork/resource/SkillResource.java index 884abcc..b9b7c20 100644 --- a/src/main/java/com/shareNwork/resource/SkillResource.java +++ b/src/main/java/com/shareNwork/resource/SkillResource.java @@ -2,10 +2,12 @@ import com.shareNwork.domain.Skill; import com.shareNwork.repository.SkillRepository; +import io.quarkus.panache.common.Page; import lombok.AllArgsConstructor; import org.eclipse.microprofile.graphql.Description; import org.eclipse.microprofile.graphql.GraphQLApi; import org.eclipse.microprofile.graphql.Mutation; +import org.eclipse.microprofile.graphql.Name; import org.eclipse.microprofile.graphql.Query; import java.text.ParseException; @@ -22,6 +24,12 @@ public List findAll() { return this.skillRepository.findAll().list(); } + @Query("skillsByPage") + @Description("Get Kills with pagination") + public List getSkillsByPage(@Name("pageSize") int pageSize, @Name("pageOffset") int pageOffset) { + return this.skillRepository.findAll().page(Page.of(pageOffset, pageSize)).list(); + } + @Mutation @Description("Add a new skill") public Skill createSkill(Skill skill) throws ParseException {