Skip to content

ahnlabcloudmatelabs/gorm-gqlgen-relay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cloudmate logo

GORM gqlgen relay

by Cloudmate


Cloudmate Golang


Install

go get -u github.com/cloudmatelabs/gorm-gqlgen-relay

About

Using Relay for GORM + gqlgen projects

Prepare

  1. Must set autobind value in gqlgen.yml
  2. Must set schema value in gqlgen.yml

Steps

  1. Write Edge and Connection schema
    see: example/graph/schema/todo.graphql
  2. Write Filter schema
    see: example/graph/schema/todo.graphql
  3. Import connection and edge
    see: example/graph/model/todo.go
  4. generate code
    see: example/generate.go
  5. Using paginate resolver
    see: under usage or example/graph/todo.resolvers.go

Usage

resolver

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"
	})
}

model

import "github.com/cloudmatelabs/gorm-gqlgen-relay/relay"

type TodoEdge = relay.Edge[Todo]
type TodoConnection = relay.Connection[Todo]

schema(graphql)

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
}

generate.go

package main

//go:generate go run -mod=mod github.com/cloudmatelabs/gorm-gqlgen-relay
//go:generate go run -mod=mod github.com/99designs/gqlgen

Paginate Option

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)

Execute example

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