Skip to content

Commit 91a9293

Browse files
feat: graphql support (#433)
1 parent 3030841 commit 91a9293

File tree

5 files changed

+83
-5
lines changed

5 files changed

+83
-5
lines changed

lua/rest-nvim/client/curl/cli.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,9 @@ function builder.build(req, ignore_stats)
298298
if req.body then
299299
if req.body.__TYPE == "external" then
300300
insert(args, builder.file(req.body.data.path))
301-
elseif req.body.__TYPE == "graphql" then
302-
log.error("graqphql body is not supportted yet")
303301
elseif req.body.__TYPE == "multipart_form_data" then
304302
log.error("multipart-form-data body is not supportted yet")
305-
elseif vim.list_contains({ "json", "xml", "raw" }, req.body.__TYPE) then
303+
elseif vim.list_contains({ "json", "xml", "raw", "graphql", }, req.body.__TYPE) then
306304
insert(args, builder.raw_body(req.body.data))
307305
else
308306
log.error(("unkown body type: '%s'"):format(req.body.__TYPE))

lua/rest-nvim/client/curl_cli.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local COMPATIBLE_METHODS = {
1111
"CONNECT",
1212
"PATCH",
1313
"LIST",
14+
"GRAPHQL",
1415
}
1516

1617
---@type rest.Client

lua/rest-nvim/parser/init.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ function parser.parse_body(content_type, body_node, source, context)
136136
name = get_node_field_text(body_node, "name", source),
137137
path = path,
138138
}
139+
elseif node_type == "graphql_body" then
140+
body.__TYPE = "graphql"
141+
local query_text = vim.treesitter.get_node_text(assert(body_node:named_child(0)), source)
142+
local variables_text
143+
local variables_node = body_node:named_child(1)
144+
if variables_node then
145+
variables_text = vim.treesitter.get_node_text(variables_node, source)
146+
end
147+
body.data = vim.json.encode({
148+
query = query_text,
149+
variables = vim.json.decode(variables_text)
150+
})
151+
logger.debug(body.data)
139152
elseif node_type == "json_body" or content_type == "application/json" then
140153
body.__TYPE = "json"
141154
body.data = vim.trim(vim.treesitter.get_node_text(body_node, source))
@@ -171,8 +184,6 @@ function parser.parse_body(content_type, body_node, source, context)
171184
body.__TYPE = "multipart_form_data"
172185
-- TODO:
173186
logger.error("multipart form data is not supported yet")
174-
elseif node_type == "graphql_body" then
175-
logger.error("graphql body is not supported yet")
176187
end
177188
return body
178189
end
@@ -370,6 +381,9 @@ function parser.parse(node, source, ctx)
370381
logger.info("no method provided, falling back to 'GET'")
371382
method = "GET"
372383
end
384+
if method == "GRAPHQL" then
385+
method = "POST"
386+
end
373387
-- NOTE: url will be parsed after because in-place variables should be parsed first
374388
local url
375389

spec/examples/graphql.http

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@token = YOUR_GITHUB_OAUTH_TOKEN
2+
3+
### GraphQL request
4+
GRAPHQL https://api.github.com/graphql
5+
Authorization: token {{token}}
6+
7+
query ($name: String!, $owner: String!) {
8+
repository(name: $name, owner: $owner) {
9+
name
10+
fullName: nameWithOwner
11+
description
12+
diskUsage
13+
forkCount
14+
stargazers(first: 5) {
15+
totalCount
16+
nodes {
17+
login
18+
name
19+
}
20+
}
21+
watchers {
22+
totalCount
23+
}
24+
}
25+
}
26+
27+
{
28+
"name": "NativeVim",
29+
"owner": "boltlessengineer"
30+
}

spec/parser/http_parser_spec.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,41 @@ key5 = value5
202202
},
203203
}, parser.parse(req_node, source))
204204
end)
205+
it("parse graphql body", function ()
206+
local source = open("spec/examples/graphql.http")
207+
local _, tree = utils.ts_parse_source(source)
208+
local req_node = assert(tree:root():child(1))
209+
local req = parser.parse(req_node, source)
210+
assert(req)
211+
assert.same("POST", req.method)
212+
assert.same("graphql", req.body.__TYPE)
213+
assert.same({
214+
query = [[query ($name: String!, $owner: String!) {
215+
repository(name: $name, owner: $owner) {
216+
name
217+
fullName: nameWithOwner
218+
description
219+
diskUsage
220+
forkCount
221+
stargazers(first: 5) {
222+
totalCount
223+
nodes {
224+
login
225+
name
226+
}
227+
}
228+
watchers {
229+
totalCount
230+
}
231+
}
232+
}
233+
]],
234+
variables = {
235+
name = "NativeVim",
236+
owner = "boltlessengineer",
237+
}
238+
}, vim.json.decode(req.body.data))
239+
end)
205240
end)
206241

207242
describe("variables", function()

0 commit comments

Comments
 (0)