go get -u github.com/cloudmatelabs/gorm-gqlgen-relay
Using Relay for GORM + gqlgen projects
- Must set
autobind
value in gqlgen.yml - Must set
schema
value in gqlgen.yml
- Write Edge and Connection schema
see: example/graph/schema/todo.graphql - Write Filter schema
see: example/graph/schema/todo.graphql - Import connection and edge
see: example/graph/model/todo.go - generate code
see: example/generate.go - Using paginate resolver
see: under usage or example/graph/todo.resolvers.go
see: example/graph/todo.resolvers.go
import (
"github.com/cloudmatelabs/gorm-gqlgen-relay/relay"
customContext "github.com/juunini/gorm-custom-context"
)
func (r *queryResolver) Todos(ctx context.Context, first *int, after *string, last *int, before *string, orderBy map[string]interface{}, where *model.TodoFilter) (*relay.Connection[model.Todo], error) {
context := customContext.GetContext(ctx)
db := context.Database.Preload("User")
return relay.Paginate[model.Todo](db, where, orderBy, relay.PaginateOption{
First: first,
After: after,
Last: last,
Before: before,
// Like postgres schema, mysql db, mssql schema, etc...
TablePrefix: "public",
Table: "todos",
// If you using joins table
// Tables: &map[string]string{"id": "todos", "user_id": "users"},
PrimaryKey: "id", // or "todos.id"
})
}
import "github.com/cloudmatelabs/gorm-gqlgen-relay/relay"
type TodoEdge = relay.Edge[Todo]
type TodoConnection = relay.Connection[Todo]
type Query {
todos(
first: Int
after: String
last: Int
before: String
orderBy: Map
where: TodoFilter
): TodoConnection!
}
type TodoEdge {
node: Todo!
cursor: String!
}
type TodoConnection {
totalCount: Int!
edges: [TodoEdge!]!
pageInfo: PageInfo!
}
input TodoFilter {
id: IDFilter
text: StringFilter
done: BooleanFilter
}
package main
//go:generate go run -mod=mod github.com/cloudmatelabs/gorm-gqlgen-relay
//go:generate go run -mod=mod github.com/99designs/gqlgen
Name | Type | Description |
---|---|---|
First | int | The number of items to return |
Last | int | The number of reversed items to return |
After | string | A cursor for use in pagination, which is a base-64 encoded string that points to a specific page in the dataset. |
Before | string | A cursor for use in pagination, which is a base-64 encoded string that points to a specific page in the dataset. |
TablePrefix | string | Schema(or DB) name (optional) |
Table | string | Table name (optional) |
Tables | *map[string]string | Table names (optional) |
PrimaryKey | string | Primary key name (optional) |
git init gorm-gqlgen-relay
cd gorm-gqlgen-relay
git config core.sparseCheckout true
git remote add -f origin https://github.com/cloudmatelabs/gorm-gqlgen-relay.git
echo "example" >> .git/info/sparse-checkout
git pull origin main
cd example
go run server.go