custom-go/function/fun1.go
package function
import (
"custom-go/pkg/base"
"custom-go/pkg/plugins"
"custom-go/pkg/wgpb"
)
func init() {
// 注册 function
plugins.RegisterFunction[helloReq, helloRes](hello, wgpb.OperationType_MUTATION)
}
type helloReq struct {
Username string `json:"username"`
Password string `json:"password"`
Info helloInfo `json:"info,omitempty"`
}
type helloInfo struct {
Code string `json:"code,omitempty"`
Captcha string `json:"captcha,omitempty"`
}
type helloRes struct {
Msg string `json:"msg"`
Data string `json:"data"`
}
func hello(hook *base.HookRequest, body *base.OperationBody[helloReq, helloRes]) (*base.OperationBody[helloReq, helloRes], error) {
if body.Input.Username != "John" || body.Input.Password != "123456" {
body.Response = &base.OperationBodyResponse[helloRes]{
Errors: []base.GraphQLError{{Message: "username or password wrong"}},
}
return body, nil
}
body.Response = &base.OperationBodyResponse[helloRes]{Data: helloRes{Msg: "hello success"}}
return body, nil
}
function
├─ fun1.go # function 钩子代码
└─ fun1.json # 该钩子对应的配置文件
{
"name": "fun1",
// 请求类型,1代表POST请求
"operationType": 1,
// 请求入参的JSON SCHEMA
"variablesSchema": "{\"definitions\":{\"helloInfo\":{\"properties\":{\"captcha\":{\"type\":\"string\"},\"code\":{\"type\":\"string\"}},\"type\":\"object\"}},\"properties\":{\"info\":{\"$ref\":\"#/definitions/helloInfo\"},\"password\":{\"type\":\"string\"},\"username\":{\"type\":\"string\"}},\"required\":[\"username\",\"password\"],\"type\":\"object\"}",
// 响应结果的JSON SCHEMA
"responseSchema": "{\"properties\":{\"data\":{\"type\":\"string\"},\"msg\":{\"type\":\"string\"}},\"required\":[\"msg\",\"data\"],\"type\":\"object\"}",
"cacheConfig": null,
// 授权配置
"authenticationConfig": null,
// 实时查询配置,仅限于GET请求
"liveQueryConfig": null,
// RBAC配置
"authorizationConfig": null,
"hooksConfiguration": null,
"variablesConfiguration": null,
"engine": 0,
"path": "/function/fun1", // 路由
}
{
"report": {
// function 钩子
"functions": [
"fun1"
],
"time": "2023-09-06T17:18:21.957519+08:00"
},
// 钩子服务的状态
"status": "ok"
}
curl http://localhost:9991/operations/function/fun1
-X POST
-H 'Content-Type: application/json'
--data-raw '{"info":{"captcha":"string","code":"string"},"password":"string","username":"string"}'
--compressed
http://localhost:9991/operations/function/fun1