{"id":20519422,"url":"https://github.com/kimi0230/gin_graphql","last_synced_at":"2025-10-26T04:36:59.698Z","repository":{"id":44781634,"uuid":"398949295","full_name":"kimi0230/gin_graphql","owner":"kimi0230","description":"Gin and GraphQL server using Golang, gorm, gqlgen.","archived":false,"fork":false,"pushed_at":"2022-03-23T06:42:15.000Z","size":26675,"stargazers_count":6,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T16:09:12.182Z","etag":null,"topics":["cors-middleware","gin","go","golang","gorm","gqlgen","graphql","graphql-server","moby","publisher-subscriber","pubsub","swagger"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kimi0230.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-23T02:20:20.000Z","updated_at":"2024-09-18T08:30:11.000Z","dependencies_parsed_at":"2022-08-25T11:30:16.724Z","dependency_job_id":null,"html_url":"https://github.com/kimi0230/gin_graphql","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimi0230%2Fgin_graphql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimi0230%2Fgin_graphql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimi0230%2Fgin_graphql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimi0230%2Fgin_graphql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kimi0230","download_url":"https://codeload.github.com/kimi0230/gin_graphql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248809046,"owners_count":21164896,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cors-middleware","gin","go","golang","gorm","gqlgen","graphql","graphql-server","moby","publisher-subscriber","pubsub","swagger"],"created_at":"2024-11-15T22:13:29.620Z","updated_at":"2025-10-26T04:36:59.603Z","avatar_url":"https://github.com/kimi0230.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Gin GraphQL\nA GraphQL, RESTful server with Golang, [gorm][10], [gqlgen][1], [go-playground/validator][11].\nImplement PubSub by [github.com/moby/moby/pkg/pubsub](https://github.com/moby/moby/tree/master/pkg/pubsub) created by Docker.\n\n## Build Server\nCopy `.env.example` to `.env` and `.env.dev`\n\n```shell\n\n# create tables\nmake migrate\n\n# create GraphQL file\nmake build_graphql \n\n# build server\nmake build\n\n# start server\n.build/gin_graphql\n```\n### URL\n* GraphQL Playground: http://localhost:5566/graphql\n* GraphQL Query: http://localhost:5566/graphql/query\n\n![](https://github.com/kimi0230/gin_graphql/blob/master/playground.png)\n\n---\n## Notes\n### GraphQL \n#### [Schema][6]\n* https://graphql.org/learn/schema/\n\n#### [99designs/gqlgen][1]\nhttps://gqlgen.com/\nYou could initialize a new project using the recommended folder structure by running this command\n\n#### Initiation\n```shell\ngo run github.com/99designs/gqlgen init\n```\n\n```\n├── go.mod\n├── go.sum\n├── gqlgen.yml               - The gqlgen config file, knobs for controlling the generated code.\n├── graph\n│   ├── generated            - A package that only contains the generated runtime\n│   │   └── generated.go     - DO NOT EDIT !\n│   ├── model                - A package for all your graph models, generated or otherwise\n│   │   └── models_gen.go    - DO NOT EDIT !\n│   ├── resolver.go          - The root graph resolver type. This file wont get regenerated\n│   ├── schema.graphqls      - Some schema. You can split the schema into as many graphql files as you like\n│   └── schema.resolvers.go  - the resolver implementation for schema.graphql\n└── server.go                - The entry point to your app. Customize it however you see fit\n```\n\n#### Finishing touches\nAt the top of our resolver.go, between package and import, add the following line:\n``` go\n//go:generate go run github.com/99designs/gqlgen\n```\nThis magic comment tells go generate what command to run when we want to regenerate our code. \nTo run go generate recursively over your entire project, use this command:`go generate ./...`\n\n\n#### Implement the directive\n#####  Declare it in the schema\nPath: `graph/schema.graphqls`.\n```Graphql\ntype Mutation {\n    deleteMeetUp(id: ID!): Boolean! @hasRole(role: ADMIN) @isAuthenticated\n}\n\ndirective @isAuthenticated on FIELD_DEFINITION\ndirective @hasRole(role: Role!) on FIELD_DEFINITION\n```\n\n##### Implement\nPath: `graph/directives/directiveAuth.go` \n```go\nfunc IsAuthenticated(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {\n\tctxUserID := ctx.Value(CurrentUserKey)\n\tif ctxUserID == nil {\n\t\treturn nil, graph.ErrUnauthenticated\n\t}\n\treturn next(ctx)\n}\n```\n\n##### Pass it in when start server\nPath: `main.go`\n```go\nc := generated.Config{Resolvers: \u0026graph.Resolver{}}\n// Schema Directive\nc.Directives.IsAuthenticated = directives.IsAuthenticated\nc.Directives.HasRole = directives.HasRole\n\nsrv := handler.NewDefaultServer(generated.NewExecutableSchema(c))\n\nreturn func(c *gin.Context) {\n    srv.ServeHTTP(c.Writer, c.Request)\n}\n```\n\n\n\n\n#### Modify schema\n1. modify your schema `graph/schema.graphqls`\n2. run gqlgen `./script/gqlgen.sh` or `go run -v github.com/99designs/gqlgen`\n3. modify resolvers `graph/resolver.go`\n\n---\n\n### Database (Mysql)\n#### Migrate\n`make migrate mode={auto | drop | refresh}` or `go run  database/migrate.go -m=auto`\n\n---\n### Swagger API Doc\nhttp://localhost:5566/swagger/index.html\n\n##### init\n`swag init`\n\n### PPROF\n```shell\n[GIN-debug] GET    /admin/pprof/             --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] GET    /admin/pprof/cmdline      --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] GET    /admin/pprof/profile      --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] POST   /admin/pprof/symbol       --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] GET    /admin/pprof/symbol       --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] GET    /admin/pprof/trace        --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] GET    /admin/pprof/allocs       --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] GET    /admin/pprof/block        --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] GET    /admin/pprof/goroutine    --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] GET    /admin/pprof/heap         --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] GET    /admin/pprof/mutex        --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n[GIN-debug] GET    /admin/pprof/threadcreate --\u003e github.com/gin-contrib/pprof.pprofHandler.func1 (4 handlers)\n```\n\n---\n### Go commands\n```shell\n# Initiation Go project\ngo mod init  \ngo mod tidy\n\n# download Go package\ngo mod download\n```\n---\n### Fix\n1. `graph/prelude.resolvers.go:19:34: cannot refer to unexported name generated.__DirectiveResolver`\n    * rollback the version of gqlparser from github.com/vektah/gqlparser/v2 v2.2.0 to github.com/vektah/gqlparser/v2 v2.1.0\n    ```\n    go mod edit -require github.com/vektah/gqlparser/v2@v2.1.0    \n    go clean -i github.com/vektah/gqlparser/v2  \n    go get github.com/vektah/gqlparser/v2@v2.1.0\n    ```\n    * https://github.com/99designs/gqlgen/issues/1402\n    * https://stackoverflow.com/questions/24855081/how-do-i-import-a-specific-version-of-a-package-using-go-get\n    * https://github.com/golang/go/issues/44129\n\n---\n\n## Reference\n1. [99designs/gqlgen][1]\n2. [go.uber.org/ratelimit][2]\n3. [didip/tollbooth][3]\n4. [EQuimper/youtube-golang-graphql-tutorial][4]\n5. [wtlin1228/unasees][5]\n6. [schema][6]\n7. [gqlgen gin][7]\n8. [blog.laisky.com][8]\n9. [qlgen-custom-data-validation][9]\n10. [gorm][10]\n11. [go-playground/validator][11]\n12. [graphql-spec][12]\n\n[1]: https://github.com/99designs/gqlgen \n\"99designs/gqlgen\"\n[2]: https://pkg.go.dev/go.uber.org/ratelimit\n\"go.uber.org/ratelimit\"\n[3]: https://github.com/didip/tollbooth\n\"didip/tollbooth\"\n[4]: https://github.com/EQuimper/youtube-golang-graphql-tutorial\n\"EQuimper/youtube-golang-graphql-tutorial\"\n[5]: https://github.com/wtlin1228/unasees\n\"wtlin1228/unasees\"\n[6]: https://graphql.org/learn/schema/\n\"schema\"\n[7]: https://gqlgen.com/recipes/gin/\n\"gqlgen gin\"\n[8]: https://blog.laisky.com/p/gqlgen/#%E5%AE%9A%E4%B9%89+schema-Hfxfd\n\"blog.laisky.com\"\n[9]: https://david-yappeter.medium.com/gqlgen-custom-data-validation-part-1-7de8ef92de4c\n\"qlgen-custom-data-validation\"\n[10]: https://gorm.io/index.html\n\"gorm\"\n[11]: https://github.com/go-playground/validator\n\"go-playground/validator\"\n[12]: https://github.com/graphql/graphql-spec/blob/main/spec/Section%203%20--%20Type%20System.md#descriptions\n\"graphql-spec\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkimi0230%2Fgin_graphql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkimi0230%2Fgin_graphql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkimi0230%2Fgin_graphql/lists"}