Skip to content

Commit

Permalink
Fixed dependency on accounts-base
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorDiaconu committed Oct 3, 2019
1 parent b353560 commit ef3a05f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 26 deletions.
1 change: 1 addition & 0 deletions client/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default {
disableWebsockets: false,
disableMeteorAccounts: false,
getLink: link => link,
httpLinkOptions: {},
};
6 changes: 3 additions & 3 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createHttpLink } from 'apollo-link-http';
import ApolloLink from 'apollo-link';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { getMainDefinition } from 'apollo-utilities';
import { meteorAccountsLink } from './meteorAccountsLink';
import meteorAccountsLink from './meteorAccountsLink';
import { createUploadLink } from 'apollo-upload-client';
import Config from './config';
import { checkNpmVersions } from 'meteor/tmeasday:check-npm-versions';
Expand Down Expand Up @@ -51,8 +51,8 @@ export function initialize(config = {}) {

// We define the HTTP Link
const httpLink = createHttpLink(config.httpLinkOptions);
if (meteorAccountsLink) {

if (!config.disableMeteorAccounts) {
terminatingLink = ApolloLink.concat(meteorAccountsLink, uploadLink, httpLink);
} else {
terminatingLink = ApolloLink.concat(uploadLink, httpLink);
Expand Down
30 changes: 9 additions & 21 deletions client/meteorAccountsLink.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
import { ApolloClient } from 'apollo-client';
import { ApolloLink } from 'apollo-link';

let meteorAccountsLink;
export default new ApolloLink((operation, forward) => {
const token = localStorage.getItem('Meteor.loginToken');

// We have a weak dependency on this package, and if we import it without it being added, it will crash
if (Package['accounts-base']) {
import { Accounts } from 'meteor/accounts-base';

meteorAccountsLink = new ApolloLink((operation, forward) => {
const token = Accounts._storedLoginToken();

operation.setContext(() => ({
headers: {
'meteor-login-token': token,
},
}));

return forward(operation);
});
}
operation.setContext(() => ({
headers: {
'meteor-login-token': token,
},
}));

export {
meteorAccountsLink
}
return forward(operation);
});
8 changes: 7 additions & 1 deletion docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ initialize({
uri: 'http://endpoint:5000/graphql',

// Whether or not to try to connect to websockets, it connects by default
disableWebsockets: false,
disableWebsockets: false,

// Whether to pass `meteor-login-token` as header read from localStorage
disableMeteorAccounts: false,

// Here you can concatenated stuff to your links and perform whatever operations you prefer
getLink: link => newLink
});
```

Expand Down
2 changes: 1 addition & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ checkNpmVersions({
'apollo-server-express': '2.x.x',
graphql: '14.x.x',
'graphql-load': '0.1.x',
'graphql-type-json': '0.2.x',
'graphql-type-json': '0.x.x',
'graphql-tools': '4.x.x',
});

0 comments on commit ef3a05f

Please sign in to comment.