diff --git a/pkg/engine/datasource/rest_datasource/rest_datasource.go b/pkg/engine/datasource/rest_datasource/rest_datasource.go index ceae9f32af..5d80cee369 100644 --- a/pkg/engine/datasource/rest_datasource/rest_datasource.go +++ b/pkg/engine/datasource/rest_datasource/rest_datasource.go @@ -167,11 +167,10 @@ Next: typeRef := p.v.Operation.VariableDefinitions[variableDefRef].Type typeName := p.v.Operation.TypeNameString(typeRef) typeKind := p.v.Operation.Types[typeRef].TypeKind - query[i].rawMessage = []byte(`"` + query[i].Value + `"`) - // if the type is not a nullable or non-nullable string, leave the rawMessage with quotes - if typeKind == ast.TypeKindList || - (typeName != "" && typeName != typeString) || - (typeKind == ast.TypeKindNonNull && p.v.Operation.TypeNameString(p.v.Operation.Types[typeRef].OfType) != typeString) { + // if type is a nullable or non-nullable string, add quotes to the raw message + if typeName == typeString || (typeKind == ast.TypeKindNonNull && p.v.Operation.TypeNameString(p.v.Operation.Types[typeRef].OfType) == typeString) { + query[i].rawMessage = []byte(`"` + query[i].Value + `"`) + } else { query[i].rawMessage = []byte(query[i].Value) } diff --git a/pkg/engine/datasource/rest_datasource/rest_datasource_test.go b/pkg/engine/datasource/rest_datasource/rest_datasource_test.go index ff9c02dc63..845ccb6bdc 100644 --- a/pkg/engine/datasource/rest_datasource/rest_datasource_test.go +++ b/pkg/engine/datasource/rest_datasource/rest_datasource_test.go @@ -26,6 +26,7 @@ const ( withArgument(id: String!, name: String, optional: String): Friend withArrayArguments(names: [String]): Friend withIntArgument(limit: Int): Friend + withStringArgument(name: String!): Friend } type Subscription { @@ -137,6 +138,14 @@ query ArgumentQuery($in: Int!) { } ` + stringArgumentOperationNonNullableString = ` +query ArgumentQuery($in: String!) { + withStringArgument(name: $in) { + name + } + } +` + createFriendOperation = ` mutation CreateFriend($friendVariable: InputFriend!) { createFriend(friend: $friendVariable) { @@ -1177,7 +1186,7 @@ func TestFastHttpJsonDataSourcePlanning(t *testing.T) { DisableResolveFieldPositions: true, }, )) - t.Run("get request with non null string as query param", datasourcetesting.RunTest(schema, intArgumentOperationNonNullableInt, "ArgumentQuery", + t.Run("get request with non null int as query param", datasourcetesting.RunTest(schema, intArgumentOperationNonNullableInt, "ArgumentQuery", &plan.SynchronousResponsePlan{ Response: &resolve.GraphQLResponse{ Data: &resolve.Object{ @@ -1250,6 +1259,79 @@ func TestFastHttpJsonDataSourcePlanning(t *testing.T) { DisableResolveFieldPositions: true, }, )) + t.Run("get request with non null string as query param", datasourcetesting.RunTest(schema, stringArgumentOperationNonNullableString, "ArgumentQuery", + &plan.SynchronousResponsePlan{ + Response: &resolve.GraphQLResponse{ + Data: &resolve.Object{ + Fetch: &resolve.SingleFetch{ + BufferId: 0, + Input: `{"query_params":[{"name":"name","value":"$$0$$"}],"method":"GET","url":"https://example.com/friend"}`, + DataSource: &Source{}, + Variables: resolve.NewVariables( + &resolve.ContextVariable{ + Path: []string{"in"}, + Renderer: resolve.NewPlainVariableRendererWithValidation(`{"type":["string"]}`), + }, + ), + DataSourceIdentifier: []byte("rest_datasource.Source"), + DisableDataLoader: true, + }, + Fields: []*resolve.Field{ + { + BufferID: 0, + HasBuffer: true, + Name: []byte("withStringArgument"), + Value: &resolve.Object{ + Nullable: true, + Fields: []*resolve.Field{ + { + Name: []byte("name"), + Value: &resolve.String{ + Path: []string{"name"}, + Nullable: true, + }, + }, + }, + }, + }, + }, + }, + }, + }, + plan.Configuration{ + DataSources: []plan.DataSourceConfiguration{ + { + RootNodes: []plan.TypeField{ + { + TypeName: "Query", + FieldNames: []string{"withStringArgument"}, + }, + }, + Custom: ConfigJSON(Configuration{ + Fetch: FetchConfiguration{ + URL: "https://example.com/friend", + Method: "GET", + Query: []QueryConfiguration{ + { + Name: "name", + Value: "{{ .arguments.name }}", + }, + }, + }, + }), + Factory: &Factory{}, + }, + }, + Fields: []plan.FieldConfiguration{ + { + TypeName: "Query", + FieldName: "withStringArgument", + DisableDefaultMapping: true, + }, + }, + DisableResolveFieldPositions: true, + }, + )) t.Run("get request with array query", datasourcetesting.RunTest(schema, arrayArgumentOperation, "ArgumentQuery", &plan.SynchronousResponsePlan{ Response: &resolve.GraphQLResponse{