Skip to content

Commit

Permalink
Merge pull request #816 from nii236/patch-1
Browse files Browse the repository at this point in the history
Update cors.md to allow CORS for websockets
  • Loading branch information
vektah authored Aug 7, 2019
2 parents 410d832 + 297e09c commit 8f0d9b4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions docs/content/recipes/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Setting CORS headers using rs/cors for gqlgen"
description: Use the best of breed rs/cors library to set CORS headers when working with gqlgen
linkTitle: CORS
menu: { main: { parent: 'recipes' } }
menu: { main: { parent: "recipes" } }
---

Cross-Origin Resource Sharing (CORS) headers are required when your graphql server lives on a different domain to the one your client code is served. You can read more about CORS in the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).
Expand Down Expand Up @@ -34,9 +34,18 @@ func main() {
Debug: true,
}).Handler)

upgrader := websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// Check against your desired domains here
return r.Host == "example.org"
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}

router.Handle("/", handler.Playground("Starwars", "/query"))
router.Handle("/query",
handler.GraphQL(starwars.NewExecutableSchema(starwars.NewResolver())),
handler.GraphQL(starwars.NewExecutableSchema(starwars.NewResolver()), handler.WebsocketUpgrader(upgrader)),
)

err := http.ListenAndServe(":8080", router)
Expand Down

0 comments on commit 8f0d9b4

Please sign in to comment.