Skip to content

Commit ebac77f

Browse files
antas-marcinaliszka
authored andcommitted
Add support for groupBy argument
1 parent 8828634 commit ebac77f

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/graphql/getter.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,3 +1311,22 @@ warm`,
13111311
expect(mockClient.query).toHaveBeenCalledWith(expectedQuery);
13121312
});
13131313
});
1314+
1315+
describe('groupBy valid searchers', () => {
1316+
const mockClient: any = {
1317+
query: jest.fn(),
1318+
};
1319+
1320+
test('valid groupBy', () => {
1321+
const groupByQuery = `(groupBy:{path:["property"],groups:2,objectsPerGroup:3})`;
1322+
const expectedQuery = `{Get{Person` + groupByQuery + `{name}}}`;
1323+
1324+
new Getter(mockClient)
1325+
.withClassName('Person')
1326+
.withFields('name')
1327+
.withGroupBy({ path: ['property'], groups: 2, objectsPerGroup: 3 })
1328+
.do();
1329+
1330+
expect(mockClient.query).toHaveBeenCalledWith(expectedQuery);
1331+
});
1332+
});

src/graphql/getter.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { CommandBase } from '../validation/commandBase';
1313
import { WhereFilter } from '../openapi/types';
1414
import { GenerateArgs, GraphQLGenerate } from './generate';
1515
import { ConsistencyLevel } from '../data';
16+
import GroupBy, { GroupByArgs } from './groupBy';
1617

1718
export default class GraphQLGetter extends CommandBase {
1819
private after?: string;
@@ -33,6 +34,7 @@ export default class GraphQLGetter extends CommandBase {
3334
private whereString?: string;
3435
private generateString?: string;
3536
private consistencyLevel?: ConsistencyLevel;
37+
private groupByString?: string;
3638

3739
constructor(client: Connection) {
3840
super(client);
@@ -183,6 +185,15 @@ export default class GraphQLGetter extends CommandBase {
183185
return this;
184186
};
185187

188+
withGroupBy = (args: GroupByArgs) => {
189+
try {
190+
this.groupByString = new GroupBy(args).toString();
191+
} catch (e: any) {
192+
this.addError(e.toString());
193+
}
194+
return this;
195+
};
196+
186197
validateIsSet = (prop: string | undefined | null, name: string, setter: string) => {
187198
if (prop == undefined || prop == null || prop.length == 0) {
188199
this.addError(`${name} must be set - set with ${setter}`);
@@ -267,6 +278,10 @@ export default class GraphQLGetter extends CommandBase {
267278
args = [...args, `consistencyLevel:${this.consistencyLevel}`];
268279
}
269280

281+
if (this.groupByString) {
282+
args = [...args, `groupBy:${this.groupByString}`];
283+
}
284+
270285
if (args.length > 0) {
271286
params = `(${args.join(',')})`;
272287
}

src/graphql/groupBy.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export interface GroupByArgs {
2+
path: string[];
3+
groups: number;
4+
objectsPerGroup: number;
5+
}
6+
7+
export default class GraphQLGroupBy {
8+
private args: GroupByArgs;
9+
10+
constructor(args: GroupByArgs) {
11+
this.args = args;
12+
}
13+
14+
toString() {
15+
let parts: string[] = [];
16+
17+
if (this.args.path) {
18+
parts = [...parts, `path:${JSON.stringify(this.args.path)}`];
19+
}
20+
21+
if (this.args.groups) {
22+
parts = [...parts, `groups:${this.args.groups}`];
23+
}
24+
25+
if (this.args.objectsPerGroup) {
26+
parts = [...parts, `objectsPerGroup:${this.args.objectsPerGroup}`];
27+
}
28+
29+
return `{${parts.join(',')}}`;
30+
}
31+
}

0 commit comments

Comments
 (0)