graphql: fix two bugs in the connection code

Michael Muré created

1) totalCount was incorrect when not in the first page
2) pageInfo.Has{Previous,Next}Page was incorrect when using before or after

Change summary

graphql/connections/connection_template.go | 5 ++++-
graphql/connections/gen_bug.go             | 5 ++++-
graphql/connections/gen_comment.go         | 5 ++++-
graphql/connections/gen_operation.go       | 5 ++++-
4 files changed, 16 insertions(+), 4 deletions(-)

Detailed changes

graphql/connections/connection_template.go 🔗

@@ -33,6 +33,7 @@ func NodeTypeCon(source []NodeType, edgeMaker NodeTypeEdgeMaker, conMaker NodeTy
 	var edges []EdgeType
 	var cursors []string
 	var pageInfo models.PageInfo
+	var totalCount = len(source)
 
 	emptyCon, _ := conMaker(edges, nodes, pageInfo, 0)
 
@@ -45,6 +46,7 @@ func NodeTypeCon(source []NodeType, edgeMaker NodeTypeEdgeMaker, conMaker NodeTy
 				// remove all previous element including the "after" one
 				source = source[i+1:]
 				offset = i + 1
+				pageInfo.HasPreviousPage = true
 				break
 			}
 		}
@@ -56,6 +58,7 @@ func NodeTypeCon(source []NodeType, edgeMaker NodeTypeEdgeMaker, conMaker NodeTy
 
 			if edge.GetCursor() == *input.Before {
 				// remove all after element including the "before" one
+				pageInfo.HasNextPage = true
 				break
 			}
 
@@ -109,5 +112,5 @@ func NodeTypeCon(source []NodeType, edgeMaker NodeTypeEdgeMaker, conMaker NodeTy
 		pageInfo.EndCursor = cursors[len(cursors)-1]
 	}
 
-	return conMaker(edges, nodes, pageInfo, len(source))
+	return conMaker(edges, nodes, pageInfo, totalCount)
 }

graphql/connections/gen_bug.go 🔗

@@ -27,6 +27,7 @@ func StringCon(source []string, edgeMaker StringEdgeMaker, conMaker StringConMak
 	var edges []LazyBugEdge
 	var cursors []string
 	var pageInfo models.PageInfo
+	var totalCount = len(source)
 
 	emptyCon, _ := conMaker(edges, nodes, pageInfo, 0)
 
@@ -39,6 +40,7 @@ func StringCon(source []string, edgeMaker StringEdgeMaker, conMaker StringConMak
 				// remove all previous element including the "after" one
 				source = source[i+1:]
 				offset = i + 1
+				pageInfo.HasPreviousPage = true
 				break
 			}
 		}
@@ -50,6 +52,7 @@ func StringCon(source []string, edgeMaker StringEdgeMaker, conMaker StringConMak
 
 			if edge.GetCursor() == *input.Before {
 				// remove all after element including the "before" one
+				pageInfo.HasNextPage = true
 				break
 			}
 
@@ -103,5 +106,5 @@ func StringCon(source []string, edgeMaker StringEdgeMaker, conMaker StringConMak
 		pageInfo.EndCursor = cursors[len(cursors)-1]
 	}
 
-	return conMaker(edges, nodes, pageInfo, len(source))
+	return conMaker(edges, nodes, pageInfo, totalCount)
 }

graphql/connections/gen_comment.go 🔗

@@ -28,6 +28,7 @@ func BugCommentCon(source []bug.Comment, edgeMaker BugCommentEdgeMaker, conMaker
 	var edges []models.CommentEdge
 	var cursors []string
 	var pageInfo models.PageInfo
+	var totalCount = len(source)
 
 	emptyCon, _ := conMaker(edges, nodes, pageInfo, 0)
 
@@ -40,6 +41,7 @@ func BugCommentCon(source []bug.Comment, edgeMaker BugCommentEdgeMaker, conMaker
 				// remove all previous element including the "after" one
 				source = source[i+1:]
 				offset = i + 1
+				pageInfo.HasPreviousPage = true
 				break
 			}
 		}
@@ -51,6 +53,7 @@ func BugCommentCon(source []bug.Comment, edgeMaker BugCommentEdgeMaker, conMaker
 
 			if edge.GetCursor() == *input.Before {
 				// remove all after element including the "before" one
+				pageInfo.HasNextPage = true
 				break
 			}
 
@@ -104,5 +107,5 @@ func BugCommentCon(source []bug.Comment, edgeMaker BugCommentEdgeMaker, conMaker
 		pageInfo.EndCursor = cursors[len(cursors)-1]
 	}
 
-	return conMaker(edges, nodes, pageInfo, len(source))
+	return conMaker(edges, nodes, pageInfo, totalCount)
 }

graphql/connections/gen_operation.go 🔗

@@ -28,6 +28,7 @@ func BugOperationCon(source []bug.Operation, edgeMaker BugOperationEdgeMaker, co
 	var edges []models.OperationEdge
 	var cursors []string
 	var pageInfo models.PageInfo
+	var totalCount = len(source)
 
 	emptyCon, _ := conMaker(edges, nodes, pageInfo, 0)
 
@@ -40,6 +41,7 @@ func BugOperationCon(source []bug.Operation, edgeMaker BugOperationEdgeMaker, co
 				// remove all previous element including the "after" one
 				source = source[i+1:]
 				offset = i + 1
+				pageInfo.HasPreviousPage = true
 				break
 			}
 		}
@@ -51,6 +53,7 @@ func BugOperationCon(source []bug.Operation, edgeMaker BugOperationEdgeMaker, co
 
 			if edge.GetCursor() == *input.Before {
 				// remove all after element including the "before" one
+				pageInfo.HasNextPage = true
 				break
 			}
 
@@ -104,5 +107,5 @@ func BugOperationCon(source []bug.Operation, edgeMaker BugOperationEdgeMaker, co
 		pageInfo.EndCursor = cursors[len(cursors)-1]
 	}
 
-	return conMaker(edges, nodes, pageInfo, len(source))
+	return conMaker(edges, nodes, pageInfo, totalCount)
 }