Skip to content

juunini/entgo-gqlgen-fiber-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

entgo, gqlgen, fiber sample

How to start this server?

$ go mod tidy
$ go generate ./...
$ go run main.go

How to make it?

  1. Insert under commands
$ go get -d entgo.io/ent/cmd/ent
$ go run entgo.io/ent/cmd/ent init Todo
$ go get -u entgo.io/contrib/entgql
  1. Add ent/schema/todo.go Field and Annotation

  2. Add ent/schema/todo.graphql and ent/gqlgen.yml

  3. Add ent/entc.go

  4. Fix ent/generate.go content

  5. Do generate

$ go generate ./...
  1. Fill resolvers/* contents

  2. Install fiber

$ go get -u github.com/gofiber/fiber/v2
$ go get -u github.com/gofiber/adaptor/v2
$ go get -u github.com/99designs/gqlgen/graphql/playground
$ go get -u github.com/mattn/go-sqlite3
  1. Add main.go and Start server
import (
  "context"
  "entgogqlgenfibersample/ent"
  "entgogqlgenfibersample/ent/migrate"
  "entgogqlgenfibersample/resolvers"

  "entgo.io/contrib/entgql"
  "entgo.io/ent/dialect"
  "github.com/99designs/gqlgen/graphql/handler"
  "github.com/99designs/gqlgen/graphql/playground"
  "github.com/gofiber/adaptor/v2"
  "github.com/gofiber/fiber/v2"

  _ "github.com/mattn/go-sqlite3"
)

func main() {
  client, err := ent.Open(dialect.SQLite, "file:ent?mode=memory&cache=shared&_fk=1")
  if err != nil {
    panic(err)
  }
  
  if err := client.Schema.Create(
    context.Background(),
    migrate.WithGlobalUniqueID(true),
  ); err != nil {
    panic(err)
  }

  srv := handler.NewDefaultServer(resolvers.NewSchema(client))
  srv.Use(entgql.Transactioner{TxOpener: client})
  
  app := fiber.New()
  app.Get("/playground", adaptor.HTTPHandlerFunc(playground.Handler("Graphql Playground", "/query")))
  app.All("/query", adaptor.HTTPHandler(srv))
  app.Listen(":3000")
}
$ go run main.go
  1. Enter http://localhost:3000/playground

If you want embedded postgresql

  1. Add dependencies
$ go get -u github.com/fergusstrange/embedded-postgres
$ go get -u github.com/jackc/pgx/v4/stdlib
  1. Write main.go
package main

import (
	"context"
	"database/sql"
	"entgogqlgenfibersample/ent"
	"entgogqlgenfibersample/ent/migrate"
	"entgogqlgenfibersample/resolvers"
	"log"
	"os"
	"os/signal"
	"syscall"

	"entgo.io/contrib/entgql"
	"entgo.io/ent/dialect"
	entsql "entgo.io/ent/dialect/sql"
	"github.com/99designs/gqlgen/graphql/handler"
	"github.com/99designs/gqlgen/graphql/playground"
	"github.com/gofiber/adaptor/v2"
	"github.com/gofiber/fiber/v2"

	_ "github.com/jackc/pgx/v4/stdlib"

	embeddedpostgres "github.com/fergusstrange/embedded-postgres"
)

func main() {
	postgres := embeddedpostgres.NewDatabase(embeddedpostgres.DefaultConfig().Database("postgres"))
	if err := postgres.Start(); err != nil {
		panic(err)
	}

	client := openPsql("postgresql://postgres:[email protected]/postgres")
	if err := client.Schema.Create(
		context.Background(),
		migrate.WithGlobalUniqueID(true),
	); err != nil {
		panic(err)
	}

	go interruptEmbeddedPostgres(client, postgres)

	srv := handler.NewDefaultServer(resolvers.NewSchema(client))
	srv.Use(entgql.Transactioner{TxOpener: client})

	app := fiber.New()

	app.Get("/playground", adaptor.HTTPHandlerFunc(playground.Handler("Graphql Playground", "/query")))
	app.All("/query", adaptor.HTTPHandler(srv))

	app.Listen(":3000")
}

func openPsql(databaseUrl string) *ent.Client {
	db, err := sql.Open("pgx", databaseUrl)
	if err != nil {
		log.Fatal(err)
	}

	drv := entsql.OpenDB(dialect.Postgres, db)
	return ent.NewClient(ent.Driver(drv))
}

func interruptEmbeddedPostgres(client *ent.Client, postgres *embeddedpostgres.EmbeddedPostgres) {
	sig := make(chan os.Signal, 1)
	signal.Notify(
		sig,
		syscall.SIGKILL,
		syscall.SIGTERM,
		syscall.SIGINT,
		os.Interrupt,
	)

	<-sig

	client.Close()
	postgres.Stop()
	os.Exit(0)
}

If you want uuid to id

CAUTION: If you using uuid, you can't query node relay

  1. ent/gqlgen.yml add models.ID in - <projectName>/ent/models.UUID

  2. Add ent/models/uuid.go

  3. Change field in id property

    field.UUID("id", uuid.UUID{}).
    		Default(uuid.New).
    		SchemaType(
    			map[string]string{
    				dialect.Postgres: "uuid",
    			},
    		)
  4. go generate ./...