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: add degraphql plugin #8959

Merged
merged 2 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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"
Copy link
Contributor

Choose a reason for hiding this comment

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

I have a small question, should we call it graphql-transcode according to the grpc class plugin specification (rest2grpc => grpc-transcode) 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

We borrow the name degraphql from Kong. What do you think about it, @juzhiyuan?

Copy link
Member

@juzhiyuan juzhiyuan Mar 1, 2023

Choose a reason for hiding this comment

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

From my understanding, de means decode, so degraphql means decode GraphQL. I think this name works because it does not belong to Kong only :)

Or graphql-transcode?

Copy link
Member Author

Choose a reason for hiding this comment

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

IMHO, it would be better to keep the original name, so we don't need to modify this PR and other things which are not public.

Copy link
Member

Choose a reason for hiding this comment

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

+1


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