Skip to content

Commit

Permalink
Merge branch 'main' into code-review
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Jan 10, 2025
2 parents f24f940 + 42b1452 commit b9226a5
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 26 deletions.
10 changes: 5 additions & 5 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ runtimes:
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
enabled:
- [email protected].5
- [email protected].346
- [email protected].6
- [email protected].350
- git-diff-check
- [email protected]
- golangci-lint@1.62.2
- golangci-lint@1.63.4
- [email protected]
- [email protected]
- [email protected]
- renovate@39.90.2
- [email protected].0
- renovate@39.92.0
- [email protected].1
- [email protected]

actions:
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
- feat: add readfrom json tag to support reverse edges
[#49](https://github.com/hypermodeinc/modusDB/pull/49)

- chore: Refactoring package management #51 [#51](https://github.com/hypermodeinc/modusDB/pull/51)
- chore: Refactoring package management [#51](https://github.com/hypermodeinc/modusDB/pull/51)

- fix: alter schema on reverse edge after querying schema
[#55](https://github.com/hypermodeinc/modusDB/pull/55)

## 2025-01-02 - Version 0.1.0

Expand Down
9 changes: 4 additions & 5 deletions api/mutations/mutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ import (
)

func HandleReverseEdge(jsonName string, value reflect.Type, nsId uint64, sch *schema.ParsedSchema,
jsonToReverseEdgeTags map[string]string) error {
if jsonToReverseEdgeTags[jsonName] == "" {
reverseEdgeStr string) error {
if reverseEdgeStr == "" {
return nil
}

if value.Kind() != reflect.Slice || value.Elem().Kind() != reflect.Struct {
return fmt.Errorf("reverse edge %s should be a slice of structs", jsonName)
}

reverseEdge := jsonToReverseEdgeTags[jsonName]
typeName := strings.Split(reverseEdge, ".")[0]
typeName := strings.Split(reverseEdgeStr, ".")[0]
u := &pb.SchemaUpdate{
Predicate: apiutils.AddNamespace(nsId, reverseEdge),
Predicate: apiutils.AddNamespace(nsId, reverseEdgeStr),
ValueType: pb.Posting_UID,
Directive: pb.SchemaUpdate_REVERSE,
}
Expand Down
17 changes: 17 additions & 0 deletions api/querygen/dql_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ import (
"strings"
)

type SchemaField struct {
Name string `json:"name"`
}

type SchemaType struct {
Name string `json:"name,omitempty"`
Fields []SchemaField `json:"fields,omitempty"`
}

type SchemaResponse struct {
Types []SchemaType `json:"types,omitempty"`
}

type QueryFunc func() string

const (
Expand Down Expand Up @@ -56,6 +69,10 @@ const (
}
`

SchemaQuery = `
schema{}
`

FuncUid = `uid(%d)`
FuncEq = `eq(%s, %s)`
FuncSimilarTo = `similar_to(%s, %d, "[%s]")`
Expand Down
29 changes: 27 additions & 2 deletions api_mutation_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"context"
"fmt"
"reflect"
"strings"

"github.com/dgraph-io/dgo/v240/protos/api"
"github.com/dgraph-io/dgraph/v24/dql"
Expand Down Expand Up @@ -46,10 +47,34 @@ func generateSetDqlMutationsAndSchema[T any](ctx context.Context, n *Namespace,
var nquad *api.NQuad

if tagMaps.JsonToReverseEdge[jsonName] != "" {
if err := mutations.HandleReverseEdge(jsonName, reflectValueType, n.ID(), sch,
tagMaps.JsonToReverseEdge); err != nil {
reverseEdgeStr := tagMaps.JsonToReverseEdge[jsonName]
typeName := strings.Split(reverseEdgeStr, ".")[0]
currSchema, err := getSchema(ctx, n)
if err != nil {
return err
}

typeFound := false
predicateFound := false
for _, t := range currSchema.Types {
if t.Name == typeName {
typeFound = true
for _, f := range t.Fields {
if f.Name == reverseEdgeStr {
predicateFound = true
break
}
}
break
}
}

if !(typeFound && predicateFound) {
if err := mutations.HandleReverseEdge(jsonName, reflectValueType, n.ID(), sch,
reverseEdgeStr); err != nil {
return err
}
}
continue
}
if jsonName == "gid" {
Expand Down
13 changes: 13 additions & 0 deletions api_query_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,16 @@ func getExistingObject[T any](ctx context.Context, n *Namespace, gid uint64, cf
}
return gid, nil
}

func getSchema(ctx context.Context, n *Namespace) (*querygen.SchemaResponse, error) {
resp, err := n.queryWithLock(ctx, querygen.SchemaQuery)
if err != nil {
return nil, err
}

var schema querygen.SchemaResponse
if err := json.Unmarshal(resp.Json, &schema); err != nil {
return nil, err
}
return &schema, nil
}
39 changes: 26 additions & 13 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,36 @@ func TestCreateApi(t *testing.T) {
string(resp.GetJson()))

// TODO schema{} should work
schemaQuery := `schema(pred: [User.name, User.age, User.clerk_id])
{
type
index
tokenizer
}`
schemaQuery := `schema{}`
resp, err = db1.Query(ctx, schemaQuery)
require.NoError(t, err)

require.JSONEq(t,
`{"schema":
[
{"predicate":"User.age","type":"int"},
{"predicate":"User.clerk_id","type":"string","index":true,"tokenizer":["exact"]},
{"predicate":"User.name","type":"string"}
]
}`,
`{
"types": [
{
"name": "User",
"fields": [
{"name": "User.name"},
{"name": "User.age"},
{"name": "User.clerk_id"}
]
},
{
"name": "dgraph.graphql",
"fields": [
{"name": "dgraph.graphql.schema"},
{"name": "dgraph.graphql.xid"}
]
},
{
"name": "dgraph.graphql.persisted_query",
"fields": [
{"name": "dgraph.graphql.p_query"}
]
}
]
}`,
string(resp.GetJson()))
}

Expand Down

0 comments on commit b9226a5

Please sign in to comment.