-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thegrpc
class plugin specification (rest2grpc
=>grpc-transcode
) 🤔There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding,
de
meansdecode
, sodegraphql
meansdecode GraphQL
. I think this name works because it does not belong to Kong only :)Or
graphql-transcode
?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1