github.com/bowd/gqlgen@v0.7.2/docs/content/recipes/cors.md (about)

     1  ---
     2  title: "Setting CORS headers using rs/cors for gqlgen"
     3  description: Use the best of breed rs/cors library to set CORS headers when working with gqlgen
     4  linkTitle: CORS
     5  menu: { main: { parent: 'recipes' } }
     6  ---
     7  
     8  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).
     9  
    10  ## rs/cors
    11  
    12  gqlgen doesn't include a CORS implementation, but it is built to work with all standard http middleware. Here we are going to use the fantastic `chi` and `rs/cors` to build our server.
    13  
    14  ```go
    15  package main
    16  
    17  import (
    18  	"net/http"
    19  
    20  	"github.com/99designs/gqlgen/example/starwars"
    21  	"github.com/99designs/gqlgen/handler"
    22  	"github.com/go-chi/chi"
    23  	"github.com/rs/cors"
    24  )
    25  
    26  func main() {
    27  	router := chi.NewRouter()
    28  
    29  	// Add CORS middleware around every request
    30  	// See https://github.com/rs/cors for full option listing
    31  	router.Use(cors.New(cors.Options{
    32  		AllowedOrigins:   []string{"http://localhost:8080"},
    33  		AllowCredentials: true,
    34  		Debug:            true,
    35  	}).Handler)
    36  
    37  	router.Handle("/", handler.Playground("Starwars", "/query"))
    38  	router.Handle("/query",
    39  		handler.GraphQL(starwars.NewExecutableSchema(starwars.NewResolver())),
    40  	)
    41  
    42  	err := http.ListenAndServe(":8080", router)
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  }
    47  
    48  ```