我想实现一种根据 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-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 …
目前,当我使用 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) 阿波罗应该retryLink在 之前还是之后出现errorLink?一些示例显示它之前https://medium.com/@joanvila/productizing-apollo-links-4cdc11d278eb#3249而一些示例显示它之后https://www.apollographql.com/docs/react/api/link/apollo -link-rest/#link-order。
我有一个带有 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