使用 GraphJin 的步骤
安装 GraphJin:
go get github.com/dosco/graphjin
配置 GraphJin: 创建一个
config.yaml
文件,配置数据库连接等信息。config.yaml
# config.yaml
app_name: "test graphql"
host_port: 0.0.0.0:18081
port: 18081
web_ui: true
database:
type: postgres
host: localhost
port: 5432
dbname: graphjin_development
user: postgres
password: postgres
queries:
path: "./queries"
allow_sql: true
allow_list:
queries:
- GetReports
- GetProjects
- GetUser
# Log levels: debug, error, warn, info
log_level: "debug"
# Log formats: json, plain
log_format: "json"
启动 GraphJin:GraphQL Editor http://localhost:18081/?endpoint=/api/v1/graphql
graphjin serve
运行 GraphQL 查询: 使用 Postman 或其他 API 客户端向
http://localhost:8080/v1/graphql
发送上述 GraphQL 查询。代码启动GraphJin - GraphQL API for Rails http://localhost:18081/?endpoint=/api/v1/graphql
package main
import (
"log"
"github.com/dosco/graphjin/serv"
)
func main() {
// 配置文件路径
confPath := "./config.yaml"
// 加载配置
conf, err := serv.ReadInConfig(confPath)
if err != nil {
log.Fatal(err)
}
// 创建 GraphJin 服务
gj, err := serv.NewGraphJinService(conf)
if err != nil {
log.Fatal(err)
}
// 启动服务
if err := gj.Start(); err != nil {
log.Fatalf("Failed to start GraphJin service: %v", err)
}
}