Skip to content

Commit

Permalink
restrcutured logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kofoworola committed Jun 20, 2023
1 parent 6093c54 commit a017257
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
9 changes: 4 additions & 5 deletions pkg/engine/datasource/rest_datasource/rest_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 messasge
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)
}

Expand Down
84 changes: 83 additions & 1 deletion pkg/engine/datasource/rest_datasource/rest_datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit a017257

Please sign in to comment.