Skip to content

Commit

Permalink
Fix aliases for parsing result (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
seka17 committed Nov 4, 2023
1 parent 321aaba commit 46022b1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 9 deletions.
8 changes: 6 additions & 2 deletions executor/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func FindInsertionPoints(
) ([][]string, error) {
oldBranch := copy2DStringArray(startingPoints)

// track the root of the selection set while  we walk
// track the root of the selection set while we walk
selectionSetRoot := selectionSet

// a place to refer to parts of the results
Expand Down Expand Up @@ -164,7 +164,11 @@ func FindInsertionPoints(
}

// the point we are going to add to the list
entryPoint := fmt.Sprintf("%s:%v", foundSelection.Name, entryI)
foundSelectionName := foundSelection.Alias
if foundSelectionName == "" {
foundSelectionName = foundSelection.Name
}
entryPoint := fmt.Sprintf("%s:%v", foundSelectionName, entryI)

newBranchSet := make([][]string, len(oldBranch))
for i, c := range oldBranch {
Expand Down
88 changes: 84 additions & 4 deletions executor/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,90 @@ func TestResultFindInsertionPointRootList(t *testing.T) {
assert.Equal(t, finalInsertionPoint, generatedPoint)
}

func TestResultFindInsertionPointRootListAliases(t *testing.T) {
// we want the list of insertion points that point to
planInsertionPoint := []string{"myUsers", "photoGallery", "myLikedBy"}

// pretend we are in the middle of stitching a larger object
startingPoint := [][]string{}

// there are 6 total insertion points in this example
finalInsertionPoint := [][]string{
// photo 0 is liked by 2 users
{"myUsers:0", "photoGallery:0", "myLikedBy:0#1"},
{"myUsers:0", "photoGallery:0", "myLikedBy:1#2"},
}

// the selection we're going to make
stepSelectionSet := ast.SelectionSet{
&ast.Field{
Name: "users",
Alias: "myUsers",
Definition: &ast.FieldDefinition{
Type: ast.ListType(ast.NamedType("User", nil), nil),
},
SelectionSet: ast.SelectionSet{
&ast.Field{
Name: "photoGallery",
Definition: &ast.FieldDefinition{
Type: ast.ListType(ast.NamedType("Photo", nil), nil),
},
SelectionSet: ast.SelectionSet{
&ast.Field{
Name: "likedBy",
Alias: "myLikedBy",
Definition: &ast.FieldDefinition{
Type: ast.ListType(ast.NamedType("User", nil), nil),
},
SelectionSet: ast.SelectionSet{
&ast.Field{
Name: "totalLikes",
Definition: &ast.FieldDefinition{
Type: ast.NamedType("Int", nil),
},
},
&ast.Field{
Name: "id",
Definition: &ast.FieldDefinition{
Type: ast.NamedType("ID", nil),
},
},
},
},
},
},
},
},
}

// the result of the step
result := map[string]interface{}{
"myUsers": []interface{}{
map[string]interface{}{
"photoGallery": []interface{}{
map[string]interface{}{
"myLikedBy": []interface{}{
map[string]interface{}{
"totalLikes": 10,
"id": "1",
},
map[string]interface{}{
"totalLikes": 10,
"id": "2",
},
},
},
},
},
},
}

generatedPoint, err := FindInsertionPoints(planInsertionPoint, stepSelectionSet, result, startingPoint)
assert.NoError(t, err)

assert.Equal(t, finalInsertionPoint, generatedPoint)
}

func TestResultFindInsertionPointStitchIntoObject(t *testing.T) {
// we want the list of insertion points that point to
planInsertionPoint := []string{"users", "photoGallery", "author"}
Expand Down Expand Up @@ -235,10 +319,6 @@ func TestResultFindInsertionPointWorkOnNil(t *testing.T) {
assert.Equal(t, expected, generatedPoint)
}

func TestResultFindInsertionPoint_handlesNullObjects(t *testing.T) {
t.Skip("Not yet implemented")
}

func TestResultFindObject(t *testing.T) {
// create an object we want to extract
source := map[string]interface{}{
Expand Down
2 changes: 0 additions & 2 deletions executor/selection_set.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IMPORTANT: this is all copy paste, but with some changes to fix data mutation, leading to race conditions in runtime
package executor

import (
Expand All @@ -21,7 +20,6 @@ func FindSelection(matchString string, selectionSet ast.SelectionSet) *ast.Field
}

if len(s.SelectionSet) > 0 {

if f := FindSelection(matchString, s.SelectionSet); f != nil {
return f
}
Expand Down
1 change: 0 additions & 1 deletion queryer/files.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// generally copy paste from nautilus/graphql
package queryer

import (
Expand Down

0 comments on commit 46022b1

Please sign in to comment.