首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用Gin的Graphql订阅

不使用Gin的Graphql订阅
EN

Stack Overflow用户
提问于 2021-10-04 08:09:16
回答 2查看 645关注 0票数 2

当我尝试用GraphQL安装Go web服务器时,我使用了作为模板。它基本上是杜松子酒99designs/gqlgen的组合。

当我基于net/http包创建一个基本的gqlgen服务器时,GraphQL订阅的声明就像预期的那样工作。

代码语言:javascript
复制
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))
}

但当我加入杜松子酒时,就像这样:

代码语言:javascript
复制
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订阅一起工作?

EN

回答 2

Stack Overflow用户

发布于 2021-10-23 14:02:48

使用Gin将Could not connect to websocket endpoint..更改为r.Any("/query", graphqlHandler())修复错误r.POST("/query", graphqlHandler())

票数 0
EN

Stack Overflow用户

发布于 2022-07-14 16:15:41

请把Gin和Graphql结合起来,有可能有两条不同的路线吗?一个用于查询/突变,另一个用于订阅。如下所示:

代码语言:javascript
复制
func main() {
    // Setting up Gin
    r := gin.Default()
    r.POST("/query", graphqlHandler())
    r.POST("/subscription", graphqlHandler())
    r.GET("/", playgroundHandler())
    r.Run()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69432800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档