gqlgen.com
Â
gqlgen version: v0.17.44
Â
gqlgen å
¬å¼ã®æé ã«å¾ãAPIãä½æãã
const wsLink = new GraphQLWsLink(
 createClient({
 })
);
Â
Apollo Client ã使ã£ã¦ä¸è¨ã®ããã« WebSocket éä¿¡ã確ç«ãããã¨ãããã®ã®ã
ChatMessage.tsx:16 Â WebSocket connection to 'ws://localhost:8080/query' failed:
ã®ã¨ã©ã¼ã«ééããã¨ãã®å¯¾å¿ã§ãã
Â
ã¨ã©ã¼ã®åå
ã¨ã©ã¼ã®åå ã¯ä¸è¨ç®æã§ããã
srv := handler.NewDefaultServer
Â
golang ã®ãµã¼ãã¼å´ã§ã¯ä¸è¨ã¨ã©ã¼ãã³ã³ã½ã¼ã«åºåããã¦ãã¾ãã
unable to upgrade *http.response to websocket websocket: request origin not allowed by Upgrader.CheckOrigin:Â
Â
handler.NewDefaultServer é¢æ°ã®ä¸èº«ã®ã³ã¼ããã¿ã¦ã¿ãã¨ã
func NewDefaultServer(es graphql.ExecutableSchema) *Server {
  srv.AddTransport(transport.Websocket{
    KeepAlivePingInterval: 10 * time.Second,
  })
  srv.AddTransport(transport.Options{})
  srv.AddTransport(transport.GET{})
  srv.AddTransport(transport.POST{})
  srv.AddTransport(transport.MultipartForm{})
  srv.SetQueryCache(lru.New(1000))
  srv.Use(extension.Introspection{})
  srv.Use(extension.AutomaticPersistedQuery{
    Cache: lru.New(100),
  })
}
Â
ä¸è¨ã®ããã«ãªã£ã¦ãããtransport.Websocket ã®ç®æã§Originã¾ããã®è¨å®ããªãã§ãã
Â
ã¨ã©ã¼å¯¾å¿
handler.New é¢æ°ã§ãµã¼ãã¼ãçæãããã¨ãã WebSocket ã¾ããã®è¨å®ã追å ãããã¨ã§ WebSocket éä¿¡ã§ããããã«ãªãã¾ããã
Â
    graph.NewExecutableSchema(
      graph.Config{Resolvers: graph.NewResolver()}),
  )
  // add ws transport configured by ourselves
  srv.AddTransport(transport.Options{})
  srv.AddTransport(transport.GET{})
  srv.AddTransport(transport.POST{})
  srv.AddTransport(transport.MultipartForm{})
  srv.AddTransport(&transport.Websocket{
    Upgrader: websocket.Upgrader{
      //ReadBufferSize:  1024,
      //WriteBufferSize: 1024,
      CheckOrigin: func(r *http.Request) bool {
        // add checking origin logic to decide return true or false
        return true
      },
    },
    KeepAlivePingInterval: 10 * time.Second,
  })
Â
åãã¨ã©ã¼ã§ããã£ã¦ããæ¹ã¯ä¸è¨è©¦ãã¦ã¿ã¦ãã ããã
Â
ï¼ãã®è¨äºã¯ã以ä¸ãåèã«ããã¦ããã ãã¾ãããï¼
stackoverflow.com
Â
vallettaio.hatenablog.com
Â