Slicknode Apollo Link
ApolloLink component to use slicknode-client
with apollo-client.
Installation
Install both the slicknode-client and the
slicknode-apollo-link npm package:
yarn add slicknode-client slicknode-apollo-link
Usage
This is a minimal example to create an instance of an apollo client. Refer to the documentation of the
slicknode-client and the apollo-client
to learn how to use and customize it:
import SlicknodeClient from 'slicknode-client';
import SlicknodeLink from 'slicknode-apollo-link';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
const slicknodeClient = new SlicknodeClient({
endpoint: 'https://myproject.slicknode.com'
});
const apolloClient = new ApolloClient({
link: new SlicknodeLink({
client: slicknodeClient
}),
cache: new InMemoryCache()
});
Usage with authenticators
To use the SlicknodeLink
instance in combination with the available authenticators, use the
SlicknodeClient
instance directly that was passed to the link component. Once the SlicknodeClient
is authenticated,
the ApolloClient
instance will also make requests as the authenticated user.
import login from 'slicknode-auth-email-password';
slicknodeClient.authenticate(login('myemail@example.com', 'mysecretpassword'))
.then(() => {
console.log('Login successful');
})
.catch(e => {
console.log('Something went wrong: ' + e.message);
});
Tipp: You might want to invalidate the cached data in the apollo client once the authentication state changes
inside of the slicknode client.