Skip to content

Commit e449742

Browse files
authored
fix(parser): allow GRAPHQL method (#552)
* fix(parser): allow GRAPHQL method * fix(lsp): skip requests with no name
1 parent b384bf9 commit e449742

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

lua/kulala/cmd/lsp.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ local header_values = {
286286
{ "application/octet-stream" },
287287
{ "application/pdf" },
288288
{ "application/zip" },
289+
{ "application/graphql-response+json" },
289290
{ "text/plain" },
290291
{ "text/html" },
291292
{ "text/css" },
@@ -836,7 +837,9 @@ local function get_symbols()
836837
vim.iter(cache.requests):each(function(request)
837838
local cnum = 0
838839
local line = request.show_icon_line_number - 2
839-
symbol = get_symbol(request.name, kind.Function, line) or {}
840+
841+
symbol = get_symbol(request.name, kind.Function, line)
842+
if not symbol then return end
840843

841844
if #request.scripts.pre_request.inline + #request.scripts.pre_request.files > 0 then
842845
table.insert(symbol.children, get_symbol("|< Pre-request script", kind.Module, line - 2))

lua/kulala/parser/graphql.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ end
5555

5656
M.get_json = function(body)
5757
local query, variables = parse(body)
58-
local json = { query = "", variables = "" }
58+
local json = { query = "" }
5959

6060
if not (query and #query > 0) then return end
6161

lua/kulala/parser/request.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ local function process_graphql(request)
276276
or PARSER_UTILS.contains_header(request.headers, "x-request-type", "graphql")
277277

278278
if request.body and #request.body > 0 and is_graphql then
279+
request.method = "POST"
279280
local gql_json = GRAPHQL_PARSER.get_json(request.body)
280281

281282
if gql_json then
@@ -285,6 +286,7 @@ local function process_graphql(request)
285286
request.body_computed = gql_json
286287
end
287288
end
289+
288290
return request
289291
end
290292

lua/kulala/utils/json.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ M.format = function(json_string)
2727
return result.stdout
2828
end
2929

30+
---Parse a JSON string into a Lua table
31+
---@param str string
32+
---@param opts table<{object: boolean, array: boolean, verbose: boolean}> -- verbose: log errors
33+
---@return table|nil, string|nil
3034
M.parse = function(str, opts)
3135
opts = opts or { object = true, array = true }
3236
local verbose = opts.verbose or false

tests/functional/requests_spec.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,15 @@ describe("requests", function()
504504
kulala.run()
505505
wait_for_requests(1)
506506

507-
local request_body_computed = DB.data.current_request.body_computed
507+
local request = DB.data.current_request
508508

509-
assert.has_string(request_body_computed, '"query":"query Person($id: ID) { person(personID: $id) { name } }')
510-
assert.has_string(request_body_computed, '"variables":{"id":1}')
509+
assert.is_same("POST", request.method)
510+
assert.has_properties(request.headers, {
511+
["Content-Type"] = "application/json",
512+
})
513+
514+
assert.has_string(request.body_computed, '"query":"query Person($id: ID) { person(personID: $id) { name } }')
515+
assert.has_string(request.body_computed, '"variables":{"id":1}')
511516
end)
512517

513518
it("runs API callbacks", function()

0 commit comments

Comments
 (0)