Skip to content

Commit b27ae6d

Browse files
committed
feat: maybe uri from env
1 parent e3ca6a8 commit b27ae6d

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ module.exports = function ArtemisGraphql(moduleOptions) {
1414
this.addPlugin({
1515
src: resolve(__dirname, 'src', 'plugin.js'),
1616
options: {
17-
localUri: moduleOptions.localUri || 'http://localhost:4000/api/grapqhl',
18-
serverUri: moduleOptions.serverUri || 'http://localhost:4000/api/grapqhl',
17+
browserUri: moduleOptions.browserUri,
18+
serverUri: moduleOptions.serverUri,
1919
graphqlFolder: moduleOptions.graphqlFolder || '~/graphql',
2020
},
2121
});

src/plugin.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@ import { setContext } from 'apollo-link-context';
2727
import { partial } from 'lodash';
2828

2929
const serverUri = '<%= options.serverUri %>';
30-
const localUri = '<%= options.localUri %>';
30+
const browserUri = '<%= options.browserUri %>';
3131

3232
const createAuthLink = store => setContext((_, { headers }) => {
3333
const token = store.getters['auth/token'];
34-
const context = store.getters['auth/context'];
3534

3635
return {
3736
headers: {
3837
...headers,
39-
...(context && { 'X-Authorization-Context': context }),
40-
Authorization: token ? 'Bearer ' + token : '',
38+
authorization: token ? 'Bearer ' + token : '',
4139
},
4240
};
4341
});
@@ -51,11 +49,18 @@ const batchFetch = async (uri, options) => {
5149
return { text };
5250
};
5351

54-
function createClient({ link, store, queries, mutations }) {
52+
function maybeFetchEnv(uri, env) {
53+
if (uri.startsWith('env://')) {
54+
return env[uri.substr(6)];
55+
}
56+
return uri;
57+
}
58+
59+
function createClient({ link, store, queries, mutations, env }) {
5560
const splitLink = ApolloLink.split(
5661
() => process.server,
57-
createHttpLink({ uri: serverUri, fetch }),
58-
createHttpLink({ uri: localUri, fetch }),
62+
createHttpLink({ uri: maybeFetchEnv(serverUri, env), fetch }),
63+
createHttpLink({ uri: maybeFetchEnv(browserUri, env), fetch }),
5964
// new BatchHttpLink({ uri: localUri, fetch: batchFetch }),
6065
);
6166

@@ -142,13 +147,14 @@ function createClient({ link, store, queries, mutations }) {
142147

143148
export { createClient };
144149

145-
export default ({ store }, inject) => {
150+
export default ({ store, nuxtState, isServer }, inject) => {
146151
const queries = require('<%= options.graphqlFolder %>/queries').default;
147152
const mutations = require('<%= options.graphqlFolder %>/mutations').default;
153+
const env = isServer ? process.env : nuxtState.env || {};
148154

149155
const {
150156
query, q, mutate, m,
151-
} = createClient({ store, queries, mutations });
157+
} = createClient({ store, queries, mutations, env });
152158

153159
inject('query', query);
154160
inject('q', q);

0 commit comments

Comments
 (0)