From 37e2ca9ef66c56de90ab267671d59148ab885269 Mon Sep 17 00:00:00 2001 From: aacebo Date: Mon, 2 Dec 2024 23:31:14 +0800 Subject: [PATCH] add struct field pointer test --- gq/pointer_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/gq/pointer_test.go b/gq/pointer_test.go index 11c653e..dd21363 100644 --- a/gq/pointer_test.go +++ b/gq/pointer_test.go @@ -257,6 +257,48 @@ func TestPointer(t *testing.T) { } }) + t.Run("should resolve struct field as pointer", func(t *testing.T) { + type User struct { + Name string `json:"name"` + Email *string `json:"email"` + } + + schema := gq.Object[User]{ + Name: "User", + Fields: gq.Fields{ + "name": gq.Field{Type: gq.String{}}, + "email": gq.Field{Type: gq.Pointer{Type: gq.String{}}}, + }, + } + + email := "test@test.com" + res := schema.Do(&gq.DoParams{ + Query: "{name,email}", + Value: User{ + Name: "test", + Email: &email, + }, + }) + + if res.Error != nil { + t.Fatal(res.Error) + } + + user, ok := res.Data.(User) + + if !ok { + t.Fatal(res.Data) + } + + if user.Name != "test" { + t.Fatalf("expected `%s`, received `%s`", "test", user.Name) + } + + if user.Email == nil || *user.Email != "test@test.com" { + t.Fatalf("expected `%s`, received `%s`", "test@test.com", *user.Email) + } + }) + t.Run("should json", func(t *testing.T) { schema := gq.Pointer{gq.String{}} b, _ := json.Marshal(schema)