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.
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.
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.
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.
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.
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
});
}
You can import ApolloProvider
like this:
import { ApolloProvider } from "@apollo/client";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With