Skip to content

Commit

Permalink
Graph connection (#9)
Browse files Browse the repository at this point in the history
* field resolver for participants in collection

* WIP: participant =<> collection

* forwaerdRef removed

* participant retrieved in collection

* participant -> shares

* collection -> shares

* github action
  • Loading branch information
Snafkin547 authored Aug 31, 2023
1 parent 6a0170d commit 41bfc06
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Build & Push Docker image
uses: docker/build-push-action@v4
with:
context: /packages/server
context: ./packages/server
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
12 changes: 11 additions & 1 deletion packages/server/src/collection/collection.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
import mongoose, { Document } from 'mongoose';
import { ObjectType, Field, ID, Directive } from '@nestjs/graphql';
import { Participant } from '../participant/participant.model'
import { Shares } from '../share/share.model'

@Schema()
@ObjectType()
Expand Down Expand Up @@ -52,10 +54,18 @@ export class Collection {
@Prop( {type: mongoose.Schema.Types.Date} )
@Field(() => Date)
lastOpenedAt: Date;

@Prop()
@Field({ nullable: true })
deletedAt: Date

@Prop()
@Field(() => [Participant])
participants: Participant[]

@Prop()
@Field(() => [Shares])
Shares: Shares[]
}

export type CollectionDocument = Collection & Document;
Expand Down
7 changes: 6 additions & 1 deletion packages/server/src/collection/collection.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { MongooseModule } from '@nestjs/mongoose';
import { Collection, CollectionSchema } from './collection.model'
import { CollectionService } from './collection.service'
import { CollectionResolver } from './collection.resolver';
import { ParticipantModule } from '../participant/participant.module'
import { ShareModule } from '../share/share.module'

@Module({
imports: [MongooseModule.forFeature([{ name: Collection.name, schema: CollectionSchema}])],
imports: [
ShareModule,
ParticipantModule,
MongooseModule.forFeature([{ name: Collection.name, schema: CollectionSchema}])],
providers: [CollectionService, CollectionResolver],
exports: [CollectionService]
})
Expand Down
22 changes: 20 additions & 2 deletions packages/server/src/collection/collection.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { Resolver, Query, Mutation, ResolveReference, Args } from '@nestjs/graphql';
import { Resolver, Query, Mutation, ResolveReference, ResolveField, Parent, Args } from '@nestjs/graphql';
import { Collection } from './collection.model';
import { CollectionService } from './collection.service';
import { BadRequestException } from '@nestjs/common';
import { CollectionType } from './collection.type'
import { PageInfoInput, PaginatedCollections} from './collection.pageinfo'
import { Participant } from '../participant/participant.model'
import { ParticipantService } from '../participant/participant.service'
import { Shares } from '../share/share.model'
import { ShareService } from '../share/share.service'

@Resolver(()=> Collection)
export class CollectionResolver {
constructor(private readonly collectionService: CollectionService) {}
constructor(
private readonly participantService: ParticipantService,
private readonly collectionService: CollectionService,
private readonly shareService:ShareService
) {}

@Query(() => PaginatedCollections)
async getAllCollections(@Args('input') input: PageInfoInput): Promise<PaginatedCollections> {
Expand All @@ -33,6 +41,16 @@ export class CollectionResolver {
return this.collectionService.delete(id);
}

@ResolveField(() => [Participant])
async participants(@Parent() collection: Collection): Promise<Participant[]> {
return this.participantService.findByCollectionId(collection._id.toString());
}

@ResolveField(() => [Shares])
async shares(@Parent() collection: Collection): Promise<Shares[]> {
return this.shareService.getCollectionShares(collection._id.toString());
}

@ResolveReference()
async resolveReference(reference: {
__typename: string;
Expand Down
5 changes: 5 additions & 0 deletions packages/server/src/participant/participant.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
import mongoose, { Document } from 'mongoose';
import { ObjectType, Field, ID, Directive } from '@nestjs/graphql';
import { Shares } from '../share/share.model'

@Schema()
@ObjectType()
Expand All @@ -23,6 +24,10 @@ export class Participant {
@Prop()
@Field({ nullable: true })
deletedAt: Date

@Prop()
@Field(() => [Shares])
Shares: Shares[]
}

export type ParticipantDocument = Participant & Document;
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/participant/participant.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { Participant, ParticipantSchema } from './participant.model';
import { ParticipantService } from './participant.service';
import { ParticipantResolver } from './participant.resolver';
import { ParticipantController } from './particpant.controller';
import { ShareModule } from '../share/share.module'

@Module({
imports: [
ShareModule,
MongooseModule.forFeature([
{ name: Participant.name, schema: ParticipantSchema },
]),
Expand Down
16 changes: 14 additions & 2 deletions packages/server/src/participant/participant.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ import {
Resolver,
Query,
ResolveReference,
Mutation,
Mutation,
ResolveField,
Parent,
Args,
} from '@nestjs/graphql';
import { Participant } from './participant.model';
import { ParticipantService } from './participant.service';
import { BadRequestException } from '@nestjs/common';
import { ParticipantsArgs } from '../dto/participants.input';
import { CreateParticipantDto } from '../dto/participant.dto';
import { Shares } from '../share/share.model'
import { ShareService } from '../share/share.service'

@Resolver(() => Participant)
export class ParticipantResolver {
constructor(private readonly participantService: ParticipantService) {}
constructor(
private readonly participantService: ParticipantService,
private readonly shareService:ShareService
) {}

@Query(() => [Participant])
async participants(@Args() args: ParticipantsArgs): Promise<Participant[]> {
Expand Down Expand Up @@ -50,6 +57,11 @@ export class ParticipantResolver {
return this.participantService.delete(id);
}

@ResolveField(() => [Shares])
async shares(@Parent() participant: Participant): Promise<Shares[]> {
return this.shareService.getParticipantShares(participant._id.toString());
}

@ResolveReference()
async resolveReference(reference: {
__typename: string;
Expand Down
6 changes: 6 additions & 0 deletions packages/server/src/participant/participant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export class ParticipantService {
.exec();
}

findByCollectionId(id: string): Promise<Participant[]> {
return this.participantModel
.find({ collectionId: id, deletedAt: null })
.exec();
}

async delete(id: string) {
const participantToDelete = await this.participantModel
.findOneAndUpdate(new mongoose.Types.ObjectId(id), {
Expand Down

0 comments on commit 41bfc06

Please sign in to comment.