Skip to content

Commit

Permalink
feat: handle empty filter and group
Browse files Browse the repository at this point in the history
  • Loading branch information
MeStrak committed May 10, 2021
1 parent 2a1b653 commit 3cebd76
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date
"""
scalar DateTime

type WhispCountGroup {
_id: JSONObject!
type WhispCount {
_id: JSONObject
count: Int!
}

Expand All @@ -77,7 +77,7 @@ type Query {
whispById(id: String!): Whisp
whisps(limit: Int, sort: JSONObject, filter: JSONObject): [Whisp!]
whispsCount(filter: JSONObject): Float!
whispCountGroup(group: JSONObject, filter: [JSONObject!]): [WhispCountGroup!]!
whispCount(group: JSONObject, filter: [JSONObject!]): [WhispCount!]!
tags(tag: TagInputType!): [Tag!]
tagById(id: String!): Tag
}
Expand Down
8 changes: 4 additions & 4 deletions src/whisp/whisp.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { DistributionService } from '../distribution/distribution.service';
import { filterPayload } from '../utils/filterPayload.service';
import { Tag } from '../tag/tag.entity';
import { TagInputType } from '../tag/tag.input';
import { WhispCountGroup } from './whispCountGroup.entity';
import { WhispCount } from './whispCount.entity';

@Resolver(() => Whisp)
export class WhispResolver {
Expand Down Expand Up @@ -54,13 +54,13 @@ export class WhispResolver {
return this.whispService.countWhisps(filter);
}

@Query(() => [WhispCountGroup])
async whispCountGroup(
@Query(() => [WhispCount])
async whispCount(
@Args('filter', { type: () => [GraphQLJSONObject], nullable: true })
filter: string[],
@Args('group', { type: () => GraphQLJSONObject, nullable: true })
group: Record<string, unknown>,
): Promise<WhispCountGroup[]> {
): Promise<WhispCount[]> {
return this.whispService.countWhispsGroup(filter, group);
}

Expand Down
7 changes: 4 additions & 3 deletions src/whisp/whisp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { WhispInputType } from './whisp.input';
import { WhispAttachment } from './whisp-attachment.entity';
import { WhispAttachmentInput } from './whisp-attachment.input';
import { TagInputType } from '../tag/tag.input';
import { WhispCountGroup } from './whispCountGroup.entity';
import { WhispCount } from './whispCount.entity';

@Injectable()
export class WhispService {
Expand Down Expand Up @@ -90,10 +90,11 @@ export class WhispService {
return this.whispModel.countDocuments(filter).exec();
}

async countWhispsGroup(filter: Partial<IWhisp>[], group: any): Promise<WhispCountGroup[]> {
async countWhispsGroup(filter?: Partial<IWhisp>[], group?: any): Promise<WhispCount[]> {
//TODO: group interface instead of using any type

const mongoMatch = { '$match': { '$or': filter } };
// match and group code simulates mongo countDocuments but allows custom group
const mongoMatch = { '$match': (filter ? { '$or': filter } : {}) };
const mongoGroup = { '$group': { '_id': group, count: { '$sum': 1 } } };

const result = this.whispModel.aggregate([mongoMatch, mongoGroup]).exec();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
import { GraphQLJSONObject } from 'graphql-type-json';

@ObjectType()
export class WhispCountGroup {
@Field(() => GraphQLJSONObject)
export class WhispCount {
@Field(() => GraphQLJSONObject, { nullable: true })
_id: any;

@Field(() => Int)
Expand Down

0 comments on commit 3cebd76

Please sign in to comment.