Browse Source

Connection

- follow GraphQL specs
Alexey Kim 1 year ago
parent
commit
1ae6368983
1 changed files with 25 additions and 19 deletions
  1. 25 19
      model/connection.go

+ 25 - 19
model/connection.go

@@ -9,26 +9,31 @@ import (
 )
 )
 
 
 type Connection[T connection.Cursor] struct {
 type Connection[T connection.Cursor] struct {
-	Nodes    []T       `json:"nodes"`
+	Edges    []Edge[T] `json:"edges"`
 	PageInfo *PageInfo `json:"page_info"`
 	PageInfo *PageInfo `json:"page_info"`
 }
 }
 
 
+type Edge[T connection.Cursor] struct {
+	Node   T      `json:"node"`
+	Cursor string `json:"cursor"`
+}
+
 func (c *Connection[T]) Reverse() {
 func (c *Connection[T]) Reverse() {
-	c.Nodes = lo.Reverse[T](c.Nodes)
+	c.Edges = lo.Reverse[Edge[T]](c.Edges)
 }
 }
 
 
 func (c *Connection[T]) Grain(after, before *string) error {
 func (c *Connection[T]) Grain(after, before *string) error {
 	if after != nil && before != nil {
 	if after != nil && before != nil {
-		return fmt.Errorf("illegal agruments: only one of `after` and `before` can be used at a time")
+		return fmt.Errorf("illegal agruments: only one of `after` or `before` can be used at a time")
 	}
 	}
 
 
-	if len(c.Nodes) == 0 {
+	if len(c.Edges) == 0 {
 		c.PageInfo = &PageInfo{}
 		c.PageInfo = &PageInfo{}
 		return nil
 		return nil
 	}
 	}
 
 
-	edges := lo.FlatMap[T, string](c.Nodes, func(item T, _ int) []string {
-		return []string{item.Cursor()}
+	edges := lo.FlatMap[Edge[T], string](c.Edges, func(item Edge[T], _ int) []string {
+		return []string{item.Cursor}
 	})
 	})
 
 
 	c.PageInfo = &PageInfo{
 	c.PageInfo = &PageInfo{
@@ -38,13 +43,13 @@ func (c *Connection[T]) Grain(after, before *string) error {
 
 
 	if after != nil {
 	if after != nil {
 		if idx := lo.IndexOf(edges, *after); idx != -1 {
 		if idx := lo.IndexOf(edges, *after); idx != -1 {
-			c.Nodes = lo.Slice[T](c.Nodes, idx+1, math.MaxUint32)
+			c.Edges = lo.Slice[Edge[T]](c.Edges, idx+1, math.MaxUint32)
 		}
 		}
 	}
 	}
 
 
 	if before != nil {
 	if before != nil {
 		if idx := lo.IndexOf(edges, *before); idx != -1 {
 		if idx := lo.IndexOf(edges, *before); idx != -1 {
-			c.Nodes = lo.Subset[T](c.Nodes, -idx, math.MaxUint32)
+			c.Edges = lo.Subset[Edge[T]](c.Edges, -idx, math.MaxUint32)
 		}
 		}
 	}
 	}
 
 
@@ -53,40 +58,41 @@ func (c *Connection[T]) Grain(after, before *string) error {
 
 
 func (c *Connection[T]) Slice(first, last *int) error {
 func (c *Connection[T]) Slice(first, last *int) error {
 	if first != nil && last != nil {
 	if first != nil && last != nil {
-		return fmt.Errorf("illegal agruments: only one of `first` and `last` can be used at a time")
+		return fmt.Errorf("illegal agruments: only one of `first` or `last` can be used at a time")
 	}
 	}
 
 
 	if first != nil {
 	if first != nil {
-		c.Nodes = lo.Slice[T](c.Nodes, 0, *first)
+		c.Edges = lo.Slice[Edge[T]](c.Edges, 0, *first)
 	}
 	}
 
 
 	if last != nil {
 	if last != nil {
-		c.Nodes = lo.Subset[T](c.Nodes, -*last, math.MaxUint32)
+		c.Edges = lo.Subset[Edge[T]](c.Edges, -*last, math.MaxUint32)
 	}
 	}
 
 
-	if len(c.Nodes) < 1 {
+	if len(c.Edges) < 1 {
 		c.PageInfo.HasPreviousPage = c.PageInfo.StartCursor != nil
 		c.PageInfo.HasPreviousPage = c.PageInfo.StartCursor != nil
 		c.PageInfo.HasNextPage = c.PageInfo.EndCursor != nil
 		c.PageInfo.HasNextPage = c.PageInfo.EndCursor != nil
 		c.PageInfo.StartCursor = nil
 		c.PageInfo.StartCursor = nil
 		c.PageInfo.EndCursor = nil
 		c.PageInfo.EndCursor = nil
 	} else {
 	} else {
-		c.PageInfo.HasPreviousPage = !(*c.PageInfo.StartCursor == c.Nodes[0].Cursor())
-		c.PageInfo.HasNextPage = !(*c.PageInfo.EndCursor == c.Nodes[len(c.Nodes)-1].Cursor())
-		c.PageInfo.StartCursor = lo.ToPtr(c.Nodes[0].Cursor())
-		c.PageInfo.EndCursor = lo.ToPtr(c.Nodes[len(c.Nodes)-1].Cursor())
+		c.PageInfo.HasPreviousPage = !(*c.PageInfo.StartCursor == c.Edges[0].Cursor)
+		c.PageInfo.HasNextPage = !(*c.PageInfo.EndCursor == c.Edges[len(c.Edges)-1].Cursor)
+		c.PageInfo.StartCursor = lo.ToPtr(c.Edges[0].Cursor)
+		c.PageInfo.EndCursor = lo.ToPtr(c.Edges[len(c.Edges)-1].Cursor)
 	}
 	}
 
 
 	return nil
 	return nil
 }
 }
 
 
 func (c Connection[T]) Marshal() ([]byte, error) {
 func (c Connection[T]) Marshal() ([]byte, error) {
-	return json.Marshal(c.Nodes)
+	return json.Marshal(c.Edges)
 }
 }
 
 
 func (c *Connection[T]) Unmarshal(data []byte) error {
 func (c *Connection[T]) Unmarshal(data []byte) error {
-	return json.Unmarshal(data, &c.Nodes)
+	return json.Unmarshal(data, &c.Edges)
 }
 }
 
 
 func (c Connection[T]) Key() string {
 func (c Connection[T]) Key() string {
-	return fmt.Sprintf("connection-nodes-%s", "match")
+	var zero T
+	return fmt.Sprintf("connection-edges-%T", zero)
 }
 }