Skip to content

Commit 08e6298

Browse files
committed
Fix import errors, documentation
1 parent d9fcd28 commit 08e6298

File tree

4 files changed

+315
-272
lines changed

4 files changed

+315
-272
lines changed

README.md

Lines changed: 82 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,111 @@
11
# Slicknode Apollo Link
22

3-
ApolloLink component to use [slicknode-client](https://github.com/slicknode/slicknode-client)
4-
with [apollo-client](https://www.apollographql.com/client).
3+
ApolloLink component that automatically sets authentication headers for GraphQL requests via the [apollo-client](https://www.apollographql.com/client). It stores the access and refresh tokens in a store (for example InMemory, localStorage, sessionStorage etc.) and keeps track of expiration times.
4+
If auth tokens expire, they are automatically refreshed in the background when a request is issued, without interruption to the user. Can be combined with any of the available [apollo links](https://www.apollographql.com/docs/link/#linkslist).
55

66
## Installation
77

8-
Install both the [slicknode-client](https://github.com/slicknode/slicknode-client) and the
9-
[slicknode-apollo-link](https://github.com/slicknode/slicknode-apollo-link) npm package:
8+
Install the [slicknode-apollo-link](https://github.com/slicknode/slicknode-apollo-link) npm package:
109

11-
yarn add slicknode-client slicknode-apollo-link
10+
yarn add slicknode-apollo-link
11+
12+
There is also a peer dependencie to `graphql` which you should already have installed when you are using the [apollo-client](https://www.apollographql.com/client).
1213

1314
## Usage
1415

1516
This is a minimal example to create an instance of an apollo client. Refer to the documentation of the
16-
[slicknode-client](https://github.com/slicknode/slicknode-client) and the [apollo-client](https://www.apollographql.com/client)
17-
to learn how to use and customize it:
17+
[apollo-client](https://www.apollographql.com/client) to learn how to use and customize it:
1818

19-
```typescript
20-
import SlicknodeClient from 'slicknode-client';
19+
```javascript
2120
import SlicknodeLink from 'slicknode-apollo-link';
2221
import { ApolloClient } from 'apollo-client';
2322
import { InMemoryCache } from 'apollo-cache-inmemory';
23+
import { ApolloLink } from 'apollo-link';
24+
import { HttpLink } from 'apollo-link-http';
2425

25-
// Create an instance of the slicknode-client
26-
const slicknodeClient = new SlicknodeClient({
27-
// Set your Slicknode GraphQL endpoint here
28-
endpoint: 'https://myproject.slicknode.com'
29-
});
26+
const slicknodeLink = new SlicknodeLink();
3027

3128
// Create the ApolloClient instance to use in your projects
32-
const apolloClient = new ApolloClient({
33-
// Pass the Slicknode client to the link that should be used by the ApolloClient
34-
link: new SlicknodeLink({
35-
client: slicknodeClient
36-
}),
29+
const client = new ApolloClient({
30+
link: ApolloLink.from([
31+
// Add the slicknode link somewhere before the HttpLink
32+
slicknodeLink,
33+
34+
// ... more links (retry, error handling etc.)
35+
36+
new HttpLink({
37+
// Add your slicknode GraphQL endpoint here
38+
uri: 'https://you-project.slicknode.com',
39+
credentials: 'same-origin',
40+
}),
41+
]),
3742

3843
// Add a cache (required by apollo, change as needed...)
3944
cache: new InMemoryCache()
4045
});
4146

42-
// Use the apolloClient as usual... (See apollo-client documentation)
47+
// Use the client as usual... (See apollo-client documentation)
4348
```
4449

45-
### Usage with authenticators
50+
### Authentication
51+
52+
To authenticate the client on the server and obtain an auth token set, you can execute any mutation
53+
that returns such data. By adding the directive `@authenticate` to the mutation, the `SlicknodeLink`
54+
automatically picks up the tokens, stores them on the client and adds the required authentication headers
55+
to subsequent requests.
56+
57+
Make sure that the module with the login mutation is installed and deployed to your Slicknode server. See the list of
58+
[available auth modules](#available-auth-modules) for details.
59+
60+
For example:
61+
62+
```javascript
63+
import { gql } from 'graphql-tag';
64+
65+
client.query(gql`
66+
mutation LoginUser {
67+
loginEmailPassword(input: {email: "[email protected]", password: "xyz123"}) @authenticate {
68+
accessToken
69+
refreshToken
70+
accessTokenLifetime
71+
refreshTokenLifetime
72+
}
73+
}`
74+
)
75+
.then(result => {
76+
console.log('');
77+
})
78+
.catch(err => {
79+
console.log('Something went wrong: ', err.message);
80+
});
81+
```
4682

47-
To use the `SlicknodeLink` instance in combination with the available authenticators, use the
48-
`SlicknodeClient` instance directly that was passed to the link component. Once the `SlicknodeClient` is authenticated,
49-
the `ApolloClient` instance will also make requests as the authenticated user.
5083

51-
```typescript
52-
// Create slicknode client / apollo client and link as described above
53-
// Then use any authenticator:
84+
### Logout
85+
86+
To log the user out, you can execute the `logoutUser` mutation. This automatically deletes the auth tokens
87+
from the store when successful and invalidates the refresh token no the server:
88+
89+
```javascript
90+
client.query({
91+
query: gql`mutation LogoutUser($token: String) {
92+
logoutUser(input: {refreshToken: $token}) {
93+
success
94+
}
95+
}`,
96+
variables: {
97+
// Get the current refreshToken from the SlicknodeLink instance to invalidate it on the server
98+
token: slicknodeLink.getRefreshToken()
99+
}
100+
)
101+
.then(result => {
102+
console.log('Login successful', result.data.logoutUser);
103+
})
104+
.catch(err => {
105+
console.log('Something went wrong: ', err.message);
106+
});
107+
```
54108
55-
import login from 'slicknode-auth-email-password';
109+
### Available Auth Modules
56110
57-
slicknodeClient.authenticate(login('[email protected]', 'mysecretpassword'))
58-
.then(() => {
59-
console.log('Login successful');
60-
})
61-
.catch(e => {
62-
console.log('Something went wrong: ' + e.message);
63-
});
64-
```
65111
66-
**Tipp:** You might want to invalidate the cached data in the apollo client once the authentication state changes
67-
inside of the slicknode client.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
],
3030
"devDependencies": {
3131
"@types/chai": "^4.1.4",
32+
"@types/graphql": "^14.0.3",
3233
"@types/mocha": "^5.2.5",
3334
"@types/sinon": "^5.0.2",
3435
"chai": "^4.1.2",
@@ -63,5 +64,7 @@
6364
"apollo-utilities": "^1.0.26",
6465
"graphql-tag": "^2.10.0"
6566
},
66-
"peerDependencies": {}
67+
"peerDependencies": {
68+
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0"
69+
}
6770
}

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
export {default as default} from './SlicknodeLink';
1+
export {
2+
default as default,
3+
LOGOUT_MUTATION,
4+
REFRESH_TOKEN_MUTATION,
5+
} from './SlicknodeLink';
26

37
export * from './storage';
8+
export * from './types';

0 commit comments

Comments
 (0)