Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix selecting relationship fields in quries #19

Merged
merged 8 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix selecting relationship fields in quries
  • Loading branch information
imperfect-fourth committed Sep 15, 2024
commit 0e3b59f09b0b03b447cc082eff23fc31948ee4ce
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ resp, err := GetUnsafe[User]().Where(
).Limit(5).Select("id", "age").Exec(client)
```

## `fieldgen` and death to raw string literals
## `eywagen` and death to raw string literals
In the examples above, you may have noticed that the `Select` method takes raw
strings as field names. This is prone to typos. `eywa` has a codegen tool for
generating constants and functions for selecting fields.
Install fieldgen
Install eywagen
```bash
go install github.com/imperfect-fourth/eywa/cmd/fieldgen
go install github.com/imperfect-fourth/eywa/cmd/eywagen
```
Add `go:generate` comments to your code.
```go
//go:generate fieldgen -types User -output user_fields.go
//go:generate eywagen -types User -output user_fields.go
type User struct {
...
}
Expand Down Expand Up @@ -114,10 +114,10 @@ resp, err := Get[User]().Where(
).Exec(client)
```

If a model has a relationship with another model, `fieldgen` will generate a
If a model has a relationship with another model, `eywagen` will generate a
function to select fields for that relationship. Eg.
```go
//go:generate fieldgen -types User,Order -output model_fields.go
//go:generate eywagen -types User,Order -output model_fields.go
type User struct {
...
Orders []Order `json:"orders"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ func testTable_customVar[T interface{eywa.JSONValue | eywa.JSONBValue;eywa.Typed
}
}

func testTable_testTable2(subField eywa.ModelFieldName[testTable2], subFields ...eywa.ModelFieldName[testTable2]) string {
buf := bytes.NewBuffer([]byte("testTable2 {"))
func testTable_testTable2(subField eywa.ModelFieldName[testTable2], subFields ...eywa.ModelFieldName[testTable2]) eywa.ModelFieldName[testTable] {
buf := bytes.NewBuffer([]byte((new(testTable2)).ModelName()))
buf.WriteString(" {")
buf.WriteString(string(subField))
for _, f := range subFields {
buf.WriteString("\n")
buf.WriteString(string(f))
}
buf.WriteString("}")
return buf.String()
return eywa.ModelFieldName[testTable](buf.String())
}
const testTable_JsonBCol eywa.ModelFieldName[testTable] = "jsonb_col"

Expand Down
37 changes: 37 additions & 0 deletions cmd/eywagen/eywatest/eywa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,43 @@ name
}
}

func TestRelationshipSelectQuery(t *testing.T) {
age := 10
q := eywa.Get[testTable]().Limit(2).Offset(1).DistinctOn(testTable_Name).OrderBy(
eywa.Desc[testTable](testTable_Name),
).Where(
eywa.Or(
eywa.Eq[testTable](testTable_NameField("abcd")),
eywa.Eq[testTable](testTable_AgeField(&age)),
),
).Select(
testTable_Name,
testTable_testTable2(
testTable2_ID,
),
)

expected := `query get_test_table {
test_table(limit: 2, offset: 1, distinct_on: name, where: {_or: [{name: {_eq: "abcd"}}, {age: {_eq: 10}}]}, order_by: {name: desc}) {
test_table2 {id}
name
}
}`
if assert.Equal(t, expected, q.Query()) {
accessKey := os.Getenv("TEST_HGE_ACCESS_KEY")
c := eywa.NewClient("https://aware-cowbird-80.hasura.app/v1/graphql", &eywa.ClientOpts{
Headers: map[string]string{
"x-hasura-access-key": accessKey,
},
})

resp, err := q.Exec(c)

assert.NoError(t, err)
assert.Equal(t, []testTable{{Name: "abcd"}, {Name: "abc"}}, resp)
}
}

func TestUpdateQuery(t *testing.T) {
q := eywa.Update[testTable]().Where(
eywa.Eq[testTable](testTable_IDField(3)),
Expand Down
9 changes: 6 additions & 3 deletions cmd/eywagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,16 @@ func %sVar[T interface{%s;eywa.TypedValue}](val %s) eywa.ModelField[%s] {
`

modelRelationshipNameFunc = `
func %s(subField eywa.ModelFieldName[%s], subFields ...eywa.ModelFieldName[%s]) string {
buf := bytes.NewBuffer([]byte("%s {"))
func %s(subField eywa.ModelFieldName[%s], subFields ...eywa.ModelFieldName[%s]) eywa.ModelFieldName[%s] {
buf := bytes.NewBuffer([]byte((new(%s)).ModelName()))
buf.WriteString(" {")
buf.WriteString(string(subField))
for _, f := range subFields {
buf.WriteString("\n")
buf.WriteString(string(f))
}
buf.WriteString("}")
return buf.String()
return eywa.ModelFieldName[%s](buf.String())
}
`
)
Expand Down Expand Up @@ -192,7 +193,9 @@ func parseType(typeName string, pkg *types.Package, contents *fileContent) {
fmt.Sprintf("%s_%s", typeName, field.Name()),
fieldTypeName,
fieldTypeName,
typeName,
fieldName,
typeName,
))
recurseParse = append(recurseParse, fieldTypeName)
} else {
Expand Down
Loading