string_connection.go 864 B

1234567891011121314151617181920212223242526272829303132333435
  1. package helper
  2. import (
  3. "github.com/gshopify/service-wrapper/fun"
  4. "github.com/gshopify/service-wrapper/interfaces"
  5. "github.com/gshopify/service-wrapper/model"
  6. "gshopper.com/gshopify/products/graphql/generated"
  7. )
  8. func NewStringConnection[T interfaces.Node](src []T,
  9. first *int, last *int, reverse *bool) (*generated.StringConnection, error) {
  10. var connection = &generated.StringConnection{}
  11. if len(src) == 0 {
  12. return connection, nil
  13. }
  14. if reverse != nil && *reverse {
  15. src = fun.Reverse(src)
  16. }
  17. src = fun.First(src, first)
  18. src = fun.Last(src, last)
  19. connection.PageInfo = model.NewPageInfo(src, "", src[0], src[len(src)-1])
  20. // Edges
  21. for i := range src {
  22. connection.Edges = append(connection.Edges, &generated.StringEdge{
  23. Cursor: *model.NewSimpleCursor(src[i].GetID()).String(),
  24. Node: src[i].GetID(),
  25. })
  26. }
  27. return connection, nil
  28. }