Giter Club home page Giter Club logo

entgo-gqlgen-fiber-sample's Introduction

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 ./...

entgo-gqlgen-fiber-sample's People

Contributors

juunini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.