Skip to content

Commit 5e8e392

Browse files
authored
Dynamic headers (#54)
1 parent 20b1cc9 commit 5e8e392

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

dist/vuex-orm-graphql.esm.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27070,7 +27070,6 @@ var Apollo = /** @class */ (function () {
2707027070
this.httpLink = new HttpLink({
2707127071
uri: context.options.url ? context.options.url : '/graphql',
2707227072
credentials: context.options.credentials ? context.options.credentials : 'same-origin',
27073-
headers: context.options.headers ? context.options.headers : {},
2707427073
useGETForQueries: Boolean(context.options.useGETForQueries)
2707527074
});
2707627075
}
@@ -27093,18 +27092,19 @@ var Apollo = /** @class */ (function () {
2709327092
if (mutation === void 0) { mutation = false; }
2709427093
if (bypassCache === void 0) { bypassCache = false; }
2709527094
return __awaiter$1(this, void 0, void 0, function () {
27096-
var fetchPolicy, response;
27095+
var fetchPolicy, context, response;
2709727096
return __generator$1(this, function (_a) {
2709827097
switch (_a.label) {
2709927098
case 0:
2710027099
fetchPolicy = bypassCache ? 'network-only' : 'cache-first';
2710127100
Context.getInstance().logger.logQuery(query, variables, fetchPolicy);
27101+
context = { headers: Apollo.getHeaders() };
2710227102
if (!mutation) return [3 /*break*/, 2];
27103-
return [4 /*yield*/, this.apolloClient.mutate({ mutation: query, variables: variables })];
27103+
return [4 /*yield*/, this.apolloClient.mutate({ mutation: query, variables: variables, context: context })];
2710427104
case 1:
2710527105
response = _a.sent();
2710627106
return [3 /*break*/, 4];
27107-
case 2: return [4 /*yield*/, this.apolloClient.query({ query: query, variables: variables, fetchPolicy: fetchPolicy })];
27107+
case 2: return [4 /*yield*/, this.apolloClient.query({ query: query, variables: variables, fetchPolicy: fetchPolicy, context: context })];
2710827108
case 3:
2710927109
response = _a.sent();
2711027110
_a.label = 4;
@@ -27121,17 +27121,25 @@ var Apollo = /** @class */ (function () {
2712127121
var fetchPolicy;
2712227122
return __generator$1(this, function (_a) {
2712327123
fetchPolicy = bypassCache ? 'network-only' : 'cache-first';
27124-
return [2 /*return*/, this.apolloClient.query({ query: src(query), variables: variables, fetchPolicy: fetchPolicy, context: context })];
27124+
return [2 /*return*/, this.apolloClient.query({ query: src(query), variables: variables, fetchPolicy: fetchPolicy, context: { headers: Apollo.getHeaders() } })];
2712527125
});
2712627126
});
2712727127
};
2712827128
Apollo.prototype.simpleMutation = function (query, variables, context) {
2712927129
return __awaiter$1(this, void 0, void 0, function () {
2713027130
return __generator$1(this, function (_a) {
27131-
return [2 /*return*/, this.apolloClient.mutate({ mutation: src(query), variables: variables, context: context })];
27131+
return [2 /*return*/, this.apolloClient.mutate({ mutation: src(query), variables: variables, context: { headers: Apollo.getHeaders() } })];
2713227132
});
2713327133
});
2713427134
};
27135+
Apollo.getHeaders = function () {
27136+
var context = Context.getInstance();
27137+
var headers = context.options.headers ? context.options.headers : {};
27138+
if (headers instanceof Function) {
27139+
headers = headers(context);
27140+
}
27141+
return headers;
27142+
};
2713527143
return Apollo;
2713627144
}());
2713727145

docs/guide/setup/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ These are the possible options to pass when calling `VuexORM.use()`:
3232
- `database` (required): The Vuex-ORM database.
3333
- `debug` (optional, default: `false`): Set to true to activate the debug logging.
3434
- `url` (optional, default: `/graphql`): The URL to the graphql api. Will be passed to apollo-client.
35-
- `headers` (optional, default: `{}`) HTTP Headers. See [apollo-link-http])(https://github.com/apollographql/apollo-link/tree/master/packages/apollo-link-http#options)
35+
- `headers` (optional, default: `{}`) HTTP Headers. See
36+
[apollo-link-http])(https://github.com/apollographql/apollo-link/tree/master/packages/apollo-link-http#options).
37+
This can be a static object or a function, returning a object, which will be called before a request is made.
3638
- `credentials` (optional, default: `same-origin`) Credentials Policy. See [apollo-link-http])(https://github.com/apollographql/apollo-link/tree/master/packages/apollo-link-http#options)
3739
- `useGETForQueries` (optional, default: `false`) Use GET for queries (not for mutations). See [apollo-link-http])(https://github.com/apollographql/apollo-link/tree/master/packages/apollo-link-http#options)
3840
- `connectionQueryMode` (optional, default: `auto`). One of `auto | nodes | edges | plain`. See [Connection Mode](/guide/connection-mode)

src/graphql/apollo.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export default class Apollo {
3737
this.httpLink = new HttpLink({
3838
uri: context.options.url ? context.options.url : '/graphql',
3939
credentials: context.options.credentials ? context.options.credentials : 'same-origin',
40-
headers: context.options.headers ? context.options.headers : {},
4140
useGETForQueries: Boolean(context.options.useGETForQueries)
4241
});
4342
}
@@ -64,11 +63,13 @@ export default class Apollo {
6463
const fetchPolicy: FetchPolicy = bypassCache ? 'network-only' : 'cache-first';
6564
Context.getInstance().logger.logQuery(query, variables, fetchPolicy);
6665

66+
const context = { headers: Apollo.getHeaders() };
67+
6768
let response;
6869
if (mutation) {
69-
response = await this.apolloClient.mutate({ mutation: query, variables });
70+
response = await this.apolloClient.mutate({ mutation: query, variables, context });
7071
} else {
71-
response = await this.apolloClient.query({ query, variables, fetchPolicy });
72+
response = await this.apolloClient.query({ query, variables, fetchPolicy, context });
7273
}
7374

7475
// Transform incoming data into something useful
@@ -77,10 +78,22 @@ export default class Apollo {
7778

7879
public async simpleQuery (query: string, variables: Arguments, bypassCache: boolean = false, context?: Data): Promise<any> {
7980
const fetchPolicy: FetchPolicy = bypassCache ? 'network-only' : 'cache-first';
80-
return this.apolloClient.query({ query: gql(query), variables, fetchPolicy, context });
81+
return this.apolloClient.query({ query: gql(query), variables, fetchPolicy, context: { headers: Apollo.getHeaders() } });
8182
}
8283

8384
public async simpleMutation (query: string, variables: Arguments, context?: Data): Promise<any> {
84-
return this.apolloClient.mutate({ mutation: gql(query), variables, context });
85+
return this.apolloClient.mutate({ mutation: gql(query), variables, context: { headers: Apollo.getHeaders() } });
86+
}
87+
88+
private static getHeaders () {
89+
const context = Context.getInstance();
90+
91+
let headers: Object | Function = context.options.headers ? context.options.headers : {};
92+
93+
if (headers instanceof Function) {
94+
headers = headers(context);
95+
}
96+
97+
return headers;
8598
}
8699
}

0 commit comments

Comments
 (0)