Skip to content

Commit 4ed8b81

Browse files
committed
upgrade
1 parent 106d5b6 commit 4ed8b81

File tree

32 files changed

+3256
-82
lines changed

32 files changed

+3256
-82
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@
3535
```cassandraql
3636
├── main.go # 项目入口文件
3737
├── configs # 配置文件统一存放目录
38-
├── docs # Swagger 文档,执行 swag init 生成的
39-
├── init # 项目初始脚本,比如初始化 SQL 语句
38+
├── docs # Swagger 文档
39+
├── init # 项目初始配置,比如初始化 SQL 语句
4040
├── internal # 业务目录
4141
│ ├── api # 业务代码
42+
│ ├── graph # GraphQL 代码
4243
│ ├── pkg # 内部使用的 package
43-
├── logs # 存放日志的目录
44-
└── pkg # 一些封装好的 package
44+
├── logs # 日志目录
45+
├── pkg # 外部使用的 package
46+
└── scripts # 操作脚本
4547
```
4648
## Features
4749

configs/configs.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ func init() {
6868
viper.SetConfigName(env.Active().Value() + "_configs")
6969
viper.SetConfigType("toml")
7070
viper.AddConfigPath("./configs")
71-
viper.AddConfigPath("../../configs") // 兼容 cmd/cron/main.go 引用配置文件
7271

7372
if err := viper.ReadInConfig(); err != nil {
7473
panic(err)

docs/docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ type swaggerInfo struct {
351351

352352
// SwaggerInfo holds exported Swagger Info so clients can modify it
353353
var SwaggerInfo = swaggerInfo{
354-
Version: "",
354+
Version: "2.0",
355355
Host: "127.0.0.1:9999",
356356
BasePath: "",
357357
Schemes: []string{},
358-
Title: "go-gin-api docs api",
358+
Title: "swagger 接口文档",
359359
Description: "",
360360
}
361361

docs/swagger.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"swagger": "2.0",
33
"info": {
4-
"title": "go-gin-api docs api",
4+
"title": "swagger 接口文档",
55
"contact": {},
66
"license": {
77
"name": "MIT",
88
"url": "https://github.com/xinliangnote/go-gin-api/blob/master/LICENSE"
9-
}
9+
},
10+
"version": "2.0"
1011
},
1112
"host": "127.0.0.1:9999",
1213
"paths": {

docs/swagger.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ info:
6262
license:
6363
name: MIT
6464
url: https://github.com/xinliangnote/go-gin-api/blob/master/LICENSE
65-
title: go-gin-api docs api
65+
title: swagger 接口文档
66+
version: "2.0"
6667
paths:
6768
/auth/get:
6869
post:

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/xinliangnote/go-gin-api
33
go 1.15
44

55
require (
6+
github.com/99designs/gqlgen v0.13.0
67
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
78
github.com/dgrijalva/jwt-go v3.2.0+incompatible
89
github.com/gin-contrib/pprof v1.2.1
@@ -19,6 +20,7 @@ require (
1920
github.com/spf13/viper v1.7.1
2021
github.com/swaggo/gin-swagger v1.3.0
2122
github.com/swaggo/swag v1.7.0
23+
github.com/vektah/gqlparser/v2 v2.1.0
2224
go.uber.org/multierr v1.5.0
2325
go.uber.org/zap v1.16.0
2426
golang.org/x/mod v0.4.0 // indirect

go.sum

Lines changed: 40 additions & 0 deletions
Large diffs are not rendered by default.

gqlgen.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Where are all the schema files located? globs are supported eg src/**/*.graphqls
2+
schema:
3+
- internal/graph/schemas/*.graphql
4+
5+
# Where should the generated server code go?
6+
exec:
7+
filename: internal/graph/generated/generated.go
8+
package: generated
9+
10+
# Uncomment to enable federation
11+
# federation:
12+
# filename: graph/generated/federation.go
13+
# package: generated
14+
15+
# Where should any generated models go?
16+
model:
17+
filename: internal/graph/model/generated.go
18+
package: model
19+
20+
# Where should the resolver implementations go?
21+
resolver:
22+
filename: internal/graph/resolvers/generated/generated.go
23+
type: Resolver
24+
package: resolvers
25+
26+
# Optional: turn on use `gqlgen:"fieldName"` tags in your models
27+
# struct_tag: json
28+
29+
# Optional: turn on to use []Thing instead of []*Thing
30+
# omit_slice_element_pointers: false
31+
32+
# Optional: set to speed up generation time by not performing a final validation pass.
33+
# skip_validation: true
34+
35+
# gqlgen will search for any type names in the schema in these go packages
36+
# if they match it will use them, otherwise it will generate them.
37+
autobind:
38+
- "github.com/xinliangnote/go-gin-api/internal/graph/model"
39+
40+
# This section declares type mapping between the GraphQL and go type systems
41+
#
42+
# The first line in each type will be used as defaults for resolver arguments and
43+
# modelgen, the others will be allowed when binding to fields. Configure them to
44+
# your liking
45+
models:
46+
ID:
47+
model:
48+
- github.com/99designs/gqlgen/graphql.ID
49+
- github.com/99designs/gqlgen/graphql.Int
50+
- github.com/99designs/gqlgen/graphql.Int64
51+
- github.com/99designs/gqlgen/graphql.Int32
52+
Int:
53+
model:
54+
- github.com/99designs/gqlgen/graphql.Int
55+
- github.com/99designs/gqlgen/graphql.Int64
56+
- github.com/99designs/gqlgen/graphql.Int32

internal/api/controller/demo/demo.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ func (d *Demo) Trace() core.HandlerFunc {
171171
}
172172

173173
// 调试信息
174-
p.Println("res2.Data.Name", res2.Data.Name, p.WithTrace(c.Trace()))
174+
p.Println("res2.Data.Name",
175+
res2.Data.Name,
176+
p.WithTrace(c.Trace()),
177+
)
175178

176179
// 执行 SQL 信息
177180
d.userService.GetUserByUserName(c, "test_user")

internal/api/repository/db_repo/user_demo_repo/user_demo.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/xinliangnote/go-gin-api/internal/pkg/db"
77

88
"github.com/pkg/errors"
9+
"gorm.io/gorm"
910
)
1011

1112
var _ UserRepo = (*userRepo)(nil)
@@ -42,8 +43,9 @@ func (u *userRepo) Create(ctx core.Context, user user_model.UserDemo) (id uint,
4243

4344
func (u *userRepo) getUserByID(ctx core.Context, id uint) (*user_model.UserDemo, error) {
4445
data := new(user_model.UserDemo)
45-
err := u.db.GetDbR().WithContext(ctx.RequestContext()).First(data, id).Where("is_deleted = ?", -1).Error
46-
if err != nil {
46+
err := u.db.GetDbR().
47+
WithContext(ctx.RequestContext()).First(data, id).Where("is_deleted = ?", -1).Error
48+
if err != nil && err != gorm.ErrRecordNotFound {
4749
return nil, errors.Wrap(err, "[user_demo] get user data err")
4850
}
4951
return data, nil
@@ -52,15 +54,15 @@ func (u *userRepo) getUserByID(ctx core.Context, id uint) (*user_model.UserDemo,
5254
func (u *userRepo) UpdateNickNameByID(ctx core.Context, id uint, nickname string) (err error) {
5355
user, err := u.getUserByID(ctx, id)
5456
if err != nil {
55-
return errors.Wrap(err, "[user_demo] update user data err")
57+
return errors.Wrap(err, "[user_demo] get user data err")
5658
}
5759
return u.db.GetDbW().WithContext(ctx.RequestContext()).Model(user).Update("nick_name", nickname).Error
5860
}
5961

6062
func (u *userRepo) Delete(ctx core.Context, id uint) (err error) {
6163
user, err := u.getUserByID(ctx, id)
6264
if err != nil {
63-
return errors.Wrap(err, "[user_demo] update user data err")
65+
return errors.Wrap(err, "[user_demo] get user data err")
6466
}
6567
return u.db.GetDbW().WithContext(ctx.RequestContext()).Model(user).Update("is_deleted", 1).Error
6668
}
@@ -72,7 +74,7 @@ func (u *userRepo) GetUserByUserName(ctx core.Context, username string) (*user_m
7274
Select([]string{"id", "user_name", "nick_name", "mobile"}).
7375
Where("user_name = ? and is_deleted = ?", username, -1).
7476
First(data).Error
75-
if err != nil {
77+
if err != nil && err != gorm.ErrRecordNotFound {
7678
return nil, errors.Wrap(err, "[user_demo] get user data err")
7779
}
7880
return data, nil

internal/api/router/router.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/xinliangnote/go-gin-api/internal/api/controller/demo"
55
"github.com/xinliangnote/go-gin-api/internal/api/controller/user_handler"
66
"github.com/xinliangnote/go-gin-api/internal/api/router/middleware/auth"
7+
"github.com/xinliangnote/go-gin-api/internal/graph/handler"
78
"github.com/xinliangnote/go-gin-api/internal/pkg/cache"
89
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
910
"github.com/xinliangnote/go-gin-api/internal/pkg/db"
@@ -33,6 +34,13 @@ func NewHTTPMux(logger *zap.Logger, db db.Repo, cache cache.Repo) (core.Mux, err
3334

3435
demoHandler := demo.NewDemo(logger, db, cache)
3536
userHandler := user_handler.NewUserDemo(logger, db, cache)
37+
gqlHandler := handler.New(logger, db, cache)
38+
39+
gql := mux.Group("/graphql")
40+
{
41+
gql.GET("", gqlHandler.Playground())
42+
gql.POST("/query", gqlHandler.Query())
43+
}
3644

3745
// user_demo CURD
3846
user := mux.Group("/user", core.WrapAuthHandler(auth.AuthHandler))

internal/graph/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## example
2+
3+
```cassandraql
4+
1.
5+
query {
6+
bySex(sex: "男") {
7+
id
8+
name
9+
sex
10+
mobile
11+
}
12+
}
13+
14+
2.
15+
mutation {
16+
updateUserMobile(data: {id: "1", mobile: "13299999999"}) {
17+
id
18+
name
19+
sex
20+
mobile
21+
}
22+
}
23+
24+
```

0 commit comments

Comments
 (0)