You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/02/18 03:09:32 UTC
[apisix] branch master updated: feat(graphql): support http get and post json request (#6343)
This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 00b7b01 feat(graphql): support http get and post json request (#6343)
00b7b01 is described below
commit 00b7b0198f167c1e1a69d0c976b4a1dd231f48ce
Author: 帅进超 <sh...@gmail.com>
AuthorDate: Fri Feb 18 11:09:26 2022 +0800
feat(graphql): support http get and post json request (#6343)
---
apisix/core/ctx.lua | 59 +++++++++++++++++++++++++++++++++++++++++++---
t/router/graphql.t | 67 ++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 117 insertions(+), 9 deletions(-)
diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua
index af8672d..fc1d3f8 100644
--- a/apisix/core/ctx.lua
+++ b/apisix/core/ctx.lua
@@ -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
@@ -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)
@@ -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)
diff --git a/t/router/graphql.t b/t/router/graphql.t
index 294e2a1..b6936fc 100644
--- a/t/router/graphql.t
+++ b/t/router/graphql.t
@@ -232,7 +232,7 @@ query {
}
--- error_code: 404
--- error_log
-failed to read graphql body
+failed to read graphql data
@@ -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
@@ -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 {
@@ -299,7 +354,7 @@ hello world
-=== TEST 14: root fields mismatch
+=== TEST 20: root fields mismatch
--- request
POST /hello
query {
@@ -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