Skip to content
This repository was archived by the owner on Apr 14, 2023. It is now read-only.

Add client awareness settings to request headers #872

Merged
merged 6 commits into from
Nov 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"editor.tabSize": 2,
"editor.rulers": [140],
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"files.exclude": {
9 changes: 9 additions & 0 deletions packages/apollo-link-http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change log

### vNext

- If `name` or `version` client awareness settings are found in the
incoming `operation` `context`, they'll be extracted and added as headers
to all outgoing requests. The header names used (`apollographql-client-name`
and `apollographql-client-version`) line up with the associated Apollo Server
changes made in https://github.com/apollographql/apollo-server/pull/1960. <br/>
[@hwillson](http://github.com/hwillson) in [#872](https://github.com/apollographql/apollo-link/pull/872)

### 1.5.5
- Added `graphql` 14 to peer and dev deps; Updated `@types/graphql` to 14 <br/>
[@hwillson](http://github.com/hwillson) in [#789](https://github.com/apollographql/apollo-link/pull/789)
33 changes: 33 additions & 0 deletions packages/apollo-link-http/src/__tests__/httpLink.ts
Original file line number Diff line number Diff line change
@@ -183,6 +183,39 @@ describe('HttpLink', () => {
}),
);
});

it('should add client awareness settings to request headers', done => {
const variables = { params: 'stub' };
const link = createHttpLink({
uri: 'http://data/',
});

const clientAwareness = {
name: 'Some Client Name',
version: '1.0.1',
};

execute(link, {
query: sampleQuery,
variables,
context: {
clientAwareness,
},
}).subscribe(
makeCallback(done, result => {
const [uri, options] = fetchMock.lastCall();
const { headers } = options;
expect(headers['apollographql-client-name']).toBeDefined();
expect(headers['apollographql-client-name']).toEqual(
clientAwareness.name,
);
expect(headers['apollographql-client-version']).toBeDefined();
expect(headers['apollographql-client-version']).toEqual(
clientAwareness.version,
);
}),
);
});
});

it("throws for GET if the variables can't be stringified", done => {
17 changes: 16 additions & 1 deletion packages/apollo-link-http/src/httpLink.ts
Original file line number Diff line number Diff line change
@@ -64,11 +64,26 @@ export const createHttpLink = (linkOptions: HttpLink.Options = {}) => {

const context = operation.getContext();

// `apollographql-client-*` headers are automatically set if a
// `clientAwareness` object is found in the context. These headers are
// set first, followed by the rest of the headers pulled from
// `context.headers`. If desired, `apollographql-client-*` headers set by
// the `clientAwareness` object can be overridden by
// `apollographql-client-*` headers set in `context.headers`.
const clientAwarenessHeaders = {};
if (context.clientAwareness) {
const { name, version } = context.clientAwareness;
clientAwarenessHeaders['apollographql-client-name'] = name;
clientAwarenessHeaders['apollographql-client-version'] = version;
}

const contextHeaders = { ...clientAwarenessHeaders, ...context.headers };

const contextConfig = {
http: context.http,
options: context.fetchOptions,
credentials: context.credentials,
headers: context.headers,
headers: contextHeaders,
};

//uses fallback, link, and then context to build options