Skip to content

Commit

Permalink
feat(graphql): support http get and post json request (apache#6343)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuaijinchao authored Feb 18, 2022
1 parent deb3a56 commit 00b7b01
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 9 deletions.
59 changes: 56 additions & 3 deletions apisix/core/ctx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local core_str = require("apisix.core.string")
local core_tab = require("apisix.core.table")
local request = require("apisix.core.request")
local log = require("apisix.core.log")
local json = require("apisix.core.json")
local config_local = require("apisix.core.config_local")
local tablepool = require("tablepool")
local get_var = require("resty.ngxvar").fetch
Expand All @@ -36,7 +37,52 @@ local pcall = pcall


local _M = {version = 0.2}
local GRAPHQL_DEFAULT_MAX_SIZE = 1048576 -- 1MiB
local GRAPHQL_DEFAULT_MAX_SIZE = 1048576 -- 1MiB
local GRAPHQL_REQ_DATA_KEY = "query"
local GRAPHQL_REQ_METHOD_HTTP_GET = "GET"
local GRAPHQL_REQ_METHOD_HTTP_POST = "POST"
local GRAPHQL_REQ_MIME_JSON = "application/json"


local fetch_graphql_data = {
[GRAPHQL_REQ_METHOD_HTTP_GET] = function(ctx, max_size)
local body = request.get_uri_args(ctx)[GRAPHQL_REQ_DATA_KEY]
if not body then
return nil, "failed to read graphql data, args[" ..
GRAPHQL_REQ_DATA_KEY .. "] is nil"
end

if type(body) == "table" then
body = body[1]
end

return body
end,

[GRAPHQL_REQ_METHOD_HTTP_POST] = function(ctx, max_size)
local body, err = request.get_body(max_size, ctx)
if not body then
return nil, "failed to read graphql data, " .. (err or "request body has zero size")
end

if request.header(ctx, "Content-Type") == GRAPHQL_REQ_MIME_JSON then
local res
res, err = json.decode(body)
if not res then
return nil, "failed to read graphql data, " .. err
end

if not res[GRAPHQL_REQ_DATA_KEY] then
return nil, "failed to read graphql data, json body[" ..
GRAPHQL_REQ_DATA_KEY .. "] is nil"
end

body = res[GRAPHQL_REQ_DATA_KEY]
end

return body
end
}


local function parse_graphql(ctx)
Expand All @@ -51,9 +97,16 @@ local function parse_graphql(ctx)
max_size = size
end

local body, err = request.get_body(max_size, ctx)
local method = request.get_method()
local func = fetch_graphql_data[method]
if not func then
return nil, "graphql not support `" .. method .. "` request"
end

local body
body, err = func(ctx, max_size)
if not body then
return nil, "failed to read graphql body: " .. (err or "request body has zero size")
return nil, err
end

local ok, res = pcall(gq_parse, body)
Expand Down
67 changes: 61 additions & 6 deletions t/router/graphql.t
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ query {
}
--- error_code: 404
--- error_log
failed to read graphql body
failed to read graphql data
Expand All @@ -244,7 +244,7 @@ failed to read graphql body
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[=[{
"methods": ["POST"],
"methods": ["POST", "GET"],
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
Expand Down Expand Up @@ -283,7 +283,62 @@ hello world
=== TEST 13: multiple root fields
=== TEST 13: test send http post json data
--- request
POST /hello
{"query":"query{owner{name}}"}
--- more_headers
Content-Type: application/json
--- response_body
hello world
=== TEST 14: test send http get query data
--- request
GET /hello?query=query{owner{name}}
--- response_body
hello world
=== TEST 15: test send http get multiple query data success
--- request
GET /hello?query=query{owner{name}}&query=query{repo{name}}
--- response_body
hello world
=== TEST 16: test send http get multiple query data failure
--- request
GET /hello?query=query{repo{name}}&query=query{owner{name}}
--- error_code: 404
=== TEST 17: no body (HTTP GET)
--- request
GET /hello
--- error_code: 404
--- error_log
failed to read graphql data, args[query] is nil
=== TEST 18: no body (HTTP POST JSON)
--- request
POST /hello
{}
--- more_headers
Content-Type: application/json
--- error_code: 404
--- error_log
failed to read graphql data, json body[query] is nil
=== TEST 19: multiple root fields
--- request
POST /hello
query {
Expand All @@ -299,7 +354,7 @@ hello world
=== TEST 14: root fields mismatch
=== TEST 20: root fields mismatch
--- request
POST /hello
query {
Expand All @@ -311,9 +366,9 @@ query {
=== TEST 15: no body
=== TEST 21: no body
--- request
POST /hello
--- error_code: 404
--- error_log
failed to read graphql body: request body has zero size
failed to read graphql data, request body has zero size

0 comments on commit 00b7b01

Please sign in to comment.