Skip to content

Commit

Permalink
feat(client): modify Request to accept Query
Browse files Browse the repository at this point in the history
re #2
  • Loading branch information
binier committed Jul 15, 2020
1 parent b0565e8 commit ce26e57
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from 'dgraph-js';
export { Txn } from './txn';
export { Request } from './request';
32 changes: 32 additions & 0 deletions packages/client/src/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Request } from 'dgraph-js';
import { paramsToObj } from './common';
import { Query } from './types';

declare module 'dgraph-js/generated/api_pb' {
export interface Request {
_setQuery: (query: string) => void;

setQuery(query: Query | string): void;
setQueryWithVars(query: Query | string, vars?: Record<string, string>): void;
}
}

Request.prototype._setQuery = Request.prototype.setQuery;

Request.prototype.setQuery = function (query) {
return typeof query === 'string'
? this._setQuery(query)
: this.setQueryWithVars(query, {});
}

Request.prototype.setQueryWithVars = function (query, vars = {}) {
this._setQuery(query.toString());

const varsMap = this.getVarsMap();
const params = typeof query === 'string' ? vars
: { ...paramsToObj(query.params()), ...vars };

Object.keys(params).forEach(k => varsMap.set(k, params[k]));
}

export { Request };

0 comments on commit ce26e57

Please sign in to comment.