Skip to content

Commit

Permalink
Variables can now be passed.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscartlidge committed Mar 7, 2017
1 parent 53503f9 commit e22726d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ Example for a components' server.js:
module.exports.data = function(context, callback){
const qb = context.plugins.graphql.queryBuilder;

const query = qb`restaurant(id: 4) {
const query = qb`restaurant(id: $id) {
name
}`;

const headers = {
'accept-language': 'en-US, en'
};

context.plugins.graphql.query(query, headers)
context.plugins.graphql.query({ query, variables: { id: 4 } }, headers)
.then(res => { ... })
.catch(err => { ... })
````
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ require('isomorphic-fetch'); // eslint-disable-line global-require

let client;

const mergeHeaderArguments = (options, headers) =>
_.merge(options, { variables: { __headers: headers } });

module.exports.register = (opts, dependencies, next) => { // eslint-disable-line consistent-return
if (opts.batchInterval && !_.isInteger(opts.batchInterval)) {
return next(new Error('The batchInterval parameter is invalid'));
Expand Down Expand Up @@ -35,6 +38,6 @@ module.exports.register = (opts, dependencies, next) => { // eslint-disable-line
};

module.exports.execute = () => ({
query: (query, headers) => client.query({ query, variables: { __headers: headers } }),
query: (options, headers) => client.query(mergeHeaderArguments(options, headers)),
queryBuilder: gql,
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oc-graphql-client",
"version": "2.0.0",
"version": "2.1.0",
"description": "OpenComponents Apollo client plugin for GraphQL",
"main": "index.js",
"author": "Chris Cartlidge <ccartlidge@opentable.com>",
Expand Down
19 changes: 18 additions & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const sinon = require('sinon');

describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
// Mocking constructors is messy.
const queryMock = sinon.stub();
const apollo = sinon.stub({ ApolloClient: () => { } }, 'ApolloClient').returns({
query: () => { },
query: queryMock,
});

const mockCreateBatchingNetworkInterface = sinon
Expand Down Expand Up @@ -99,4 +100,20 @@ describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
expect(client).to.have.property('queryBuilder');
});
});
describe('when calling query with headers', () => {

beforeEach((done) => {
plugin.register({ batchInterval: 25, serverUrl: 'http://graphql' }
, {}, () => {
const client = plugin.execute();
client.query({ query: {}, variables: { test: 1 } }, { 'accept-language': 'en-US' });
done();
});
});

it('should call apollo client with merged headers', () => {
const expectedResult = { query: {}, variables: { test: 1, __headers: { 'accept-language': 'en-US' } } };
expect(queryMock.calledWith(expectedResult)).to.be.true;
});
});
});

0 comments on commit e22726d

Please sign in to comment.