Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to bind to a component to use the apollo react client even if it has no UI?

Tags:

react-apollo

I'm doing some business logic around an implementation of the Auth0 lock widget and need to call some graphql mutations to sign in. In this case there is no UI to render since it's simply a call to Auth0's lock.show() method. However, looking at all the apollo client examples with react - the graphql method seems to bind the functions to a component.

  • Can I call a gql mutation directly without binding it to a react component?
  • What's the best way to get the client from the ApolloProvider if I'm not in a component? Is there a static singleton instance i can reference from ApolloProvider or do I need to pass the client reference from the root application compoment?
like image 637
MonkeyBonkey Avatar asked Nov 16 '16 14:11

MonkeyBonkey


People also ask

How do you use Apollo Client in React?

To connect Apollo Client to React, you will need to use the ApolloProvider component exported from @apollo/react-hooks . The ApolloProvider is similar to React's Context. Provider . It wraps your React app and places the client on the context, which allows you to access it from anywhere in your component tree.

Do you have to use Apollo Client with Apollo server?

Apollo Server is an open-source, spec-compliant GraphQL server that's compatible with any GraphQL client, including Apollo Client. It's the best way to build a production-ready, self-documenting GraphQL API that can use data from any source.

How do I use Apollo redux Client in React?

If you want to use your Redux and Apollo state in a component, you need to use both graphql from react-apollo and connect from react-redux. This will let you better track the different events that happen in your app, and how your client and server side data changes interleave.

How do you access the Apollo Client in child components?

One way to access the configured Apollo Client instance directly is to create an ApolloConsumer component and provide a render prop function as its child. The render prop function will be called with your ApolloClient instance as its only argument.


2 Answers

You can use the withApollo() decorator exported from apollo-client to access the client as a prop inside a component. The ApolloProvider exposes the client to its child components through context. The withApollo() higher-order component accesses the client on context and passes it to its child as a prop.

So, if the auth.lock() function is being triggered by some type of UI interaction or one of the React lifecycle methods, you can access the client in that component and either call the mutation directly in the component or pass it as an argument through to the function that calls auth.lock().

However, since you want to access the client outside of the React tree, you have to access the client in a different way.

Alternatively, you can export the same client that you pass as a prop to ApolloProvider and import it wherever you need to use it in the app. Note, this singleton pattern won't work with server-side rendering. Example:

root.jsx

import React from 'react';
import { Router, browserHistory } from 'react-router';
import ApolloClient, { createNetworkInterface } from 'apollo-client';
import { syncHistoryWithStore } from 'react-router-redux';
import routes from './routes';

const networkInterface = createNetworkInterface({
  uri: '/graphql',
  opts: {
    credentials: 'same-origin'
  }
});

export const client = new ApolloClient({
  networkInterface
});
export const store = configureStore(browserHistory, client);
export const history = syncHistoryWithStore(browserHistory, store);

export default function Root() {
  <ApolloProvider client={client} store={store}>
    <Router
      history={history}
      routes={routes}
    />
  </ApolloProvider>
}

some-other-module.js

import { client } from 'app/root';

export default function login(username, password) {
  return client.mutate({
    // ...mutationStuff
  });
}
like image 164
Brandon Avatar answered Sep 19 '22 03:09

Brandon


You can import ApolloProvider like this:

import { ApolloProvider } from "@apollo/client";

like image 24
Ahmed Mersal Avatar answered Sep 21 '22 03:09

Ahmed Mersal