Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for querying with operationName #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ client.query(query, vars).then(result => {
});
```

### Querying With OperationName

Querying with an operationName will send the query string under an operationName param. This is useful for pre-defined queries on the server.

```js
client.queryWithOperationName(
'AllFilms',
{ after: 'cursor' }
)
```

## Cache API

Lokka has a built in cache. But it won't be used when you are invoking the core API. For that, you need to use following APIs:
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ var Lokka = function () {

return this.send(queryWithFragments, vars);
}
}, {
key: 'queryWithOperationName',
value: function queryWithOperationName(operationName, vars) {
if (!operationName) {
throw new Error('operationName is required!');
}

return this._transport.send(null, vars, operationName);
}
}, {
key: 'mutate',
value: function mutate(query, vars) {
Expand Down Expand Up @@ -179,4 +188,4 @@ var Lokka = function () {
}();

exports.Lokka = Lokka;
exports.default = Lokka;
exports.default = Lokka;
27 changes: 27 additions & 0 deletions lib/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,33 @@ describe('Lokka', () => {
});
});

describe('queryWithOperationName()', () => {
describe('called without an operationName', () => {
it('should throw an error', () => {
const lokka = new Lokka({transport: new EchoTransport()});
expect(() => lokka.queryWithOperationName()).to.throw(/operationName is required!/);
});
});

describe('called with an operationName', () => {
it('should add vars to the query', async () => {
const lokka = new Lokka({transport: new EchoTransport()});
const operationNameDef = 'AllPosts';
const vars = {after: 'cursor'};

lokka._transport.send = (q, v, oN) => {
expect(q).to.be.equal(null);
expect(v).to.deep.equal(vars);
expect(trimIt(oN)).to.be.equal(trimIt(operationNameDef));
return Promise.resolve(oN);
};

const finalQuery = await lokka.queryWithOperationName(operationNameDef, vars);
expect(trimIt(finalQuery)).to.be.equal(trimIt(operationNameDef));
});
});
});

describe('mutate()', () => {
describe('called without a query', () => {
it('should throw an error', () => {
Expand Down
8 changes: 8 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ export class Lokka {
return this.send(queryWithFragments, vars);
}

queryWithOperationName(operationName, vars) {
if (!operationName) {
throw new Error('operationName is required!');
}

return this._transport.send(null, vars, operationName);
}

mutate(query, vars) {
if (!query) {
throw new Error('query is required!');
Expand Down