Repository: jensneuse/graphql-go-tools
Pull request #134 (2019-12-04 13:50:49)

add test and implement fix for variable enconding using Strings only for now
This is a small fix to fix String variable encoding.


Patch:
diff --git a/pkg/execution/datasource_graphql.go b/pkg/execution/datasource_graphql.go
index 4cac092b..34b60093 100644
--- a/pkg/execution/datasource_graphql.go
+++ b/pkg/execution/datasource_graphql.go
@@ -312,7 +312,7 @@ func (g *GraphQLDataSource) Resolve(ctx Context, args ResolvedArgs, out io.Write
 		url = "https://" + url
 	}
 
-	variables := map[string]json.RawMessage{}
+	variables := map[string]interface{}{}
 	for i := 0; i < len(args); i++ {
 		key := args[i].Key
 		switch {
@@ -320,7 +320,7 @@ func (g *GraphQLDataSource) Resolve(ctx Context, args ResolvedArgs, out io.Write
 		case bytes.Equal(key, literal.URL):
 		case bytes.Equal(key, literal.QUERY):
 		default:
-			variables[string(key)] = args[i].Value
+			variables[string(key)] = string(args[i].Value)
 		}
 	}
 
@@ -346,7 +346,7 @@ func (g *GraphQLDataSource) Resolve(ctx Context, args ResolvedArgs, out io.Write
 		return CloseConnectionIfNotStream
 	}
 
-	g.log.Error("GraphQLDataSource.request",
+	g.log.Debug("GraphQLDataSource.request",
 		zap.String("url", url),
 		zap.String("data", string(gqlRequestData)),
 	)


Test patch:
diff --git a/pkg/execution/execution_test.go b/pkg/execution/execution_test.go
index 6974a48c..98d4dfd0 100644
--- a/pkg/execution/execution_test.go
+++ b/pkg/execution/execution_test.go
@@ -1234,6 +1234,112 @@ func TestExecutor_ListWithPath(t *testing.T) {
 	}
 }
 
+func TestExecutor_GraphqlDataSourceWithParams(t *testing.T) {
+
+	graphQL1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+
+		body,err := httputil.DumpRequest(r,true)
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		fmt.Println(string(body))
+
+		_, err = w.Write([]byte(`{
+			"data": {
+				"countries": [
+					{"id":1},{"id":2}
+				]
+			}
+		}`))
+		if err != nil {
+			t.Fatal(err)
+		}
+	}))
+
+	plan := &Object{
+		operationType: ast.OperationTypeQuery,
+		Fields: []Field{
+			{
+				Name: []byte("data"),
+				Value: &Object{
+					Fetch: &SingleFetch{
+						Source: &DataSourceInvocation{
+							Args: []Argument{
+								&StaticVariableArgument{
+									Name:  literal.HOST,
+									Value: []byte(graphQL1.URL),
+								},
+								&StaticVariableArgument{
+									Name:  literal.URL,
+									Value: []byte("/graphql"),
+								},
+								&StaticVariableArgument{
+									Name:  literal.QUERY,
+									Value: []byte("query q1($code: String!){countries(code: $code){id}}"),
+								},
+								&ContextVariableArgument{
+									Name:         []byte("code"),
+									VariableName: []byte("code"),
+								},
+							},
+							DataSource: &GraphQLDataSource{
+								log:zap.NewNop(),
+							},
+						},
+						BufferName: "countries",
+					},
+					Fields: []Field{
+						{
+							Name:        []byte("countries"),
+							HasResolver: true,
+							Value: &List{
+								PathSelector: PathSelector{
+									Path: "countries",
+								},
+								Value: &Object{
+									Fields: []Field{
+										{
+											Name: []byte("id"),
+											Value: &Value{
+												PathSelector: PathSelector{
+													Path: "id",
+												},
+												QuoteValue: false,
+											},
+										},
+									},
+								},
+							},
+						},
+					},
+				},
+			},
+		},
+	}
+
+	out := &bytes.Buffer{}
+	ex := NewExecutor()
+	ctx := Context{
+		Context: context.Background(),
+		Variables: map[uint64][]byte{
+			xxhash.Sum64String("code"): []byte("DE"),
+		},
+	}
+
+	_, err := ex.Execute(ctx, plan, out)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	want := `{"data":{"countries":[{"id":1},{"id":2}]}}`
+	got := out.String()
+
+	if got != want {
+		t.Fatalf("want: %s\ngot: %s\n", want, got)
+	}
+}
+
 func TestExecutor_ObjectWithPath(t *testing.T) {
 
 	plan := &Object{