You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/12/08 03:44:25 UTC

[GitHub] [apisix] membphis commented on a change in pull request #2964: feat: route accroding to the graphql attributes

membphis commented on a change in pull request #2964:
URL: https://github.com/apache/apisix/pull/2964#discussion_r538009201



##########
File path: apisix/core/ctx.lua
##########
@@ -84,6 +158,10 @@ do
                 key = re_gsub(key, "-", "_", "jo")
                 val = get_var(key, t._request)
 
+            elseif core_str.has_prefix(key, "graphql_") then
+                key = sub_str(key, 9)

Review comment:
       magic number, need comment

##########
File path: apisix/core/ctx.lua
##########
@@ -15,22 +15,96 @@
 -- limitations under the License.
 --
 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 config_local = require("apisix.core.config_local")
 local tablepool    = require("tablepool")
 local get_var      = require("resty.ngxvar").fetch
 local get_request  = require("resty.ngxvar").request
 local ck           = require "resty.cookie"
+local gq_parse     = require("graphql").parse
 local setmetatable = setmetatable
 local sub_str      = string.sub
 local rawset       = rawset
+local ngx          = ngx
 local ngx_var      = ngx.var
 local re_gsub      = ngx.re.gsub
+local ipairs       = ipairs
 local type         = type
 local error        = error
-local ngx          = ngx
+local pcall        = pcall
 
 
 local _M = {version = 0.2}
+local GRAPHQL_DEFAULT_MAX_SIZE = 1048576               -- 1MiB
+
+
+local function parse_graphql(ctx)
+    local local_conf, err = config_local.local_conf()
+    if not local_conf then
+        return nil, "failed to get local conf: " .. err
+    end
+
+    local max_size = GRAPHQL_DEFAULT_MAX_SIZE
+    local size = core_tab.try_read_attr(local_conf, "graphql", "max_size")
+    if size then
+        max_size = size
+    end
+
+    local body, err = request.get_body(max_size, ctx)
+    if not body then
+        return nil, "failed to read graphql body: " .. err
+    end
+
+    local ok, res = pcall(gq_parse, body)
+    if not ok then
+        return nil, "failed to parse graphql: " .. res .. " body: " .. body
+    end
+
+    if #res.definitions == 0 then
+        return nil, "empty graphql: " .. body
+    end
+
+    return res, nil

Review comment:
       `return  res` be fine

##########
File path: t/lib/test_admin.lua
##########
@@ -108,14 +108,15 @@ function _M.comp_tab(left_tab, right_tab)
     local err
     dir_names = {}
 
+    local _
     if type(left_tab) == "string" then
-        left_tab, err = json.decode(left_tab)
+        left_tab, _, err = json.decode(left_tab)

Review comment:
       where to get this API manual?
   
   the new way seems wrong. I think the correct style should be `local res, err = json.decode(str)`

##########
File path: apisix/core/ctx.lua
##########
@@ -15,22 +15,96 @@
 -- limitations under the License.
 --
 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 config_local = require("apisix.core.config_local")
 local tablepool    = require("tablepool")
 local get_var      = require("resty.ngxvar").fetch
 local get_request  = require("resty.ngxvar").request
 local ck           = require "resty.cookie"
+local gq_parse     = require("graphql").parse
 local setmetatable = setmetatable
 local sub_str      = string.sub
 local rawset       = rawset
+local ngx          = ngx
 local ngx_var      = ngx.var
 local re_gsub      = ngx.re.gsub
+local ipairs       = ipairs
 local type         = type
 local error        = error
-local ngx          = ngx
+local pcall        = pcall
 
 
 local _M = {version = 0.2}
+local GRAPHQL_DEFAULT_MAX_SIZE = 1048576               -- 1MiB
+
+
+local function parse_graphql(ctx)
+    local local_conf, err = config_local.local_conf()
+    if not local_conf then
+        return nil, "failed to get local conf: " .. err
+    end
+
+    local max_size = GRAPHQL_DEFAULT_MAX_SIZE
+    local size = core_tab.try_read_attr(local_conf, "graphql", "max_size")
+    if size then
+        max_size = size
+    end
+
+    local body, err = request.get_body(max_size, ctx)
+    if not body then
+        return nil, "failed to read graphql body: " .. err
+    end
+
+    local ok, res = pcall(gq_parse, body)
+    if not ok then
+        return nil, "failed to parse graphql: " .. res .. " body: " .. body
+    end
+
+    if #res.definitions == 0 then
+        return nil, "empty graphql: " .. body
+    end
+
+    return res, nil
+end
+
+
+local function get_parsed_graphql(ctx)
+    if not ctx._graphql then
+        local res, err = parse_graphql(ctx)
+        if not res then
+            log.error(err)
+            ctx._graphql = {}
+

Review comment:
       return directly




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org