Skip to content

Commit e22726d

Browse files
Variables can now be passed.
1 parent 53503f9 commit e22726d

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ Example for a components' server.js:
5151
module.exports.data = function(context, callback){
5252
const qb = context.plugins.graphql.queryBuilder;
5353

54-
const query = qb`restaurant(id: 4) {
54+
const query = qb`restaurant(id: $id) {
5555
name
5656
}`;
5757

5858
const headers = {
5959
'accept-language': 'en-US, en'
6060
};
6161

62-
context.plugins.graphql.query(query, headers)
62+
context.plugins.graphql.query({ query, variables: { id: 4 } }, headers)
6363
.then(res => { ... })
6464
.catch(err => { ... })
6565
````

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ require('isomorphic-fetch'); // eslint-disable-line global-require
77

88
let client;
99

10+
const mergeHeaderArguments = (options, headers) =>
11+
_.merge(options, { variables: { __headers: headers } });
12+
1013
module.exports.register = (opts, dependencies, next) => { // eslint-disable-line consistent-return
1114
if (opts.batchInterval && !_.isInteger(opts.batchInterval)) {
1215
return next(new Error('The batchInterval parameter is invalid'));
@@ -35,6 +38,6 @@ module.exports.register = (opts, dependencies, next) => { // eslint-disable-line
3538
};
3639

3740
module.exports.execute = () => ({
38-
query: (query, headers) => client.query({ query, variables: { __headers: headers } }),
41+
query: (options, headers) => client.query(mergeHeaderArguments(options, headers)),
3942
queryBuilder: gql,
4043
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "oc-graphql-client",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "OpenComponents Apollo client plugin for GraphQL",
55
"main": "index.js",
66
"author": "Chris Cartlidge <[email protected]>",

tests/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ const sinon = require('sinon');
44

55
describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
66
// Mocking constructors is messy.
7+
const queryMock = sinon.stub();
78
const apollo = sinon.stub({ ApolloClient: () => { } }, 'ApolloClient').returns({
8-
query: () => { },
9+
query: queryMock,
910
});
1011

1112
const mockCreateBatchingNetworkInterface = sinon
@@ -99,4 +100,20 @@ describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
99100
expect(client).to.have.property('queryBuilder');
100101
});
101102
});
103+
describe('when calling query with headers', () => {
104+
105+
beforeEach((done) => {
106+
plugin.register({ batchInterval: 25, serverUrl: 'http://graphql' }
107+
, {}, () => {
108+
const client = plugin.execute();
109+
client.query({ query: {}, variables: { test: 1 } }, { 'accept-language': 'en-US' });
110+
done();
111+
});
112+
});
113+
114+
it('should call apollo client with merged headers', () => {
115+
const expectedResult = { query: {}, variables: { test: 1, __headers: { 'accept-language': 'en-US' } } };
116+
expect(queryMock.calledWith(expectedResult)).to.be.true;
117+
});
118+
});
102119
});

0 commit comments

Comments
 (0)