このサンプルで CORS 対応した server.go ファイルです https://github.com/99designs/gqlgen/tree/master/example/todo
↓の公式ドキュメントが古いので(2020-03-17時点)動きませんでした https://gqlgen.com/recipes/cors/
実装は至ってシンプルで、chi の Router に cors のミドルウェアを Use させてるだけです。
このサンプルで CORS 対応した server.go ファイルです https://github.com/99designs/gqlgen/tree/master/example/todo
↓の公式ドキュメントが古いので(2020-03-17時点)動きませんでした https://gqlgen.com/recipes/cors/
実装は至ってシンプルで、chi の Router に cors のミドルウェアを Use させてるだけです。
| package main | |
| import ( | |
| "net/http" | |
| "github.com/99designs/gqlgen/example/todo" | |
| "github.com/99designs/gqlgen/graphql/handler" | |
| "github.com/99designs/gqlgen/graphql/playground" | |
| "github.com/go-chi/chi" | |
| "github.com/rs/cors" | |
| ) | |
| func main() { | |
| router := chi.NewRouter() | |
| router.Use(cors.New(cors.Options{ | |
| AllowedOrigins: []string{ "http://localhost:3000" }, | |
| AllowCredentials: true, | |
| Debug: true, | |
| }).Handler) | |
| srv := handler.NewDefaultServer(todo.NewExecutableSchema(todo.New())) | |
| router.Handle("/", playground.Handler("Todo", "/query")) | |
| router.Handle("/query", srv) | |
| err := http.ListenAndServe(":8081", router) | |
| if err != nil { | |
| panic(err) | |
| } | |
| } |