From 4919ea17eb7b0e049918ff4a952b8824826c58b1 Mon Sep 17 00:00:00 2001 From: linty Date: Fri, 1 Nov 2024 22:46:36 +0800 Subject: [PATCH 1/2] fix: fix id input --- example/user/models/model.go | 2 +- example/user/repo/repo.go | 4 +- example/user/resolver/operation_gen.go | 49 ++++++++++++++++-- example/user/resolver/query.go | 52 ++++++++++++-------- example/user/schema/post.graphqls | 2 + graphql/model/generate/tpl/operation_gen.tpl | 6 ++- template/import.go | 3 ++ 7 files changed, 89 insertions(+), 29 deletions(-) diff --git a/example/user/models/model.go b/example/user/models/model.go index 3bd8097..fc40aba 100644 --- a/example/user/models/model.go +++ b/example/user/models/model.go @@ -6,7 +6,7 @@ import "github.com/light-speak/lighthouse/graphql/model" type User struct { model.Model - Name string `gorm:"index;type:varchar(255)" json:"name" ` + Name string `json:"name" gorm:"index;type:varchar(255)" ` Posts []Post `json:"posts" ` } diff --git a/example/user/repo/repo.go b/example/user/repo/repo.go index efd0047..cfd216e 100644 --- a/example/user/repo/repo.go +++ b/example/user/repo/repo.go @@ -2,11 +2,11 @@ package repo import ( - "user/models" "github.com/light-speak/lighthouse/graphql/model" "sync" - "github.com/light-speak/lighthouse/graphql/ast" "github.com/light-speak/lighthouse/context" + "user/models" + "github.com/light-speak/lighthouse/graphql/ast" "gorm.io/gorm" ) diff --git a/example/user/resolver/operation_gen.go b/example/user/resolver/operation_gen.go index 5e2abad..9bb0ddf 100644 --- a/example/user/resolver/operation_gen.go +++ b/example/user/resolver/operation_gen.go @@ -2,16 +2,21 @@ package resolver import ( - "github.com/light-speak/lighthouse/context" - "fmt" "github.com/light-speak/lighthouse/graphql/excute" - "user/models" + "github.com/light-speak/lighthouse/graphql" "github.com/light-speak/lighthouse/graphql/model" + "github.com/light-speak/lighthouse/context" + "user/models" + "fmt" ) func init() { excute.AddResolver("getPost", func(ctx *context.Context, args map[string]any) (interface{}, error) { - fuck, ok := args["fuck"].(string) + pv, e := graphql.Parser.NodeStore.Scalars["String"].ScalarType.ParseValue(args["fuck"], nil) + if e != nil { + return nil, e + } + fuck, ok := pv.(string) if !ok { return nil, fmt.Errorf("argument: 'fuck' is not a string, got %T", args["fuck"]) } @@ -29,7 +34,11 @@ func init() { return res, nil }) excute.AddResolver("getPosts", func(ctx *context.Context, args map[string]any) (interface{}, error) { - fuck, ok := args["fuck"].(string) + pv, e := graphql.Parser.NodeStore.Scalars["String"].ScalarType.ParseValue(args["fuck"], nil) + if e != nil { + return nil, e + } + fuck, ok := pv.(string) if !ok { return nil, fmt.Errorf("argument: 'fuck' is not a string, got %T", args["fuck"]) } @@ -56,6 +65,21 @@ func init() { res, err := TestPostEnumResolver(ctx, enum) return res, err }) + excute.AddResolver("testPostId", func(ctx *context.Context, args map[string]any) (interface{}, error) { + pv, e := graphql.Parser.NodeStore.Scalars["ID"].ScalarType.ParseValue(args["id"], nil) + if e != nil { + return nil, e + } + id, ok := pv.(int64) + if !ok { + return nil, fmt.Errorf("argument: 'id' is not a int64, got %T", args["id"]) + } + res, err := TestPostIdResolver(ctx, id) + if res == nil { + return nil, err + } + return model.StructToMap(res) + }) excute.AddResolver("testPostInput", func(ctx *context.Context, args map[string]any) (interface{}, error) { input, err := models.MapToTestInput(args["input"].(map[string]interface{})) if err != nil { @@ -64,4 +88,19 @@ func init() { res, err := TestPostInputResolver(ctx, input) return res, err }) + excute.AddResolver("testPostInt", func(ctx *context.Context, args map[string]any) (interface{}, error) { + pv, e := graphql.Parser.NodeStore.Scalars["Boolean"].ScalarType.ParseValue(args["id"], nil) + if e != nil { + return nil, e + } + id, ok := pv.(bool) + if !ok { + return nil, fmt.Errorf("argument: 'id' is not a bool, got %T", args["id"]) + } + res, err := TestPostIntResolver(ctx, id) + if res == nil { + return nil, err + } + return model.StructToMap(res) + }) } diff --git a/example/user/resolver/query.go b/example/user/resolver/query.go index 7a9c64f..c7231b2 100644 --- a/example/user/resolver/query.go +++ b/example/user/resolver/query.go @@ -2,32 +2,14 @@ package resolver import ( - "user/models" - "github.com/light-speak/lighthouse/graphql/model" "github.com/light-speak/lighthouse/log" "github.com/light-speak/lighthouse/context" + "user/models" "fmt" + "github.com/light-speak/lighthouse/graphql/model" ) -func GetPostIdsResolver(ctx *context.Context) ([]int64, error) { - // Func:GetPostIds user code start. Do not remove this comment. - return []int64{1, 2, 3}, nil - // Func:GetPostIds user code end. Do not remove this comment. -} -func TestPostEnumResolver(ctx *context.Context,enum *models.TestEnum) (string, error) { - // Func:TestPostEnum user code start. Do not remove this comment. - log.Debug().Msgf("enum: %+v", enum) - res := fmt.Sprintf("啥也不是!:%v", *enum == models.A) - return res, nil - // Func:TestPostEnum user code end. Do not remove this comment. -} -func TestPostInputResolver(ctx *context.Context,input *models.TestInput) (string, error) { - // Func:TestPostInput user code start. Do not remove this comment. - res := fmt.Sprintf("input: %+v", input) - return res, nil - // Func:TestPostInput user code end. Do not remove this comment. -} func GetPostResolver(ctx *context.Context,fuck string) (*models.Post, error) { // Func:GetPost user code start. Do not remove this comment. log.Debug().Msg("GetPostResolver") @@ -37,6 +19,18 @@ func GetPostResolver(ctx *context.Context,fuck string) (*models.Post, error) { return post, nil // Func:GetPost user code end. Do not remove this comment. } +func TestPostIdResolver(ctx *context.Context,id int64) (*models.Post, error) { + // Func:TestPostId user code start. Do not remove this comment. + log.Debug().Msgf("id: %d", id) + return nil, nil + // Func:TestPostId user code end. Do not remove this comment. +} +func TestPostIntResolver(ctx *context.Context,id bool) (*models.Post, error) { + // Func:TestPostInt user code start. Do not remove this comment. + log.Debug().Msgf("id: %d", id) + return nil, nil + // Func:TestPostInt user code end. Do not remove this comment. +} func GetPostsResolver(ctx *context.Context,fuck string) ([]*models.Post, error) { // Func:GetPosts user code start. Do not remove this comment. posts := []*models.Post{} @@ -44,4 +38,22 @@ func GetPostsResolver(ctx *context.Context,fuck string) ([]*models.Post, error) db.Find(&posts) return posts, nil // Func:GetPosts user code end. Do not remove this comment. +} +func TestPostInputResolver(ctx *context.Context,input *models.TestInput) (string, error) { + // Func:TestPostInput user code start. Do not remove this comment. + res := fmt.Sprintf("input: %+v", input) + return res, nil + // Func:TestPostInput user code end. Do not remove this comment. +} +func GetPostIdsResolver(ctx *context.Context) ([]int64, error) { + // Func:GetPostIds user code start. Do not remove this comment. + return []int64{1, 2, 3}, nil + // Func:GetPostIds user code end. Do not remove this comment. +} +func TestPostEnumResolver(ctx *context.Context,enum *models.TestEnum) (string, error) { + // Func:TestPostEnum user code start. Do not remove this comment. + log.Debug().Msgf("enum: %+v", enum) + res := fmt.Sprintf("啥也不是!:%v", *enum == models.A) + return res, nil + // Func:TestPostEnum user code end. Do not remove this comment. } \ No newline at end of file diff --git a/example/user/schema/post.graphqls b/example/user/schema/post.graphqls index f4cb345..659896a 100644 --- a/example/user/schema/post.graphqls +++ b/example/user/schema/post.graphqls @@ -13,6 +13,8 @@ extend type Query { getPost(fuck: String!): Post! getPosts(fuck: String!): [Post!]! getPostIds: [ID!]! + testPostId(id: ID!): Post + testPostInt(id: Boolean!): Post testPostEnum(enum: TestEnum!): String! testPostInput(input: TestInput!): String! } diff --git a/graphql/model/generate/tpl/operation_gen.tpl b/graphql/model/generate/tpl/operation_gen.tpl index 1a46504..35f5f8b 100644 --- a/graphql/model/generate/tpl/operation_gen.tpl +++ b/graphql/model/generate/tpl/operation_gen.tpl @@ -9,7 +9,11 @@ func init() { {{- range $index, $arg := $args }} {{- if eq $arg.Type.GetRealType.Kind "SCALAR" }} - {{ $arg.Name | lcFirst }}, ok := args["{{ $index }}"].({{ false | $arg.Type.GetGoType }}) + pv, e := graphql.Parser.NodeStore.Scalars["{{ $arg.Type.GetRealType.Name }}"].ScalarType.ParseValue(args["{{ $index }}"], nil) + if e != nil { + return nil, e + } + {{ $arg.Name | lcFirst }}, ok := pv.({{ false | $arg.Type.GetGoType }}) if !ok { return nil, fmt.Errorf("argument: '{{ $arg.Name }}' is not a {{ false | $arg.Type.GetGoType }}, got %T", args["{{ $index }}"]) } diff --git a/template/import.go b/template/import.go index 6e08178..877612a 100644 --- a/template/import.go +++ b/template/import.go @@ -75,6 +75,9 @@ var importRegexMap = map[string]Import{ `handler\.`: { Path: "github.com/light-speak/lighthouse/handler", }, + `graphql\.`: { + Path: "github.com/light-speak/lighthouse/graphql", + }, } // AddImportRegex add a new import regex and path to the importRegexMap From 4f5928d1701ea88f2891d1ca5b9e84a252490e62 Mon Sep 17 00:00:00 2001 From: linty Date: Fri, 1 Nov 2024 22:46:55 +0800 Subject: [PATCH 2/2] feat: change version --- version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version/version.go b/version/version.go index 6568e8c..566d4f7 100644 --- a/version/version.go +++ b/version/version.go @@ -1,3 +1,3 @@ package version -var Version = "v0.0.2" +var Version = "v0.0.3"