Add Plugin de-graphql (#303)

This commit is contained in:
Jun
2023-04-27 18:45:19 +08:00
committed by GitHub
parent e2b4a52c9e
commit 311d5c21c2
9 changed files with 971 additions and 0 deletions

View File

@@ -0,0 +1,199 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// Licensed 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.
package config
import (
"errors"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"net/url"
"regexp"
"strings"
)
const (
DefaultEndpoint string = "/graphql"
DefaultConnectionTimeout uint32 = 5000
)
var gqlVariableRegex = regexp.MustCompile(`\$(\w+)\s*:\s*(String|Float|Int|Boolean)(!?)`)
type VariableType string
const (
StringType VariableType = "String"
IntType VariableType = "Int"
FloatType VariableType = "Float"
BooleanType VariableType = "Boolean"
)
type Variable struct {
name string
typ VariableType
blank bool
value string
}
type DeGraphQLConfig struct {
client wrapper.HttpClient
gql string
endpoint string
timeout uint32
variables []Variable
}
func (d *DeGraphQLConfig) SetEndpoint(endpoint string) error {
endpoint = strings.TrimSpace(endpoint)
if endpoint == "" {
d.endpoint = DefaultEndpoint
} else {
d.endpoint = endpoint
}
return nil
}
func (d *DeGraphQLConfig) GetEndpoint() string {
return d.endpoint
}
func (d *DeGraphQLConfig) GetTimeout() uint32 {
return d.timeout
}
func (d *DeGraphQLConfig) SetTimeout(timeout uint32) {
if timeout <= 0 {
d.timeout = DefaultConnectionTimeout
} else {
d.timeout = timeout
}
}
func (d *DeGraphQLConfig) SetClient(client wrapper.HttpClient) {
d.client = client
}
func (d *DeGraphQLConfig) GetClient() wrapper.HttpClient {
return d.client
}
func (d *DeGraphQLConfig) SetGql(gql string) error {
if strings.TrimSpace(gql) == "" {
return errors.New("gql can't be empty")
}
d.gql = gql
d.variables = make([]Variable, 0)
matches := gqlVariableRegex.FindAllStringSubmatch(d.gql, -1)
if len(matches) > 0 {
for _, subMatch := range matches {
variable := Variable{}
variable.name = subMatch[1]
switch subMatch[2] {
case "String":
variable.typ = StringType
case "Float":
variable.typ = FloatType
case "Int":
variable.typ = IntType
case "Boolean":
variable.typ = BooleanType
}
variable.blank = subMatch[3] != "!"
d.variables = append(d.variables, variable)
}
}
return nil
}
func (d *DeGraphQLConfig) GetGql() string {
return d.gql
}
func (d *DeGraphQLConfig) GetVersion() string {
return "1.0.0"
}
func (d *DeGraphQLConfig) ParseGqlFromUrl(requestUrl string) (string, error) {
if strings.TrimSpace(requestUrl) == "" {
return "", errors.New("request url can't be empty")
}
url, _ := url.Parse(requestUrl)
queryValues := url.Query()
values := make(map[string]string, len(queryValues))
for k, v := range queryValues {
var v1 string
if len(v) > 1 {
v1 = strings.Join(v, ",")
} else {
v1 = v[0]
}
values[k] = v1
}
variables := make([]Variable, 0, len(d.variables))
for _, variable := range d.variables {
val, ok := values[variable.name]
// TODO validate variable type and blank
if ok {
variables = append(variables, Variable{
name: variable.name,
typ: variable.typ,
blank: variable.blank,
value: val,
})
}
}
var build strings.Builder
// write query
build.WriteString("{\"query\":")
build.WriteString("\"")
build.WriteString(getJsonStr(d.gql))
build.WriteString("\"")
// write varialbes
if len(variables) > 0 {
index := 0
build.WriteString(",")
build.WriteString("\"variables\":{")
for _, variable := range variables {
build.WriteString("\"")
build.WriteString(variable.name)
build.WriteString("\":")
if variable.typ == StringType {
build.WriteString("\"")
build.WriteString(getJsonStr(variable.value))
build.WriteString("\"")
} else {
build.WriteString(variable.value)
}
if index < len(variables)-1 {
build.WriteString(",")
}
index++
}
build.WriteString("}")
}
build.WriteString("}")
return build.String(), nil
}
func getJsonStr(str string) string {
d := strings.ReplaceAll(str, "\"", "\\\"")
return strings.ReplaceAll(d, "\n", "\\n")
}

View File

@@ -0,0 +1,177 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// Licensed 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.
package config
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
)
func TestDeGraphQLConfig_SetGql(t *testing.T) {
tests := []struct {
name string
gql string
wantVariables []Variable
wantErr error
}{
{
name: "empty gql",
gql: "",
wantErr: errors.New("gql can't be empty"),
},
{
name: "no params",
gql: "query",
wantVariables: []Variable{},
wantErr: nil,
},
{
name: "four params",
gql: "query ($owner:String $num:Float! $int : Int! $boolean : Boolean )",
wantErr: nil,
wantVariables: []Variable{
{
name: "owner",
typ: StringType,
blank: true,
},
{
name: "num",
typ: FloatType,
blank: false,
},
{
name: "int",
typ: IntType,
blank: false,
},
{
name: "boolean",
typ: BooleanType,
blank: true,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &DeGraphQLConfig{}
err := d.SetGql(tt.gql)
assert.Equal(t, tt.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tt.wantVariables, d.variables)
})
}
}
func TestDeGraphQLConfig_ParseGqlFromUrl(t *testing.T) {
tests := []struct {
name string
gql string
url string
want string
wantErr error
}{
{
name: "empty url",
gql: "query ($owner:String! $name:String!)",
url: "",
want: "",
wantErr: errors.New("request url can't be empty"),
},
{
name: "no params",
gql: "query HeroNameQuery {\n hero {\n name\n }\n}",
url: "/api?owner=a",
want: "{\"query\":\"query HeroNameQuery {\\n hero {\\n name\\n }\\n}\"}",
wantErr: nil,
},
{
name: "one string variable",
gql: "query FetchSomeIDQuery($someId: String!) {\n human(id: $someId) {\n name\n }\n}",
url: "/api?someId=a",
want: "{\"query\":\"query FetchSomeIDQuery($someId: String!) {\\n human(id: $someId) {\\n name\\n }\\n}\",\"variables\":{\"someId\":\"a\"}}",
wantErr: nil,
},
{
name: "multi variables",
gql: "query FetchSomeIDQuery($someId: String! $num: Int $price: Float! $need:Boolean!) {\n human(id: $someId) {\n name\n }\n}",
url: "/api?someId=a&num=10&price=12.0&need=false&hee=1",
want: "{\"query\":\"query FetchSomeIDQuery($someId: String! $num: Int $price: Float! $need:Boolean!) {\\n human(id: $someId) {\\n name\\n }\\n}\",\"variables\":{\"someId\":\"a\",\"num\":10,\"price\":12.0,\"need\":false}}",
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &DeGraphQLConfig{}
d.SetGql(tt.gql)
body, err := d.ParseGqlFromUrl(tt.url)
assert.Equal(t, tt.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tt.want, body)
})
}
}
func TestDeGraphQLConfig_SetEndpoint(t *testing.T) {
tests := []struct {
name string
endPoint string
wantErr error
want string
}{
{
name: "empty endpoint",
endPoint: "",
wantErr: nil,
want: "/graphql",
},
{
name: "empty endpoint with blank",
endPoint: " ",
wantErr: nil,
want: "/graphql",
},
{
name: "with value",
endPoint: " /graphql2 ",
wantErr: nil,
want: "/graphql2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &DeGraphQLConfig{}
err := d.SetEndpoint(tt.endPoint)
assert.Equal(t, tt.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tt.want, d.endpoint)
})
}
}