Skip to content

Commit

Permalink
fix(core): operators.type subject gets prefixed with graphql type
Browse files Browse the repository at this point in the history
fixes: #15
  • Loading branch information
binier committed Oct 28, 2020
1 parent 6410053 commit 97b6cfd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/directive/directive-builder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Transformer } from '../utils';
import { LogicalOperatorBuilder, OperatorBuilder } from '../operator';
import { OpBuilderTypes } from '../operator';
import { DirectiveArgs, Directive } from './directive';
import { extractRefs } from '../ref';

export interface DirectiveBuilderArgs {
filter: LogicalOperatorBuilder | OperatorBuilder;
filter: OpBuilderTypes;
cascade: undefined;
ignoreReflex: undefined;
}
Expand Down
16 changes: 9 additions & 7 deletions packages/core/src/edge/edge-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
OperatorBuilder,
LogicalOperatorBuilder,
OpArg,
LogicalOperator,
Operator,
OpBuilderTypes,
BuiltOpTypes,
} from '../operator';
import { ParamBuilder, paramNameGen, ParamNameGen } from '../param';
import { Edge } from './edge';
Expand All @@ -23,7 +27,6 @@ import {
} from '../field';
import { AggregationBuilder } from '../aggregation';

type OpBuilders = OperatorBuilder | LogicalOperatorBuilder;
export type RawProjection = GenericRawProjection<
EdgeBuilder | FieldBuilder | AggregationBuilder
>;
Expand Down Expand Up @@ -198,9 +201,8 @@ export class EdgeBuilder extends FieldBuilder {
}

protected buildOp<
T extends OpBuilders,
R extends ReturnType<T['build']>
>(op: T, nameGen: NameGenerators): R {
T extends OpBuilderTypes,
>(op: T, nameGen: NameGenerators): BuiltOpTypes {
if (op instanceof OperatorBuilder) {
return op.build(args => ({
...args,
Expand All @@ -209,17 +211,17 @@ export class EdgeBuilder extends FieldBuilder {
? this.buildParam(args.arg, nameGen.param)
: args.arg as OpArg,
subject: args.subject ? this.keyToField(args.subject) : undefined,
})) as R;
}));
} else if (op instanceof LogicalOperatorBuilder) {
return op.build(args => ({
...args,
operators: args.operators.map(x => this.buildOp(x, nameGen)),
})) as R;
}));
}
throw Error('invalid `op`');
}

filter(opBuilder: OpBuilders) {
filter(opBuilder: OpBuilderTypes) {
this.directives.filter = new DirectiveBuilder('filter', opBuilder);
return this;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/operator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@ export * from './operator';
export * from './operator-builder';
export * from './logical-operator';
export * from './logical-operator-builder';

import { Operator } from './operator';
import { OperatorBuilder } from './operator-builder';
import { LogicalOperator } from './logical-operator';
import { LogicalOperatorBuilder } from './logical-operator-builder';

export type OpBuilderTypes = OperatorBuilder | LogicalOperatorBuilder;
export type BuiltOpTypes = Operator | LogicalOperator;
export type OpTypes = OpBuilderTypes | BuiltOpTypes;
4 changes: 2 additions & 2 deletions packages/core/src/operator/logical-operator-builder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { LogicalOperator, LogicalOperatorArgs } from './logical-operator';
import { Transformer } from '../utils';
import { OperatorBuilder } from './operator-builder';
import { extractRefs } from '../ref';
import type { OpBuilderTypes } from './';

export interface LogicalOperatorBuilderArgs
extends Omit<LogicalOperatorArgs, 'operators'>
{
operators: (OperatorBuilder | LogicalOperatorBuilder)[];
operators: OpBuilderTypes[];
}

type BuildTransformer = Transformer<
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/operator/operator-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class OperatorBuilder {
}

build(transform: Transformer<OperatorBuilderArgs, OperatorArgs>) {
return new Operator(transform(this.args));
const op = new Operator(transform(this.args));
// `type` operator shouldn't get prefixed with graphql type
if (this.args.name === 'type')
op.subject = this.args.subject;
return op;
}
}
6 changes: 6 additions & 0 deletions packages/core/test/operators/raw-value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ describe('Operator test - Raw value', () => {
).toMatch(/type\(User\)/)
});

it('operator: `type` shouldn\'t prefix by graphql type', () => {
expect(
edge('gqlType', {}).filter(type('User')).toString()
).toMatch(/type\(User\)/);
});

it('operator: `uid`', () => {
expect(
edge({}).filter(uid('0x2')).toString()
Expand Down

0 comments on commit 97b6cfd

Please sign in to comment.