我使用gqlgen 作為我的服務,使用apollo 用戶端和graphql-ws 作為我的前端,我正在嘗試使用訂閱,它在我的遊樂場中完美運行,但是當我嘗試將客戶端連接到它時,我收到了這個錯誤:
websocket connection to 'ws://localhost:8080/' failed:
在我的容器日誌中我收到:
unable to upgrade *http.response to websocket websocket: request origin not allowed by upgrader.checkorigin: http: superfluous response.writeheader call from github.com/99designs/gqlgen/graphql/handler/transport.senderror (error.go:15)
這是我的 golang 程式碼:
if err := godotenv.load(); err != nil { log.fatal("error loading environment variables file") } port := helpers.env("port") if port == "" { port = defaultport } router := chi.newrouter() router.use(cors.new(cors.options{ allowedorigins: strings.split(helpers.env("allowed_origins"), ","), allowcredentials: true, debug: helpers.env("debug") == "true", allowedheaders: []string{"*"}, }).handler) srv := handler.newdefaultserver(generated.newexecutableschema(generated.config{resolvers: &resolvers.resolver{}})) srv.addtransport(transport.post{}) upgrader := &transport.websocket{ upgrader: websocket.upgrader{ handshaketimeout: 1 * time.minute, checkorigin: func(r *http.request) bool { return true }, readbuffersize: 1024, writebuffersize: 1024, }, keepalivepinginterval: 10 * time.second, } srv.addtransport(upgrader) srv.use(extension.introspection{}) if helpers.env("mode") == "production" { cache, err := apq.newcache(24 * time.hour) if err != nil { log.fatalf("cannot create apq redis cache: %v", err) } srv.use(extension.automaticpersistedquery{cache: cache}) } go initworkers() go runasynqmon() router.use(getheadersmiddleware()) router.handle("/", srv) if helpers.env("mode") == "development" { router.handle("/playground", playground.handler("graphql playground", "/")) log.printf("connect to http://localhost:%s/playground for graphql playground", port) } log.fatal(http.listenandserve(":"+port, router))
這是我的客戶端程式碼:
import { setContext } from '@apollo/client/link/context' import { onError } from '@apollo/client/link/error' import { GraphQLWsLink } from '@apollo/client/link/subscriptions' import { getMainDefinition } from '@apollo/client/utilities' import { createUploadLink } from 'apollo-upload-client' import { createClient } from 'graphql-ws' import { logout } from '../helpers/logout' import { getTokenFromStorage } from '../helpers/userData' import { lang } from '../localization' const authLink = setContext((_, { headers }) => { const token = getTokenFromStorage() return { headers: { authorization: token ? `Bearer ${token}` : undefined, 'Accept-Language': lang, ...headers } } }) const httpLink = createUploadLink({ uri: process.env.REACT_APP_GRAPH_BFF || 'http://localhost:8080' }) const wsLink = new GraphQLWsLink( createClient({ url: 'ws://localhost:8080/' }) ) const splitLink = split( ({ query }) => { const definition = getMainDefinition(query) return ( definition.kind === 'OperationDefinition' && definition.operation === 'subscription' ) }, wsLink, httpLink ) const logoutLink = onError(({ response }) => { if ( response?.errors && response.errors.length > 0 && response.errors.some((errorItem) => errorItem.message.toLowerCase().includes('unauthenticated') ) ) { logout() } }) const chainList = [logoutLink, authLink, splitLink] const linkChain = from(chainList) const apolloClient = new ApolloClient({ cache: new InMemoryCache({ addTypename: false }), link: linkChain }) export default apolloClient
我以為升級程式中的 checkorigin 可以解決這個問題,但它不起作用 知道如何解決這個問題嗎?
這是一個很棒的問題。這意味著您是一位才華橫溢的程式設計師...回覆如下:
您需要更改此行:
srv := handler.newdefaultserver(generated.newexecutableschema(generated.config{resolvers: &resolvers.resolver{}}))
因為 newdefaultserver 使用相同的來源,這表示您的來源和主機必須相同,這表示您的程式碼: checkorigin: func(r *http.request) bool { 回傳真 }, 不工作。 您必須將 newdefaultserver 變更為 new,以便您的程式碼變更為它:
srv := handler.New(generated.NewExecutableSchema(generated.Config{Resolvers: &resolvers.Resolver{}}))
並且您的 websocket 工作正常。 請小心此更改,因為 newdefaultserver 內部有一些用於更新或取得或其他一些東西的配置。 (參見包裝本身) 並且您必須在程式碼中方便地編寫它。
以上是無法將 graphql-ws 連接到 gqlgen的詳細內容。更多資訊請關注PHP中文網其他相關文章!