GraphQL订阅在Gin中无法工作。

huangapple go评论100阅读模式
英文:

Graphql Subscriptions not working with Gin

问题

当我尝试使用GraphQL设置Go Web服务器时,我使用了这个作为模板。它基本上是gin99designs/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))
}

但是当我像这样添加gin时:

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"
)

// 定义GraphQL处理程序
func graphqlHandler() gin.HandlerFunc {
	// NewExecutableSchema和Config在generated.go文件中
	// Resolver在resolver.go文件中
	h := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))

	return func(c *gin.Context) {
		h.ServeHTTP(c.Writer, c.Request)
	}
}

// 定义Playground处理程序
func playgroundHandler() gin.HandlerFunc {
	h := playground.Handler("GraphQL", "/query")

	return func(c *gin.Context) {
		h.ServeHTTP(c.Writer, c.Request)
	}
}

func main() {
	// 设置Gin
	r := gin.Default()
	r.POST("/query", graphqlHandler())
	r.GET("/", playgroundHandler())
	r.Run()
}

我遇到了这个问题:

{ "error": "Could not connect to websocket endpoint ws://localhost:8080/query. Please check if the endpoint url is correct." }

是否有任何已知的解决方案可以使gin与GraphQL订阅一起工作?

英文:

When I tried to setup a Go web server with GraphQL I used this as template. It is basically a combo of gin and 99designs/gqlgen.

When I create a basic gqlgen server based on net/http package, the declaration of GraphQL subscriptions work as expected.

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))
}

But when I add gin, like this:

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()
}

I get this issue:

> { "error": "Could not connect to websocket endpoint ws://localhost:8080/query. Please check if the endpoint url is correct." }

Is there any known solution to make gin work with graphql subscriptions?

答案1

得分: 1

你好,要修复错误Could not connect to websocket endpoint..,请将Gin中的代码r.POST("/query", graphqlHandler())更改为r.Any("/query", graphqlHandler())

英文:

Hello to fix error Could not connect to websocket endpoint.. with Gin change r.POST("/query", graphqlHandler()) to r.Any("/query", graphqlHandler())

huangapple
  • 本文由 发表于 2021年10月4日 16:09:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/69432800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定