Use Multiple Clients for Your GraphQL Queries with React Apollo Client
npm install @titelmedia/react-apollo-multiple-clients``js`
yarn @titelmedia/react-apollo-multiple-clients
and use instead. Use the Higher Order Component 'withClient' to set a namespace.`js
import React from 'react';
import ReactDOM from 'react-dom';
import ApolloClient from 'apollo-boost';
import { Query, Mutation } from 'react-apollo';import { ApolloMultipleClientsProvider, withClient } from '@titelmedia/react-apollo-multiple-clients';
const client1 = new ApolloClient({
uri: 'https://graphql.client1.com'
});
const client2 = new ApolloClient({
uri: 'https://graphql.client2.com'
});
const clients = {
firstNamespace: client1,
secondNamespace: client2,
};
const FETCH_TEST = gql
;const InnerComponent = ({error, load, data: { test }}) => (loading || error) ? null : test;
const Component = ({ ...rest }) => (
);
const ClientQueryContainer = withClient('clientName1')(Component);
const ClientQuery1 = withClient('clientName1')(Component);
const ClientQuery2 = withClient('clientName2')(Component);
const App = () => (
,
);
/* Output will be something like
// context is client1
// context is client2
// context is client1 and is not extra wrapped inside a provider
// context is client2 and will be wrapped inside a new container
*/
ReactDOM.render( , document.getElementById('root'));
``