Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphql): support http get and post json request #6343

Merged
merged 4 commits into from
Feb 18, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 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,48 @@ 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]
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
if not body then
return nil, "failed to read graphql data, args[ " ..
GRAPHQL_REQ_DATA_KEY .. "] is nil"
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(nil, "Content-Type") == GRAPHQL_REQ_MIME_JSON then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pass ctx here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, I update it later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

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 +93,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