Skip to content

Commit

Permalink
feat: add degraphql plugin (#8959)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander authored Mar 1, 2023
1 parent 789e122 commit ab64aed
Show file tree
Hide file tree
Showing 9 changed files with 920 additions and 2 deletions.
160 changes: 160 additions & 0 deletions apisix/plugins/degraphql.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local core = require("apisix.core")
local gq_parse = require("graphql").parse
local req_set_body_data = ngx.req.set_body_data
local ipairs = ipairs
local pcall = pcall
local type = type


local schema = {
type = "object",
properties = {
query = {
type = "string",
minLength = 1,
maxLength = 1024,
},
variables = {
type = "array",
items = {
type = "string"
},
minItems = 1,
},
operation_name = {
type = "string",
minLength = 1,
maxLength = 1024
},
},
required = {"query"},
}

local plugin_name = "degraphql"

local _M = {
version = 0.1,
priority = 509,
name = plugin_name,
schema = schema,
}


function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end

local ok, res = pcall(gq_parse, conf.query)
if not ok then
return false, "failed to parse query: " .. res
end

if #res.definitions > 1 and not conf.operation_name then
return false, "operation_name is required if multiple operations are present in the query"
end
return true
end


local function fetch_post_variables(conf)
local req_body, err = core.request.get_body()
if err ~= nil then
core.log.error("failed to get request body: ", err)
return nil, 503
end

if not req_body then
core.log.error("missing request body")
return nil, 400
end

-- JSON as the default content type
req_body, err = core.json.decode(req_body)
if type(req_body) ~= "table" then
core.log.error("invalid request body can't be decoded: ", err or "bad type")
return nil, 400
end

local variables = {}
for _, v in ipairs(conf.variables) do
variables[v] = req_body[v]
end

return variables
end


local function fetch_get_variables(conf)
local args = core.request.get_uri_args()
local variables = {}
for _, v in ipairs(conf.variables) do
variables[v] = args[v]
end

return variables
end


function _M.access(conf, ctx)
local meth = core.request.get_method()
if meth ~= "POST" and meth ~= "GET" then
return 405
end

local new_body = core.table.new(0, 3)

if conf.variables then
local variables, code
if meth == "POST" then
variables, code = fetch_post_variables(conf)
else
variables, code = fetch_get_variables(conf)
end

if not variables then
return code
end

if meth == "POST" then
new_body["variables"] = variables
else
new_body["variables"] = core.json.encode(variables)
end
end

new_body["operationName"] = conf.operation_name
new_body["query"] = conf.query

if meth == "POST" then
if not conf.variables then
-- the set_body_data requires to read the body first
core.request.get_body()
end

core.request.set_header(ctx, "Content-Type", "application/json")
req_set_body_data(core.json.encode(new_body))
else
core.request.set_uri_args(ctx, new_body)
end
end


return _M
6 changes: 6 additions & 0 deletions ci/pod/docker-compose.plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ services:
ports:
- '7002:6379'

graphql-demo:
# the owner doesn't provide a semver tag
image: npalm/graphql-java-demo:latest
ports:
- '8888:8080'

networks:
apisix_net:
kafka_net:
Expand Down
2 changes: 1 addition & 1 deletion ci/pod/openfunction/function-example/test-uri/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/net v0.0.0-20220621193019-9d032be2e588 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.8 // indirect
google.golang.org/genproto v0.0.0-20220622171453-ea41d75dfa0f // indirect
Expand Down
1 change: 0 additions & 1 deletion ci/pod/openfunction/function-example/test-uri/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,6 @@ golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220325170049-de3da57026de/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220621193019-9d032be2e588 h1:9ubFuySsnAJYGyJrZ3koiEv8FyqofCBdz3G9Mbf2YFc=
golang.org/x/net v0.0.0-20220621193019-9d032be2e588/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand Down
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ plugins: # plugin list (sorted by priority)
- traffic-split # priority: 966
- redirect # priority: 900
- response-rewrite # priority: 899
- degraphql # priority: 509
- kafka-proxy # priority: 508
#- dubbo-proxy # priority: 507
- grpc-transcode # priority: 506
Expand Down
1 change: 1 addition & 0 deletions docs/en/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"plugins/grpc-web",
"plugins/fault-injection",
"plugins/mocking",
"plugins/degraphql",
"plugins/body-transformer"
]
},
Expand Down
Loading

0 comments on commit ab64aed

Please sign in to comment.