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

feat(core): support groupBy directive #16

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,4 @@ dist
.yarn/unplugged
.yarn/build-state.yml
.pnp.*
.idea
1 change: 1 addition & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ you can also use

Query and Edge directives:
- **cascade**
- **groupBy**

Query only directives:
- **ignoreReflex**
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/directive/directive-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { OpBuilderTypes } from '../operator';
import { DirectiveArgs, Directive } from './directive';
import { extractRefs } from '../ref';
import { RecurseBuilderArgs } from './recurse'
import { GroupByBuilderArgs } from './group-by'

export interface DirectiveBuilderArgs {
filter: [OpBuilderTypes];
cascade: undefined;
ignoreReflex: undefined;
recurse: RecurseBuilderArgs;
groupBy: GroupByBuilderArgs;
}

export class DirectiveBuilder<T extends keyof DirectiveBuilderArgs = keyof DirectiveBuilderArgs> {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/directive/directive.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { LogicalOperator, Operator } from '../operator';
import { Param } from '../param';
import { RecurseArgs } from './recurse';
import { GroupByArgs } from './group-by';

export interface DirectiveArgs {
filter: LogicalOperator | Operator;
cascade: undefined;
ignoreReflex: undefined;
recurse: RecurseArgs | undefined;
groupBy: GroupByArgs;
}

export class Directive<T extends keyof DirectiveArgs = any> {
Expand All @@ -22,6 +24,7 @@ export class Directive<T extends keyof DirectiveArgs = any> {
params(): Param[] {
const argsValues = !this.hasArgs() ? []
: Array.isArray(this.args) ? this.args
: typeof this.args!=='object'? [this.args]
: Object.entries(this.args).map(([_, value]) => value);

return argsValues.reduce((r, arg) => {
Expand All @@ -36,9 +39,10 @@ export class Directive<T extends keyof DirectiveArgs = any> {
toString() {
const argsList = !this.hasArgs() ? []
: Array.isArray(this.args) ? this.args
: typeof this.args!=='object'? [this.args]
: Object.entries(this.args).map(([key, value]) => `${key}: ${value}`);

return `@${this.name}${
return `@${this.name.toLowerCase()}${
!argsList.length ? '' : `(${argsList.join(', ')})`
}`;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/directive/group-by.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import { Param, ParamBuilder } from '../param';

/** @see https://dgraph.io/docs/query-language/groupby/ */
// predicate
export type GroupByBuilderArgs = string | ParamBuilder<'string'>

export type GroupByArgs = string | Param<'string'>;
6 changes: 6 additions & 0 deletions packages/core/src/edge/edge-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
GenericProjection,
} from './common';
import { DirectiveBuilder } from '../directive';
import { GroupByBuilderArgs } from '../directive/group-by';
import { Ref } from '../ref';
import { Runnable } from '../types';
import {
Expand Down Expand Up @@ -247,6 +248,11 @@ export class EdgeBuilder extends FieldBuilder {
return this;
}

groupBy(predicate: GroupByBuilderArgs) {
this.directives.groupBy = new DirectiveBuilder('groupBy', predicate);
return this;
}

keyToField(key: string) {
if (['id', 'uid'].includes(key))
return 'uid';
Expand Down
7 changes: 6 additions & 1 deletion packages/core/test/directive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Directive test', () => {

it('`@ignoreReflex` directive', () => {
expect(query().ignoreReflex().toString())
.toMatch(/@ignoreReflex \{/);
.toMatch(/@ignorereflex \{/);
});

it('`@recurse` directive no args', () => {
Expand Down Expand Up @@ -51,4 +51,9 @@ describe('Directive test', () => {
expect(myParams[0].getValue()).toEqual('false');
expect(myParams[1].getValue()).toEqual('5');
});

it('`@groupBy` directive', () => {
expect(edge({}).groupBy('predicate').toString())
.toMatch(/@groupby\(predicate\) \{/);
});
});