首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为CORS问题启用gqlgen服务器?

如何为CORS问题启用gqlgen服务器?
EN

Stack Overflow用户
提问于 2020-08-17 19:10:32
回答 1查看 771关注 0票数 0

目标:

运行在5000端口上的Svelte应用程序希望从运行在8080端口上的gqlgen GraphQL服务器上查询数据,这两个服务器都运行在本地主机上。我试图查询诸如https://api.react-finland.fi/graphql这样的公共graphql,只是为了测试我的Svelte应用程序(端口:5000)是否正常工作。所以我认为问题在于我的Go graphql服务器(端口:8080)。

系统

代码语言:javascript
复制
go version
go version go1.15 linux/amd64

go 1.15

require (
    github.com/99designs/gqlgen v0.12.1
    github.com/go-chi/chi v4.1.2+incompatible
    github.com/gorilla/websocket v1.4.2
    github.com/rs/cors v1.7.0
    github.com/vektah/gqlparser/v2 v2.0.1
)

已经尝试过

根据官方网站,我已经尝试过他们的方法

这是我的密码:

代码语言:javascript
复制
func main() {
    port := os.Getenv("PORT")
    if port == "" {
        port = defaultPort
    }

    router := chi.NewRouter()

    // Add CORS middleware around every request
    // See https://github.com/rs/cors for full option listing
    router.Use(cors.New(cors.Options{
        AllowedOrigins:   []string{"http://localhost:5000", "http://localhost:8080"},
        AllowOriginFunc:  func(origin string) bool { return true },
        AllowedMethods:   []string{},
        AllowedHeaders:   []string{},
        AllowCredentials: true,
        Debug:            true,
    }).Handler)

    srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
    srv.AddTransport(&transport.Websocket{
        Upgrader: websocket.Upgrader{
            CheckOrigin: func(r *http.Request) bool {
                // Check against your desired domains here
                return r.Host == "localhost:8080"
            },
            ReadBufferSize:  1024,
            WriteBufferSize: 1024,
        },
    })

    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))
    err := http.ListenAndServe(":8080", router)
    if err != nil {
        panic(err)
    }
}

结果

我发现了那些错误:

代码语言:javascript
复制
Access to fetch at 'http://localhost:8080/' from origin 'http://localhost:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

如何解决?

我读过很多文档并搜索过.但我不知道怎么做,我也不知道如何调试才能找到解决方案。到目前为止我才学了两天。有人能帮忙吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-18 20:30:32

嗨,现在有人帮我发现了我遇到的主要问题:

1.是的,我们只需在我写到的一行中使用一条:

代码语言:javascript
复制
AllowedOrigins:   []string{"http://localhost:5000", "http://localhost:8080"},
AllowOriginFunc:  func(origin string) bool { return true },

事实上,第二个会覆盖第一个,所以只能选择一个。

2.在本部中

代码语言:javascript
复制
log.Fatal(http.ListenAndServe(":"+port, nil))
    err := http.ListenAndServe(":8080", router)
    if err != nil {
        panic(err)
    }

我已经写了两次http:ListenAndServe,所以没有写到第二个。我删除了log.Fatal(http.ListenAndServe(":"+port, nil))

3.由于我们将中间件传递给用于http请求的路由器,所以我们需要使用它而不是http.handle。所以这两句话是错的:

代码语言:javascript
复制
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)

正确的方法应该是:

代码语言:javascript
复制
router.Handle("/", playground.Handler("GraphQL playground", "/query"))
router.Handle("/query", srv)

事实上,在官方的方法中.但不知怎么的,在我尝试了几种不同的解决方案后,我在兔子洞里迷路了,没有看到那些明显的错误!>.<

经过以上的改变,现在它终于起作用了!谢谢你的帮忙!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63457207

复制
相关文章

相似问题

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