当我尝试用GraphQL安装Go web服务器时,我使用了这作为模板。它基本上是杜松子酒和99designs/gqlgen的组合。
当我基于net/http包创建一个基本的gqlgen服务器时,GraphQL订阅的声明就像预期的那样工作。
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/jawil003/gqlgen/graph"
"github.com/jawil003/gqlgen/graph/generated"
)
const defaultPort = "8080"
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}但当我加入杜松子酒时,就像这样:
package main
import (
"github.com/gin-gonic/gin"
"github.com/jawil003/gqlgen-todos/graph"
"github.com/jawil003/gqlgen-todos/graph/generated"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
)
// Defining the Graphql handler
func graphqlHandler() gin.HandlerFunc {
// NewExecutableSchema and Config are in the generated.go file
// Resolver is in the resolver.go file
h := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
// Defining the Playground handler
func playgroundHandler() gin.HandlerFunc {
h := playground.Handler("GraphQL", "/query")
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func main() {
// Setting up Gin
r := gin.Default()
r.POST("/query", graphqlHandler())
r.GET("/", playgroundHandler())
r.Run()
}我明白这个问题:
{“错误”:“无法连接到websocket端点ws://localhost:8080/query。请检查端点url是否正确。”}
是否有任何已知的解决方案使gin与graphql订阅一起工作?
发布于 2021-10-23 14:02:48
使用Gin将Could not connect to websocket endpoint..更改为r.Any("/query", graphqlHandler())修复错误r.POST("/query", graphqlHandler())
发布于 2022-07-14 16:15:41
请把Gin和Graphql结合起来,有可能有两条不同的路线吗?一个用于查询/突变,另一个用于订阅。如下所示:
func main() {
// Setting up Gin
r := gin.Default()
r.POST("/query", graphqlHandler())
r.POST("/subscription", graphqlHandler())
r.GET("/", playgroundHandler())
r.Run()
}https://stackoverflow.com/questions/69432800
复制相似问题