Skip to content

Commit

Permalink
fix(degraphql): handle multiple parameter types when converting query…
Browse files Browse the repository at this point in the history
… parameters (#9911)

After the query parameters of the graphql are parsed into AST, there are three types: listType, nonNullType, and namedType.
Are their structures different, so the way of obtaining variable types from the structure is also different.

This PR is compatible with different structures for obtaining variable types.

Fix FTI-6153
  • Loading branch information
tzssangglass authored Aug 13, 2024
1 parent 8342ccb commit 8d7f501
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "**degraphql**: Fixed an issue where multiple parameter types were not handled correctly when converting query parameters."
type: bugfix
scope: Plugin
17 changes: 15 additions & 2 deletions kong/plugins/degraphql/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ local workspaces = require "kong.workspaces"

local type = type
local ipairs = ipairs
local fmt = string.format

local FORCE = true

Expand Down Expand Up @@ -109,8 +108,22 @@ local function coerce_query_variable(query_str, args)

if variable_definition and type(variable_definition) == "table" then
for _, variable in ipairs(variable_definition) do
local var_kind = variable.type.kind
local var_type
if var_kind == "listType" or var_kind == "nonNullType" then
var_type = variable.type.type.name.value
end

if var_kind == "namedType" then
var_type = variable.type.name.value
end

if not var_type then
kong.log.err("unsupported variable type: ", var_kind)
return
end

local var_name = variable.variable.name.value
local var_type = variable.type.type.name.value
local var_value = args[var_name]

if var_value then
Expand Down
46 changes: 46 additions & 0 deletions spec-ee/03-plugins/20-degraphql/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ local graphql_mock_query = {
}
}
]],

query_with_mixed_param_type = [[
query($repoCount: [Int], $rating: Float!, $isActive: Boolean) {
user {
id
name
isActive @include(if: $isActive)
repositories(last: $repoCount) {
nodes {
id
name
languages: stars(rating: $rating)
}
}
friends: following {
name
isActive
}
}
}
]],
}

for _, strategy in helpers.each_strategy() do
Expand Down Expand Up @@ -122,6 +143,12 @@ for _, strategy in helpers.each_strategy() do
query = graphql_mock_query.fetch_recent_repos,
})

assert(db.degraphql_routes:insert {
service = { id = service.id },
uri = "/query_with_mixed_param_type",
query = graphql_mock_query.query_with_mixed_param_type,
})

helpers.start_kong({
database = strategy,
plugins = "bundled,degraphql",
Expand Down Expand Up @@ -197,6 +224,25 @@ for _, strategy in helpers.each_strategy() do
assert.response(res).has.status(200)
local json = assert.response(res).has.jsonbody()
assert.same(ori_graph_query, json.data.variables)

local ori_graph_query_with_mixed_param_type = {
repoCount = 3, -- A signed 32‐bit integer
rating = 4.5, -- A signed double-precision floating-point value.
isActive = true, -- true or false.
}

local res = assert(proxy_client:send {
method = "GET",
path = "/query_with_mixed_param_type",
query = ori_graph_query_with_mixed_param_type,
headers = {
["Host"] = "graphql.test"
}
})

assert.response(res).has.status(200)
local json = assert.response(res).has.jsonbody()
assert.same(ori_graph_query_with_mixed_param_type, json.data.variables)
end)

it("can update graphql router when creating new graphql_route entity", function ()
Expand Down

0 comments on commit 8d7f501

Please sign in to comment.