Repository: jensneuse/graphql-go-tools
Pull request #80 (2019-05-20 01:28:29)

Failing empty list bug
Also ran `make format`, so there might be other things in the PR


Patch:
diff --git a/pkg/middleware/invoker.go b/pkg/middleware/invoker.go
index da64ac71..27f3962c 100644
--- a/pkg/middleware/invoker.go
+++ b/pkg/middleware/invoker.go
@@ -9,8 +9,8 @@ import (
 )
 
 type GraphQLRequest struct {
-	OperationName string `json:"operationName,omitempty"`
-	Query         string `json:"query"`
+	OperationName string                 `json:"operationName,omitempty"`
+	Query         string                 `json:"query"`
 	Variables     map[string]interface{} `json:"variables,omitempty"`
 }
 
diff --git a/pkg/parser/list_value_parser.go b/pkg/parser/list_value_parser.go
index 7e96ef43..7b2fdc0c 100644
--- a/pkg/parser/list_value_parser.go
+++ b/pkg/parser/list_value_parser.go
@@ -4,11 +4,11 @@ import (
 	"github.com/jensneuse/graphql-go-tools/pkg/lexing/keyword"
 )
 
-func (p *Parser) parsePeekedListValue(index *int) error {
+func (p *Parser) parsePeekedListValue() (ref int, err error) {
 
 	p.l.Read()
 
-	listValue := p.makeListValue(index)
+	listValue := p.IndexPoolGet()
 
 	for {
 
@@ -16,15 +16,14 @@ func (p *Parser) parsePeekedListValue(index *int) error {
 
 		if peeked == keyword.SQUAREBRACKETCLOSE {
 			p.l.Read()
-			p.putListValue(listValue, index)
-			return nil
+			return p.putListValue(listValue), nil
 
 		} else {
 
 			var next int
 			next, err := p.parseValue()
 			if err != nil {
-				return err
+				return -1, err
 			}
 
 			listValue = append(listValue, next)
diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go
index 5d3d8b34..0c62bd01 100644
--- a/pkg/parser/parser.go
+++ b/pkg/parser/parser.go
@@ -443,13 +443,6 @@ func (p *Parser) initExecutableDefinition() {
 	p.ParsedDefinitions.ExecutableDefinition.FragmentDefinitions = p.IndexPoolGet()
 }
 
-func (p *Parser) makeListValue(index *int) document.ListValue {
-	value := p.IndexPoolGet()
-	p.ParsedDefinitions.ListValues = append(p.ParsedDefinitions.ListValues, value)
-	*index = len(p.ParsedDefinitions.ListValues) - 1
-	return value
-}
-
 func (p *Parser) makeObjectValue(index *int) document.ObjectValue {
 	value := p.IndexPoolGet()
 	p.ParsedDefinitions.ObjectValues = append(p.ParsedDefinitions.ObjectValues, value)
@@ -731,16 +724,14 @@ func (p *Parser) putFloat(float float32) int {
 	return len(p.ParsedDefinitions.Floats) - 1
 }
 
-func (p *Parser) putListValue(value document.ListValue, index *int) {
+func (p *Parser) putListValue(value document.ListValue) (ref int) {
 	for i, known := range p.ParsedDefinitions.ListValues {
 		if p.integersContainSameValues(value, known) {
-			p.ParsedDefinitions.ListValues = // delete the dupe
-				append(p.ParsedDefinitions.ListValues[:*index], p.ParsedDefinitions.ListValues[*index+1:]...)
-			*index = i
-			return
+			return i
 		}
 	}
-	p.ParsedDefinitions.ListValues[*index] = value
+	p.ParsedDefinitions.ListValues = append(p.ParsedDefinitions.ListValues, value)
+	return len(p.ParsedDefinitions.ListValues) - 1
 }
 
 func (p *Parser) putObjectValue(value document.ObjectValue, index *int) {
diff --git a/pkg/parser/value_parser.go b/pkg/parser/value_parser.go
index 71858136..ad01b0bf 100644
--- a/pkg/parser/value_parser.go
+++ b/pkg/parser/value_parser.go
@@ -41,7 +41,7 @@ func (p *Parser) parseValue() (ref int, err error) {
 		p.parsePeekedByteSlice(&value)
 	case keyword.SQUAREBRACKETOPEN:
 		value.ValueType = document.ValueTypeList
-		err = p.parsePeekedListValue(&value.Reference)
+		value.Reference, err = p.parsePeekedListValue()
 	case keyword.CURLYBRACKETOPEN:
 		value.ValueType = document.ValueTypeObject
 		err = p.parsePeekedObjectValue(&value.Reference)
diff --git a/pkg/proxy/http/proxy.go b/pkg/proxy/http/proxy.go
index 28156c20..20f864ae 100644
--- a/pkg/proxy/http/proxy.go
+++ b/pkg/proxy/http/proxy.go
@@ -50,9 +50,9 @@ func (pr *ProxyRequest) AcceptRequest(buff *bytes.Buffer) error {
 
 func (pr *ProxyRequest) DispatchRequest(buff *bytes.Buffer) (io.ReadCloser, error) {
 	req := middleware.GraphQLRequest{
-		Query: buff.String(),
+		Query:         buff.String(),
 		OperationName: pr.GraphQLRequest.OperationName,
-		Variables: pr.GraphQLRequest.Variables,
+		Variables:     pr.GraphQLRequest.Variables,
 	}
 
 	out := bytes.Buffer{}
diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go
index a234fcad..8e1221df 100644
--- a/pkg/proxy/proxy.go
+++ b/pkg/proxy/proxy.go
@@ -17,10 +17,10 @@ type Proxy struct {
 }
 
 type Request struct {
-	Config     *RequestConfig
-	RequestURL url.URL
-	Body       io.Reader
-	Context    context.Context
+	Config         *RequestConfig
+	RequestURL     url.URL
+	Body           io.Reader
+	Context        context.Context
 	GraphQLRequest middleware.GraphQLRequest
 }
 


Test patch:
diff --git a/pkg/middleware/context_middleware_test.go b/pkg/middleware/context_middleware_test.go
index a752dc51..5b27dbdd 100644
--- a/pkg/middleware/context_middleware_test.go
+++ b/pkg/middleware/context_middleware_test.go
@@ -100,6 +100,23 @@ func TestContextMiddleware(t *testing.T) {
 		}
 		want := testhelper.UglifyRequestString(privateQueryWithEscapedQuote)
 
+		if want != got {
+			panic(fmt.Errorf("\nwant:\n%s\ngot:\n%s", want, got))
+		}
+	})
+	t.Run("empty list as a parameter should work", func(t *testing.T) {
+
+		// it's important to quote the value so the lexer will recognize it's a string value
+		// we might push this including checks into the implementation
+		ctx := context.WithValue(context.Background(), "user", "jsmith@example.org")
+
+		got, err := InvokeMiddleware(&ContextMiddleware{}, ctx, publicSchema, emptyListQuery)
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		want := testhelper.UglifyRequestString(emptyListQuery)
+
 		if want != got {
 			panic(fmt.Errorf("\nwant:\n%s\ngot:\n%s", want, got))
 		}
@@ -113,12 +130,18 @@ schema {
 
 type Query {
 	documents: [Document] @addArgumentFromContext(name: "user",contextKey: "user")
+	Document: [Document]
+	Drawer: [Drawer]
 }
 
 type Document implements Node {
 	owner: String
 	sensitiveInformation: String
 }
+
+type Drawer {
+	documents(owners: [String]!): [Document]
+}
 `
 
 /*
@@ -162,3 +185,13 @@ query myDocuments {
 	}
 }
 `
+
+const emptyListQuery = `
+query emptyList {
+	Drawer {
+		documents(owners: []){
+			sensitiveInformation
+		}
+	}
+}
+`
diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go
index 0f630f9c..af895414 100644
--- a/pkg/parser/parser_test.go
+++ b/pkg/parser/parser_test.go
@@ -57,13 +57,13 @@ func TestParser_putListValue(t *testing.T) {
 	var listValueIndex int
 	var listValueIndex2 int
 
-	listValue := parser.makeListValue(&listValueIndex)
-	listValue2 := parser.makeListValue(&listValueIndex2)
+	listValue := parser.IndexPoolGet()
+	listValue2 := parser.IndexPoolGet()
 	listValue = append(listValue, valueIndex)
 	listValue2 = append(listValue2, valueIndex)
 
-	parser.putListValue(listValue, &listValueIndex)
-	parser.putListValue(listValue2, &listValueIndex2)
+	listValueIndex = parser.putListValue(listValue)
+	listValueIndex2 = parser.putListValue(listValue2)
 
 	if listValueIndex != listValueIndex2 {
 		panic("expect lists to be merged")