package helper import ( "fmt" "github.com/gshopify/service-wrapper/fun" "github.com/gshopify/service-wrapper/model" "gshopper.com/gshopify/products/graphql/generated" ) func NewCollectionConnection(src []*generated.Collection, after *string, before *string, first *int, last *int, reverse *bool) (*generated.CollectionConnection, error) { var ( connection = &generated.CollectionConnection{} 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) if connection.Nodes, err = fun.WithCursor(connection.Nodes, after, model.GidCollection, true); err != nil { return nil, fmt.Errorf("illegal cursor: %s", *after) } if connection.Nodes, err = fun.WithCursor(connection.Nodes, before, model.GidCollection, false); err != nil { return nil, fmt.Errorf("illegal cursor: %s", *before) } connection.PageInfo = model.NewPageInfo(connection.Nodes, model.GidCollection, src[0], src[len(src)-1]) // Edges for i := range connection.Nodes { e := &generated.CollectionEdge{ 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 }