Sorry, there was a problem running the beginner example and I don't know how to solve it
duolabmeng6 opened this issue · comments
package main
import (
"context"
"database/sql"
"log"
"net/http"
"github.com/dosco/graphjin/core/v3"
"github.com/go-chi/chi/v5"
_ "github.com/jackc/pgx/v5/stdlib"
)
func main() {
// 连接到数据库
db, err := sql.Open("pgx", "postgres://postgres:@localhost:5432/example_db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// 创建 GraphJin 实例
gj, err := core.NewGraphJin(nil, db)
if err != nil {
log.Fatal(err)
}
// 定义 GraphQL 查询
query := `
query getPosts {
posts {
id
title
}
posts_cursor
}`
// 创建路由
r := chi.NewRouter()
// 定义路由处理程序
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
c := context.WithValue(r.Context(), core.UserIDKey, 1)
res, err := gj.GraphQL(c, query, nil, nil)
if err != nil {
log.Println(err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Write(res.Data)
})
// 启动服务器
log.Println("Starting server on port 3000")
err = http.ListenAndServe(":3000", r)
if err != nil {
log.Fatal(err)
}
}
Where the problem occurred
gj, err := core.NewGraphJin(nil, db)
Thrown an exception and I don't know how to solve it
whats the exception can you paste it here?
Throw a pointer error my go verstion 1.20
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0xd8 pc=0x13b73d8]
goroutine 1 [running]:
github.com/dosco/graphjin/core/v3.getFS(0x10fc5a6?)
/Users//go/pkg/mod/github.com/dosco/graphjin/core/v3@v3.0.0-20230512210738-36d89904d6e1/api.go:542 +0x18
github.com/dosco/graphjin/core/v3.NewGraphJin(0x16a50de?, 0x3?, {0x0, 0x0, 0x0})
/Users//go/pkg/mod/github.com/dosco/graphjin/core/v3@v3.0.0-20230512210738-36d89904d6e1/api.go:91 +0x3f
main.main()
/Users//Desktop/goproject/graphjintest/main.go:20 +0x7d
package main
import (
"context"
"database/sql"
"github.com/dosco/graphjin/core/v3"
"github.com/go-chi/chi/v5"
_ "github.com/jackc/pgx/v5/stdlib"
"log"
"net/http"
)
func main() {
// 连接到数据库
db, err := sql.Open("pgx", "postgres://postgres:@localhost:5432/mydb")
if err != nil {
panic(err)
}
defer db.Close()
// 创建 GraphJin 实例
gj, err := core.NewGraphJin(&core.Config{
SecretKey: "",
DisableAllowList: false,
EnableSchema: false,
EnableIntrospection: false,
SetUserID: false,
DefaultBlock: false,
Vars: nil,
HeaderVars: nil,
Blocklist: nil,
Resolvers: nil,
Tables: nil,
RolesQuery: "",
Roles: nil,
DBType: "",
Debug: false,
SubsPollDuration: 0,
DefaultLimit: 0,
DisableAgg: false,
DisableFuncs: false,
EnableCamelcase: false,
Production: false,
DBSchemaPollDuration: 0,
DisableProdSecurity: false,
FS: nil,
}, db)
if err != nil {
log.Fatal(err)
}
// 定义 GraphQL 查询
query := `
query {
articles() {
id
title
content
}
}`
// 创建路由
r := chi.NewRouter()
// 定义路由处理程序
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
c := context.WithValue(r.Context(), core.UserIDKey, 1)
res, err := gj.GraphQL(c, query, nil, nil)
if err != nil {
log.Println(err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Write(res.Data)
})
// 启动服务器
log.Println("Starting server on port 3000")
err = http.ListenAndServe(":3000", r)
if err != nil {
log.Fatal(err)
}
}
I found a solution
gj, err := core.NewGraphJin(&core.Config{
SecretKey: "",
DisableAllowList: false,
EnableSchema: false,
EnableIntrospection: false,
SetUserID: false,
DefaultBlock: false,
Vars: nil,
HeaderVars: nil,
Blocklist: nil,
Resolvers: nil,
Tables: nil,
RolesQuery: "",
Roles: nil,
DBType: "",
Debug: false,
SubsPollDuration: 0,
DefaultLimit: 0,
DisableAgg: false,
DisableFuncs: false,
EnableCamelcase: false,
Production: false,
DBSchemaPollDuration: 0,
DisableProdSecurity: false,
FS: nil,
}, db)
The reason is because of your introductory example
gj, err := core.NewGraphJin(nil, db)
If only the beginner were more friendly thank you