Salut,
J'ai passé plusieurs jours dessus sans succès.
Ce dont j'ai essentiellement besoin, c'est d'accéder au client, afin que je puisse appeler une mutation pour modifier un état global s'il y a une erreur dans l'arterware.
const client = location => new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors && graphQLErrors.find(isUnauthorized) && !location.pathname.startsWith('/login')) {
client.resetStore();
client.mutate({ mutation: notificationAddMutation, variables: { text: 'You did not have rights to this operation. Maybe logged out?' } });
}
if (networkError) {
client.mutate({ mutation: notificationAddMutation, variables: { text: 'There was a network problem. Please check your connection' } });
}
}),
withClientState({
cache,
defaults,
resolvers,
typeDefs,
}),
httpLink,
]),
cache,
});
J'obtiens cette erreur : TypeError : client.mutate n'est pas une fonction
Des idées sur la façon d'accéder au client là-bas? Ou comment appeler une mutation locale sans client ?
Dans votre code, client
est défini sur une fonction qui renvoie une instance d'Apollo Client, donc je pense que vous devrez faire quelque chose comme :
const clientFunction = location => {
const client = new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError }) => {
if (
graphQLErrors &&
graphQLErrors.find(isUnauthorized) &&
!location.pathname.startsWith('/login')
) {
client.resetStore();
client.mutate({
mutation: notificationAddMutation,
variables: {
text:
'You did not have rights to this operation. Maybe logged out?',
},
});
}
if (networkError) {
client.mutate({
mutation: notificationAddMutation,
variables: {
text: 'There was a network problem. Please check your connection',
},
});
}
}),
withClientState({
cache,
defaults,
resolvers,
typeDefs,
}),
httpLink,
]),
cache,
});
return client;
};
clientFunction(LOCATION);
Merci beaucoup,
cela a fonctionné.
Pour toute personne intéressée :
Je rencontre beaucoup de problèmes avec le postware et je dois encore avoir une logique substantielle dans chaque gestionnaire d'erreur de mutation, donc je vais probablement abandonner complètement le postware et avoir une fonction que j'utiliserai dans chaque bloc de capture de mutation.
@evans merci. Ça marche.
article lié à stackoverflow : https://stackoverflow.com/questions/51664533/how-to-access-apollo-client-in-apollo-link-error-to-resetstore
Hey, je n'arrive pas à faire fonctionner ça
const graphQLClient = new ApolloClient({
cache: new InMemoryCache(),
link: ApolloLink.from([
authLink(graphQLClient),
refreshLink(graphQLClient),
httpLink,
]),
})
const authLink = (graphQLClient) => setContext(async (req, { headers }) => { ... }
const refreshLink = (graphQLClient) => onError(({ graphQLErrors, networkError, operation, forward }) => { ... }
J'ai passé graphQLClient aux fonctions de lien mais graphQLClient est undefined
dans les liens
Commentaire le plus utile
Dans votre code,
client
est défini sur une fonction qui renvoie une instance d'Apollo Client, donc je pense que vous devrez faire quelque chose comme :