Skip to content

Commit

Permalink
add support for reverse edges in query
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Jan 3, 2025
1 parent 7df884a commit 51e37ba
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func Query[T any](db *DB, queryParams QueryParams, ns ...uint64) ([]uint64, []T,
return nil, nil, err
}

return executeQuery[T](ctx, n, queryParams, false)
return executeQuery[T](ctx, n, queryParams, true)
}

func Delete[T any, R UniqueField](db *DB, uniqueField R, ns ...uint64) (uint64, T, error) {
Expand Down
8 changes: 8 additions & 0 deletions api_dql.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ const (
}
`

reverseEdgeQuery = `
%s: ~%s {
gid: uid
expand(_all_)
dgraph.type
}
`

funcUid = `uid(%d)`
funcEq = `eq(%s, %s)`
funcSimilarTo = `similar_to(%s, %d, "[%s]")`
Expand Down
16 changes: 2 additions & 14 deletions api_query_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ func executeGetWithObject[T any, R UniqueField](ctx context.Context, n *Namespac
readFromQuery := ""
if withReverse {
for jsonTag, reverseEdgeTag := range jsonToReverseEdgeTags {
readFromQuery += fmt.Sprintf(`
%s: ~%s {
gid: uid
expand(_all_)
dgraph.type
}
`, getPredicateName(t.Name(), jsonTag), reverseEdgeTag)
readFromQuery += fmt.Sprintf(reverseEdgeQuery, getPredicateName(t.Name(), jsonTag), reverseEdgeTag)
}
}

Expand Down Expand Up @@ -152,13 +146,7 @@ func executeQuery[T any](ctx context.Context, n *Namespace, queryParams QueryPar
readFromQuery := ""
if withReverse {
for jsonTag, reverseEdgeTag := range jsonToReverseEdgeTags {
readFromQuery += fmt.Sprintf(`
%s: ~%s {
uid
expand(_all_)
dgraph.type
}
`, getPredicateName(t.Name(), jsonTag), reverseEdgeTag)
readFromQuery += fmt.Sprintf(reverseEdgeQuery, getPredicateName(t.Name(), jsonTag), reverseEdgeTag)
}
}

Expand Down
67 changes: 66 additions & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ type Branch struct {
Proj Project `json:"proj,omitempty"`
}

func TestReverseEdgeQuery(t *testing.T) {
func TestReverseEdgeGet(t *testing.T) {
ctx := context.Background()
db, err := modusdb.New(modusdb.NewDefaultConfig(t.TempDir()))
require.NoError(t, err)
Expand Down Expand Up @@ -470,6 +470,71 @@ func TestReverseEdgeQuery(t *testing.T) {
require.Len(t, queriedProject.Branches, 2)
require.Equal(t, "B", queriedProject.Branches[0].Name)
require.Equal(t, "B2", queriedProject.Branches[1].Name)

queryBranchesGids, queriedBranches, err := modusdb.Query[Branch](db, modusdb.QueryParams{}, db1.ID())
require.NoError(t, err)
require.Len(t, queriedBranches, 2)
require.Len(t, queryBranchesGids, 2)
require.Equal(t, "B", queriedBranches[0].Name)
require.Equal(t, "B2", queriedBranches[1].Name)

// If i query a branch and get the project, i shouldn't automatically have access to retrieve data about the branches of that project
require.Len(t, queriedBranches[0].Proj.Branches, 0)
}

func TestReverseEdgeQuery(t *testing.T) {
ctx := context.Background()
db, err := modusdb.New(modusdb.NewDefaultConfig(t.TempDir()))
require.NoError(t, err)
defer db.Close()

db1, err := db.CreateNamespace()
require.NoError(t, err)

require.NoError(t, db1.DropData(ctx))

projects := []Project{
{Name: "P1", ClerkId: "456"},
{Name: "P2", ClerkId: "789"},
}

branchCounter := 1
clerkCounter := 100

for _, project := range projects {
projGid, project, err := modusdb.Create(db, project, db1.ID())
require.NoError(t, err)
require.Equal(t, project.Name, project.Name)
require.Equal(t, project.Gid, projGid)

branches := []Branch{
{Name: fmt.Sprintf("B%d", branchCounter), ClerkId: fmt.Sprintf("%d", clerkCounter), Proj: Project{Gid: projGid}},
{Name: fmt.Sprintf("B%d", branchCounter+1), ClerkId: fmt.Sprintf("%d", clerkCounter+1), Proj: Project{Gid: projGid}},
}
branchCounter += 2
clerkCounter += 2

for _, branch := range branches {
branchGid, branch, err := modusdb.Create(db, branch, db1.ID())
require.NoError(t, err)
require.Equal(t, branch.Name, branch.Name)
require.Equal(t, branch.Gid, branchGid)
require.Equal(t, projGid, branch.Proj.Gid)
}
}

queriedProjectsGids, queriedProjects, err := modusdb.Query[Project](db, modusdb.QueryParams{}, db1.ID())
require.NoError(t, err)
require.Len(t, queriedProjects, 2)
require.Len(t, queriedProjectsGids, 2)
require.Equal(t, "P1", queriedProjects[0].Name)
require.Equal(t, "P2", queriedProjects[1].Name)
require.Len(t, queriedProjects[0].Branches, 2)
require.Len(t, queriedProjects[1].Branches, 2)
require.Equal(t, "B1", queriedProjects[0].Branches[0].Name)
require.Equal(t, "B2", queriedProjects[0].Branches[1].Name)
require.Equal(t, "B3", queriedProjects[1].Branches[0].Name)
require.Equal(t, "B4", queriedProjects[1].Branches[1].Name)
}

func TestNestedObjectMutation(t *testing.T) {
Expand Down

0 comments on commit 51e37ba

Please sign in to comment.