我的想法是使用react和urql构建一个客户端应用程序,用长生不老药和苦艾酒构建graphql,但目前看来,这些应用程序似乎并没有很好地结合在一起。
除了阿波罗之外,有没有其他任何客户端实际使用Absinthe订阅的方法?我尝试过使用urql,但是ws连接失败了,得到了以下错误:
WebSocket连接到‘ws://localhost:4000/套接字/ WebSocket’失败:在WebSocket握手过程中出错:发送非空的“Sec-WebSocket-Protocol”报头,但没有收到响应
到目前为止,我发现的唯一一件似乎与这个问题有关的东西是这个库--苦艾/套接字-阿波罗链接(https://github.com/absinthe-graphql/absinthe-socket/tree/master/packages/socket-apollo-link),但它显然只适用于阿波罗。
在我失败的尝试中,我做了以下几点:
import React from 'react'
import { Provider, Client, dedupExchange, fetchExchange, subscriptionExchange } from 'urql'
import { cacheExchange } from '@urql/exchange-graphcache'
import { SubscriptionClient } from 'subscriptions-transport-ws'
const DataProvider = ({ children }) => {
const cache = cacheExchange({})
const subscriptionClient = new SubscriptionClient('ws://localhost:4000/socket/websocket', {})
const client = new Client({
url: 'http://localhost:4000/api',
exchanges: [
dedupExchange,
cache,
fetchExchange,
subscriptionExchange({
forwardSubscription: operations => subscriptionClient.request(operations),
}),
],
})
return <Provider value={client}>{children}</Provider>
}
export default DataProvider这个“订阅-传输- ws”是我从一个教程中发现的,在ws url末尾的神秘的'/websocket‘在某些gh问题中被提到,但它似乎是有效的(在添加到url的末尾之前,根本没有ws连接)。
发布于 2021-06-03 22:19:38
这并不能直接回答这个问题,但如果有人最后也有同样的困惑,并作为对您在这里的评论的回应:
我从一个教程中发现了
,在ws url末尾的神秘的'/websocket‘在某些gh问题中被提到,但它似乎是有效的(在添加到url末尾之前,根本没有ws连接)。
这是因为长轮询和websocket都是可行的传输。您可能在Endpoint中使用websocket: true定义了套接字,这为您提供了该路由。运行mix phx.routes | grep socket将是一个有用的命令。
发布于 2020-05-20 00:00:59
这显然是迟来的答复,但无论如何我还是会尽力的。看起来Absinthe有一个与subscriptions-transport-ws不同的客户端库,特别是它在这里:https://github.com/absinthe-graphql/absinthe-socket/blob/master/packages/socket-apollo-link/src/createAbsintheSocketLink.js#L8
如果您与这个库集成,那么它可能会工作。因此,这意味着您可能不得不与PhoenixSocket集成,如下所示:https://github.com/absinthe-graphql/absinthe-socket/tree/master/packages/socket-apollo-link#examples
https://stackoverflow.com/questions/61323208
复制相似问题