https://github.com/dewski/graphql-cursor
Add GraphQL Relay Cursor Pagination with Postgres.
https://github.com/dewski/graphql-cursor
Last synced: 6 months ago
JSON representation
Add GraphQL Relay Cursor Pagination with Postgres.
- Host: GitHub
- URL: https://github.com/dewski/graphql-cursor
- Owner: dewski
- Created: 2016-12-12T21:08:06.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-12-13T23:10:29.000Z (over 9 years ago)
- Last Synced: 2025-03-17T07:45:03.393Z (about 1 year ago)
- Language: Go
- Homepage: https://godoc.org/github.com/dewski/graphql-cursor
- Size: 8.79 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# grapqhl-cursor
 [](http://godoc.org/github.com/dewski/graphql-cursor)
Add GraphQL Relay Cursor Pagination with Postgres.
```go
package web
import (
"errors"
"net/http"
"strconv"
"github.com/dewski/graphql-cursor"
"github.com/graphql-go/graphql"
"github.com/graphql-go/relay"
"github.com/labstack/echo"
"golang.org/x/net/context"
)
var nodeDefinitions *relay.NodeDefinitions
// Each top level type
var tripType *graphql.Object
func (t *Trip) GetEvents(scope cursor.Scope) ([]*TripEvent, error) {
tripEvents := []*TripEvent{}
builder := database.Conn().
Select("*").
From("trip_events").
Where("trip_id = $1", t.ID)
query, err := cursor.ApplyScope(builder, scope)
if err != nil {
return nil, err
}
err = query.QueryStructs(&tripEvents)
if err != nil {
return nil, err
}
return tripEvents, nil
}
func main() {
tripType = graphql.NewObject(graphql.ObjectConfig{
Name: "Trip",
Description: "A trip contains the details and event points for a trip",
Fields: graphql.Fields{
"id": relay.GlobalIDField("Trip", nil),
"events": &graphql.Field{
Args: relay.ConnectionArgs,
Description: "The events that make up the trip",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
scope := cursor.NewScopeWithFilters(p.Args)
t := p.Source.(*vehicle.Trip)
data := []cursor.Cursor{}
events, err := t.GetEvents(scope)
if err != nil {
return nil, err
}
for _, event := range events {
data = append(data, event)
}
return cursor.Connection(data, scope), nil
},
},
},
Interfaces: []*graphql.Interface{
nodeDefinitions.NodeInterface,
},
})
schema := graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})
}
```