How to execute an async fetch request and then retry last failed request?

I’m refreshing the token this way (updated OP’s): import { ApolloClient } from ‘apollo-client’; import { onError } from ‘apollo-link-error’; import { ApolloLink, Observable } from ‘apollo-link’; // add Observable // Define Http link const httpLink = new createHttpLink({ uri: ‘/my-graphql-endpoint’, credentials: ‘include’ }); // Add on error handler for apollo link return new ApolloClient({ … Read more

How to chain two GraphQL queries in sequence using Apollo Client

The props added by your firstQuery component will be available to the component below (inside) it, so you can do something like: export default compose( graphql(firstQuery, { name: ‘firstQuery’ }), graphql(secondQuery, { name: ‘secondQuery’, skip: ({ firstQuery }) => !firstQuery.data, options: ({firstQuery}) => ({ variables: { var1: firstQuery.data.someQuery.someValue } }) }) )(withRouter(TestPage)) Notice that we … Read more

How to refresh JWT token using Apollo and GraphQL

The example given in the the Apollo Error Link documentation is a good starting point but assumes that the getNewToken() operation is synchronous. In your case, you have to hit your GraphQL endpoint to retrieve a new access token. This is an asynchronous operation and you have to use the fromPromise utility function from the … Read more

apollo-client does not work with CORS

To workaround the CORS issue with Apollo you have to pass the no-cors option to the underlying fetch. import ApolloClient from “apollo-boost”; const client = new ApolloClient({ uri: “your client uri goes here”, fetchOptions: { mode: ‘no-cors’, }, }); This is not a specific Apollo problem, rather a configuration that is meant to be tackled … Read more

GraphQL mutation: Invariant Violation: Must contain a query definition

You should use the mutate method of the client for mutations, not the query method. The options for the method can be found in the docs. Apollo is opinionated about how queries and mutations are treated, so each method has different options that are appropriate to each operation’s behavior (for example, mutate includes a refetchQueries … Read more

Using ApolloClient with node.js. “fetch is not found globally and no fetcher passed”

If you still want to use Apollo Boost in Node.js but need to polyfill the native fetch API of the browser, try out cross-fetch. I used it for my minimal example over here. And that’s how it can be used after installing it: import ‘cross-fetch/polyfill’; import ApolloClient from ‘apollo-boost’; const client = new ApolloClient({ uri: … Read more

Apollo Client Cache vs. Redux

You’re comparing apples to oranges. Yes, at a high level both redux and apollo-client provide a way for you to manage global application state. However, redux allows you to create a predictable state container that changes in response to the actions you define. That means redux offers: Predictability. Reducers are pure functions — given the … Read more