Skip to content

Commit

Permalink
add struct field pointer test
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Dec 2, 2024
1 parent f2281cb commit 37e2ca9
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions gq/pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := "[email protected]"
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 != "[email protected]" {
t.Fatalf("expected `%s`, received `%s`", "[email protected]", *user.Email)
}
})

t.Run("should json", func(t *testing.T) {
schema := gq.Pointer{gq.String{}}
b, _ := json.Marshal(schema)
Expand Down

0 comments on commit 37e2ca9

Please sign in to comment.