Skip to content

Instantly share code, notes, and snippets.

@gjreasoner
Created January 16, 2022 12:46
Show Gist options
  • Save gjreasoner/bcbbed0347b936f452f82e1774bc19e2 to your computer and use it in GitHub Desktop.
Save gjreasoner/bcbbed0347b936f452f82e1774bc19e2 to your computer and use it in GitHub Desktop.
gqlgen cors with Mux
Index: server.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/server.go b/server.go
--- a/server.go
+++ b/server.go (date 1642337159905)
@@ -1,16 +1,15 @@
package main
-
-
import (
- "log"
- "net/http"
- "os"
-
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
+ "github.com/gorilla/mux"
+ "github.com/rs/cors"
+ "log"
+ "net/http"
+ "os"
)
const defaultPort = "8080"
@@ -23,9 +22,17 @@
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
- http.Handle("/", playground.Handler("GraphQL playground", "/query"))
- http.Handle("/query", srv)
+ router := mux.NewRouter()
+
+ router.Handle("/", playground.Handler("GraphQL playground", "/query"))
+ router.Handle("/query", srv)
+ c := cors.New(cors.Options{
+ AllowedOrigins: []string{"http://localhost:19006"},
+ AllowCredentials: true,
+ })
+
+ srvHandler := c.Handler(router)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
- log.Fatal(http.ListenAndServe(":"+port, nil))
+ log.Fatal(http.ListenAndServe(":"+port, srvHandler))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment