GithubHelp home page GithubHelp logo

kimi0230 / gin_graphql Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 2.0 26.05 MB

Gin and GraphQL server using Golang, gorm, gqlgen.

License: Apache License 2.0

Shell 1.26% Go 97.36% Makefile 0.54% HTML 0.84%
graphql golang gqlgen graphql-server gin swagger pubsub publisher-subscriber moby gorm go cors-middleware

gin_graphql's Introduction

Gin GraphQL

A GraphQL, RESTful server with Golang, gorm, gqlgen, go-playground/validator. Implement PubSub by github.com/moby/moby/pkg/pubsub created by Docker.

Build Server

Copy .env.example to .env and .env.dev

# create tables
make migrate

# create GraphQL file
make build_graphql 

# build server
make build

# start server
.build/gin_graphql

URL


Notes

GraphQL

https://gqlgen.com/ You could initialize a new project using the recommended folder structure by running this command

Initiation

go run github.com/99designs/gqlgen init
├── go.mod
├── go.sum
├── gqlgen.yml               - The gqlgen config file, knobs for controlling the generated code.
├── graph
│   ├── generated            - A package that only contains the generated runtime
│   │   └── generated.go     - DO NOT EDIT !
│   ├── model                - A package for all your graph models, generated or otherwise
│   │   └── models_gen.go    - DO NOT EDIT !
│   ├── resolver.go          - The root graph resolver type. This file wont get regenerated
│   ├── schema.graphqls      - Some schema. You can split the schema into as many graphql files as you like
│   └── schema.resolvers.go  - the resolver implementation for schema.graphql
└── server.go                - The entry point to your app. Customize it however you see fit

Finishing touches

At the top of our resolver.go, between package and import, add the following line:

//go:generate go run github.com/99designs/gqlgen

This magic comment tells go generate what command to run when we want to regenerate our code. To run go generate recursively over your entire project, use this command:go generate ./...

Implement the directive

Declare it in the schema

Path: graph/schema.graphqls.

type Mutation {
    deleteMeetUp(id: ID!): Boolean! @hasRole(role: ADMIN) @isAuthenticated
}

directive @isAuthenticated on FIELD_DEFINITION
directive @hasRole(role: Role!) on FIELD_DEFINITION
Implement

Path: graph/directives/directiveAuth.go

func IsAuthenticated(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
	ctxUserID := ctx.Value(CurrentUserKey)
	if ctxUserID == nil {
		return nil, graph.ErrUnauthenticated
	}
	return next(ctx)
}
Pass it in when start server

Path: main.go

c := generated.Config{Resolvers: &graph.Resolver{}}
// Schema Directive
c.Directives.IsAuthenticated = directives.IsAuthenticated
c.Directives.HasRole = directives.HasRole

srv := handler.NewDefaultServer(generated.NewExecutableSchema(c))

return func(c *gin.Context) {
    srv.ServeHTTP(c.Writer, c.Request)
}

Modify schema

  1. modify your schema graph/schema.graphqls
  2. run gqlgen ./script/gqlgen.sh or go run -v github.com/99designs/gqlgen
  3. modify resolvers graph/resolver.go

Database (Mysql)

Migrate

make migrate mode={auto | drop | refresh} or go run database/migrate.go -m=auto


Swagger API Doc

http://localhost:5566/swagger/index.html

init

swag init

PPROF

[GIN-debug] GET    /admin/pprof/             --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/pprof/cmdline      --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/pprof/profile      --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] POST   /admin/pprof/symbol       --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/pprof/symbol       --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/pprof/trace        --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/pprof/allocs       --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/pprof/block        --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/pprof/goroutine    --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/pprof/heap         --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/pprof/mutex        --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/pprof/threadcreate --> github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)

Go commands

# Initiation Go project
go mod init  
go mod tidy

# download Go package
go mod download

Fix

  1. graph/prelude.resolvers.go:19:34: cannot refer to unexported name generated.__DirectiveResolver
    • rollback the version of gqlparser from github.com/vektah/gqlparser/v2 v2.2.0 to github.com/vektah/gqlparser/v2 v2.1.0
    go mod edit -require github.com/vektah/gqlparser/[email protected]    
    go clean -i github.com/vektah/gqlparser/v2  
    go get github.com/vektah/gqlparser/[email protected]
    

Reference

  1. 99designs/gqlgen
  2. go.uber.org/ratelimit
  3. didip/tollbooth
  4. EQuimper/youtube-golang-graphql-tutorial
  5. wtlin1228/unasees
  6. schema
  7. gqlgen gin
  8. blog.laisky.com
  9. qlgen-custom-data-validation
  10. gorm
  11. go-playground/validator
  12. graphql-spec

gin_graphql's People

Contributors

kimi0230 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

djun tuanloc1105

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.