标签: apollo-link

基于上下文的多个 ApolloLink

我想实现一种根据 graphql 查询中设置的上下文切换不同链接的方法。到目前为止我所做的就是这样的事情,它运行良好,但随着时间的推移似乎并不是一个很好的解决方案。

const link = ApolloLink.from([
  HandlerLink1,
  HandlerLink2,
  ApolloLink.split(
    operation => operation.getContext().service === "x",
    LinkX,
    ApolloLink.split(
      operation => operation.getContext().service === "y",
      LinkY,
      ApolloLink.split(
        operation => operation.getContext().service === "z",
        LinkZ,
        LinkN
      )
    )
  )
]);
Run Code Online (Sandbox Code Playgroud)

除了嵌套之外,还有其他更好的方法吗?

react-apollo apollo-client apollo-link

7
推荐指数
1
解决办法
4188
查看次数

在使用Apollo和React捕获身份验证失败后如何正确重定向

我正在编写一个使用的React应用,apollo-client并且正在apollo-link-error全局捕获身份验证错误。我正在使用createBrowserHistory浏览器历史记录操纵和redux管理我的应用状态。

出现身份验证错误时,我想将用户重定向到/login页面。但是,使用history.push('/login')and forceRefresh:false可以更改URL,但实际上不会在我的应用程序内部导航。

如果我使用forceRefresh:true它可以正常工作,但该应用程序已完全重启,我想避免这种情况。

const errorLink = onError(({ graphQLErrors, networkError }) => {
    if(graphQLErrors[0].extensions.code == "UNAUTHENTICATED") {
        // with forceRefresh:true this works, but causes a nasty 
        // reload, without the app doesn't navigate, only the url changes 
        history.push('/login')
    }
});
Run Code Online (Sandbox Code Playgroud)

`

let links = [errorLink, authLink, httpLink];    
const link = ApolloLink.from(links);

    const client = new ApolloClient({
        link: link,
        cache: new InMemoryCache(),
        connectToDevTools: true,
    });
Run Code Online (Sandbox Code Playgroud)

我认为问题是我没有使用redux-router …

reactjs react-apollo apollo-link

5
推荐指数
1
解决办法
228
查看次数

处理自定义 apollo graphql 后端的 firebase 初始化延迟和 id 令牌

目前,当我使用 firebase 对用户进行身份验证时,我将他们的身份验证令牌存储在localStorage稍后用于连接到我的后端,如下所示:

const httpLink = new HttpLink({uri: 'http://localhost:9000/graphql'})

const authMiddleware = new ApolloLink((operation, forward) => {
  // add the authorization token to the headers
  const token = localStorage.getItem(AUTH_TOKEN) || null
  operation.setContext({
    headers: {
      authorization: token ? `Bearer ${token}` : ''
    }
  })
  return forward(operation)
})

const authAfterware = onError(({networkError}) => {
  if (networkError.statusCode === 401) AuthService.signout()
})

function createApolloClient() {
  return new ApolloClient({
    cache: new InMemoryCache(),
    link: authMiddleware.concat(authAfterware).concat(httpLink)
  })
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,一旦令牌过期,我就无法刷新它。所以我尝试使用以下内容为 apollo 设置授权令牌:

const httpLink = new …
Run Code Online (Sandbox Code Playgroud)

firebase apollo reactjs firebase-authentication apollo-link

5
推荐指数
1
解决办法
380
查看次数

5
推荐指数
1
解决办法
2238
查看次数

`不能使用来自另一个模块或领域的“__Schema”。` 和使用 ApolloClient 的`复制“graphql”模块`

我有一个带有 ApolloClient 和 Apollo-Link-Schema 的 React 应用程序。该应用程序在本地运行良好,但在我们的临时环境(使用 GOCD)中,我们收到以下错误:

Uncaught Error: Cannot use e "__Schema" from another module or realm.

Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.

https://yarnpkg.com/en/docs/selective-version-resolutions

Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used …
Run Code Online (Sandbox Code Playgroud)

graphql react-apollo apollo-client graphql-tools apollo-link

4
推荐指数
2
解决办法
2422
查看次数