12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package helper
- import (
- "fmt"
- "github.com/gshopify/service-wrapper/fun"
- "github.com/gshopify/service-wrapper/model"
- "gshopper.com/gshopify/products/graphql/generated"
- )
- func NewProductConnection(src []*generated.Product,
- after *string, before *string, first *int, last *int, reverse *bool) (*generated.ProductConnection, error) {
- var (
- connection = &generated.ProductConnection{}
- err error
- )
- if len(src) == 0 {
- return connection, nil
- }
- if reverse != nil && *reverse {
- src = fun.Reverse(src)
- }
- connection.Nodes = src[:]
- connection.Nodes = fun.First(connection.Nodes, first)
- connection.Nodes = fun.Last(connection.Nodes, last)
- connection.Nodes, err = fun.WithCursor(connection.Nodes, after, model.GidProduct, true)
- if err != nil {
- return nil, fmt.Errorf("illegal cursor: %s", *after)
- }
- connection.Nodes, err = fun.WithCursor(connection.Nodes, before, model.GidProduct, false)
- if err != nil {
- return nil, fmt.Errorf("illegal cursor: %s", *before)
- }
- connection.PageInfo = NewPageInfo(connection.Nodes, model.GidProduct, src[0], src[len(src)-1])
- // Edges
- for i := range connection.Nodes {
- e := &generated.ProductEdge{
- Node: connection.Nodes[i],
- }
- if c, err := model.NewSimpleCursor(e.Node.ID, model.GidCollection); err == nil {
- e.Cursor = *c.String()
- }
- connection.Edges = append(connection.Edges, e)
- }
- return connection, nil
- }
|