Skip to content

Commit

Permalink
add auth, unwind
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpeterkort committed Jan 3, 2025
1 parent 1268f54 commit 0bb60aa
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.5

require (
github.com/99designs/gqlgen v0.17.60
github.com/bmeg/grip v0.0.0-20250102220137-c2dbbc7c6230
github.com/bmeg/grip v0.0.0-20250102222412-9654ae9fc7a2
github.com/dop251/goja v0.0.0-20240707163329-b1681fb2a2f5
github.com/gin-gonic/gin v1.8.1
github.com/golang-jwt/jwt/v5 v5.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/bmeg/grip v0.0.0-20241212235918-1eed29a9d6e4 h1:LeJ+lXhtVIeCxEqFR2dRY
github.com/bmeg/grip v0.0.0-20241212235918-1eed29a9d6e4/go.mod h1:xBPjee1i7ZnwR9/UsHtblHyf7X7vAr5tFWTNtmtqmWg=
github.com/bmeg/grip v0.0.0-20250102220137-c2dbbc7c6230 h1:SFEtSyntXBfvzUVJYfRcXIWVyGg3DLEBbyYv2yZvNJw=
github.com/bmeg/grip v0.0.0-20250102220137-c2dbbc7c6230/go.mod h1:xBPjee1i7ZnwR9/UsHtblHyf7X7vAr5tFWTNtmtqmWg=
github.com/bmeg/grip v0.0.0-20250102222412-9654ae9fc7a2 h1:+lkAd7gbc+LQSL8BPxN68N85ovoNJ/7G9MnhphA/+Vk=
github.com/bmeg/grip v0.0.0-20250102222412-9654ae9fc7a2/go.mod h1:xBPjee1i7ZnwR9/UsHtblHyf7X7vAr5tFWTNtmtqmWg=
github.com/bmeg/jsonpath v0.0.0-20210207014051-cca5355553ad h1:ICgBexeLB7iv/IQz4rsP+MimOXFZUwWSPojEypuOaQ8=
github.com/bmeg/jsonpath v0.0.0-20210207014051-cca5355553ad/go.mod h1:ft96Irkp72C7ZrUWRenG7LrF0NKMxXdRvsypo5Njhm4=
github.com/bmeg/jsonschema/v5 v5.3.4-0.20241111204732-55db82022a92 h1:Myx/j+WxfEg+P3nDaizR1hBpjKSLgvr4ydzgp1/1pAU=
Expand Down
72 changes: 59 additions & 13 deletions gql-gen/graph/collectFields.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,36 @@ import (
"fmt"
"reflect"
"strings"
"strconv"

"github.com/99designs/gqlgen/graphql"
"github.com/bmeg/grip/gripql"
"github.com/vektah/gqlparser/v2/ast"
"github.com/bmeg/grip/gripql/inspect"
//"google.golang.org/protobuf/types/known/structpb"

)

type Resolver struct {
GripDb gripql.Client
Schema *ast.Schema
}

type renderTreePath struct{
path []string
unwindPath []string
}

type renderTree struct {
prevName string
moved bool
rFieldPaths map[string][]string
rFieldPaths map[string]renderTreePath
rTree map[string]interface{}
}

func (rt *renderTree) NewElement() string {
rName := fmt.Sprintf("f%d", len(rt.rFieldPaths))
rt.rFieldPaths[rName] = []string{}
rt.rFieldPaths[rName] = renderTreePath{path: []string{}, unwindPath: []string{}}
return rName
}

Expand All @@ -50,18 +59,29 @@ func queryBuild(query **gripql.Query, selSet ast.SelectionSet, curElement string
firstTerm = newParentPath[:dotIndex]
}
exists := false
for _, term := range rt.rFieldPaths[curElement] {
for _, term := range rt.rFieldPaths[curElement].path {
if term == firstTerm {
exists = true
break
}
}
if !exists {
rt.rFieldPaths[curElement] = append(rt.rFieldPaths[curElement], firstTerm)
rPath := rt.rFieldPaths[curElement]
rPath.path = append(rPath.path, firstTerm)
rt.rFieldPaths[curElement] = rPath
}
//rt.rFieldPaths[curElement] = append(rt.rFieldPaths[curElement], newParentPath)
currentTree[curElement] = rt.rFieldPaths[curElement]
} else {

/*fmt.Printf("OBJ DEF: %#v\n", sel.ObjectDefinition)
fmt.Printf("DEF TYPE: %#v\n", sel.Definition.Type)
fmt.Printf("DEF TYPE ELEM: %#v\n", sel.Definition.Type.Elem)
fmt.Println("PARENT PATH: ", newParentPath)*/
if sel.Definition.Type.Elem != nil{
rPath := rt.rFieldPaths[curElement]
rPath.unwindPath = append(rPath.unwindPath, newParentPath)
rt.rFieldPaths[curElement] = rPath
}
queryBuild(query, sel.SelectionSet, curElement, rt, newParentPath, currentTree)
}
case *ast.InlineFragment:
Expand All @@ -82,11 +102,9 @@ func queryBuild(query **gripql.Query, selSet ast.SelectionSet, curElement string

func (r *queryResolver) GetSelectedFieldsAst(ctx context.Context, sourceType string) ([]any, error) {
resctx := graphql.GetFieldContext(ctx)
pesctx := graphql.GetOperationContext(ctx)
fmt.Println("VARIABLES: ", pesctx.Variables)

rt := &renderTree{
rFieldPaths: map[string][]string{"f0": []string{}},
rFieldPaths: map[string]renderTreePath{"f0": renderTreePath{path: []string{}, unwindPath: []string{}}},
rTree: map[string]any{},
}
q := gripql.V().HasLabel(sourceType[:len(sourceType)-4]).As("f0")
Expand All @@ -98,17 +116,43 @@ func (r *queryResolver) GetSelectedFieldsAst(ctx context.Context, sourceType str
render := map[string]any{}
for checkpoint, paths := range rt.rFieldPaths {
render[checkpoint+"_gid"] = "$" + checkpoint + ".id"
for _, path := range paths {
for _, path := range paths.path {
render[path+"_data"] = "$" + checkpoint + "." + path
}
}

// Traverse back to f0 since only filters on the root node are applied currently
q = q.Select("f0")

// apply the unwinds on f0 before the filters so that the filters work properly.
applyUnwinds(&q, rt)
fmt.Println("QUERY BEFORE: ",q)

fmt.Printf("ARGS: %#v\n", resctx.Args)
applyFilters(&q, resctx.Args)

//fmt.Printf("RENDER: %#v\n", render)
authList, ok := ctx.Value("auth_list").([]interface{})
if !ok {
return nil, fmt.Errorf("auth_list not found or invalid")
}

//fmt.Println("AUTHLIST: ", authList)
Has_Statement := &gripql.GraphStatement{Statement: &gripql.GraphStatement_Has{gripql.Within("auth_resource_path", authList...)}}
steps := inspect.PipelineSteps(q.Statements)
FilteredGS := []*gripql.GraphStatement{}
for i, v := range q.Statements{
steps_index, _ := strconv.Atoi(steps[i])
if i == 0{
FilteredGS = append(FilteredGS, v)
continue
}else if i == steps_index {
FilteredGS = append(FilteredGS, v, Has_Statement)
}else{
FilteredGS = append(FilteredGS, v)
}
}

q.Statements = FilteredGS
q = q.Render(render)
fmt.Println("QUERY AFTER: ", q)

Expand All @@ -132,8 +176,10 @@ func buildOutputTree(renderTree map[string]interface{}, values map[string]interf
output := map[string]interface{}{}
for key, val := range renderTree {
switch v := val.(type) {
case []string:
for _, fieldPath := range v {
case renderTreePath:
fmt.Println("V: ",v)
for _, fieldPath := range v.path {
fmt.Println("FIELD PATH: ", fieldPath)
segments := strings.Split(fieldPath, ".")
current := output
for i := 0; i < len(segments)-1; i++ {
Expand All @@ -142,7 +188,7 @@ func buildOutputTree(renderTree map[string]interface{}, values map[string]interf
if _, exists := current[segments[i]]; !exists {
current[segments[i]] = map[string]interface{}{}
}
current = current[segments[i]].(map[string]interface{})
//current = current[segments[i]].(map[string]interface{})
}
lastSegment := segments[len(segments)-1]
fieldKey := fieldPath + "_data"
Expand Down
28 changes: 20 additions & 8 deletions gql-gen/graph/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,33 @@ import (
"github.com/bmeg/grip/gripql"
)

func applyUnwinds(query **gripql.Query, rt *renderTree){
/* Assumes query is at f0 and only applies unwinds to that node currently*/
for _, val := range rt.rFieldPaths["f0"].unwindPath{
*query = (*query).Unwind(val)
}
}

func applyFilters(query **gripql.Query, args map[string]any) error {
//Todo: support "accessiblity", "format", "sort":
//Todo: support "sort" operations
//fmt.Printf("FIRST: %v, TYPE: %T\n", args, args["first"])
if filter, ok := args["filter"]; ok {
chainedFilter, err := applyJsonFilter(filter.(map[string]any))
if err != nil {
return err
if filter != nil && len(filter.(map[string]any)) > 0{
chainedFilter, err := applyJsonFilter(filter.(map[string]any))
if err != nil {
fmt.Println("ERR != NIL: ", err)
return err
}
//fmt.Printf("CHAINED FILTER: %s\n", chainedFilter.String())
*query = (*query).Has(chainedFilter)
}
fmt.Printf("CHAINED FILTER: %s\n", chainedFilter.String())
*query = (*query).Has(chainedFilter)
}
if first, ok := args["first"]; ok {
if first.(*int) == nil {
firstPtr, _ := first.(*int)
if firstPtr == nil {
*query = (*query).Limit(uint32(10))
} else {
*query = (*query).Limit(uint32(*first.(*int)))
*query = (*query).Limit(uint32(*firstPtr))
}
}
if offset, ok := args["offset"]; ok {
Expand Down
3 changes: 2 additions & 1 deletion gql-gen/graph/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (

// Organization is the resolver for the organization field.
func (r *queryResolver) Organization(ctx context.Context, offset *int, first *int, filter map[string]interface{}, sort map[string]interface{}, accessibility *model.Accessibility, format *model.Format) ([]model.OrganizationType, error) {

data, err := r.GetSelectedFieldsAst(ctx, "OrganizationType")
fmt.Println("DATA: ", data)
//fmt.Println("DATA: ", data)

slice, err := model.UnmarshalOrganizationTypeSlice(data)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion mongo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Server:
Drivers:
mongo:
MongoDB:
URL: mongodb://local-mongodb
URL: mongodb://mongodb-service

0 comments on commit 0bb60aa

Please sign in to comment.