Skip to content

Commit 9658277

Browse files
committed
feat: extended_headers
1 parent 509c128 commit 9658277

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = function ArtemisGraphql(moduleOptions) {
1616
options: {
1717
browserUri: moduleOptions.browserUri,
1818
serverUri: moduleOptions.serverUri,
19+
extendedHeaders: moduleOptions.extendedHeaders,
1920
graphqlFolder: moduleOptions.graphqlFolder || '~/graphql',
2021
},
2122
});

src/plugin.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@ import { partial } from 'lodash';
2828

2929
const serverUri = '<%= options.serverUri %>';
3030
const browserUri = '<%= options.browserUri %>';
31+
const extendedHeadersStoreLocation = '<%= options.extendedHeaders %>';
32+
33+
const createAuthLink = store =>
34+
setContext((_, { headers }) => {
35+
const extendedHeaders = store.getters[extendedHeadersStoreLocation];
36+
37+
return {
38+
headers: {
39+
...headers,
40+
...extendedHeaders,
41+
},
42+
};
43+
});
3144

32-
const createAuthLink = store => setContext((_, { headers }) => {
33-
const token = store.getters['auth/token'];
34-
const context = store.getters['auth/context'];
35-
36-
return {
37-
headers: {
38-
...headers,
39-
...(context && { 'X-Authorization-Context': context }),
40-
Authorization: token ? 'Bearer ' + token : '',
41-
},
42-
};
43-
});
44-
45-
const createBatchedPayloadGetter = results => async () => JSON.stringify(results.map(result => result.payload));
45+
const createBatchedPayloadGetter = results => async () =>
46+
JSON.stringify(results.map(result => result.payload));
4647

4748
const batchFetch = async (uri, options) => {
4849
const response = await fetch(uri, options);
@@ -58,18 +59,17 @@ function maybeFetchEnv(uri, env) {
5859
return uri;
5960
}
6061

61-
function createClient({ link, store, queries, mutations, env }) {
62+
function createClient({
63+
link, store, queries, mutations, env
64+
}) {
6265
const splitLink = ApolloLink.split(
6366
() => process.server,
6467
createHttpLink({ uri: maybeFetchEnv(serverUri, env), fetch }),
6568
createHttpLink({ uri: maybeFetchEnv(browserUri, env), fetch }),
6669
// new BatchHttpLink({ uri: localUri, fetch: batchFetch }),
6770
);
6871

69-
const defaultLink = ApolloLink.from([
70-
createAuthLink(store),
71-
splitLink,
72-
]);
72+
const defaultLink = ApolloLink.from([createAuthLink(store), splitLink]);
7373

7474
const cache = new InMemoryCache();
7575

@@ -92,7 +92,11 @@ function createClient({ link, store, queries, mutations, env }) {
9292
const [name, variables] = Array.isArray(config) ? config : [config, vars];
9393

9494
return new Promise((resolve) => {
95-
client[executor[type]]({ [type]: docs[type][name], fetchPolicy: 'no-cache', variables })
95+
client[executor[type]]({
96+
[type]: docs[type][name],
97+
fetchPolicy: 'no-cache',
98+
variables,
99+
})
96100
.then((data) => {
97101
resolve({ data: data.data, errors: [] });
98102
})
@@ -114,7 +118,8 @@ function createClient({ link, store, queries, mutations, env }) {
114118
type,
115119
config.map((name, index) => [name, variables && variables[index]]),
116120
);
117-
} if (config === Object(config)) {
121+
}
122+
if (config === Object(config)) {
118123
return execAll(type, Object.entries(config));
119124
}
120125
return exec(type, config, variables);
@@ -143,7 +148,10 @@ function createClient({ link, store, queries, mutations, env }) {
143148
const m = partial(c, 'mutation');
144149

145150
return {
146-
query, q, mutate, m,
151+
query,
152+
q,
153+
mutate,
154+
m,
147155
};
148156
}
149157

@@ -155,8 +163,13 @@ export default ({ store, nuxtState, isServer }, inject) => {
155163
const env = isServer ? process.env : nuxtState.env || {};
156164

157165
const {
158-
query, q, mutate, m,
159-
} = createClient({ store, queries, mutations, env });
166+
query, q, mutate, m
167+
} = createClient({
168+
store,
169+
queries,
170+
mutations,
171+
env,
172+
});
160173

161174
inject('query', query);
162175
inject('q', q);

0 commit comments

Comments
 (0)