Skip to content

Commit

Permalink
Notification graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
BoogieMonster1O1 committed Jun 26, 2024
1 parent b81b928 commit 43a4823
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Sources/App/GraphQL/Relation/Notification+Resolver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Notification+Resolver.swift
//
//
// Created by Shrish Deshpande on 24/06/24.
//

import Vapor
import Fluent
import Graphiti

extension Notification {
func getTargetUser(request: Request, arguments: NoArguments) async throws -> RegisteredUser {
return try await self.$targetUser.get(on: request.db)
}

func getReferenceUser(request: Request, arguments: NoArguments) async throws -> RegisteredUser? {
return try await self.$referenceUser.get(on: request.db)
}

func getReferencePost(request: Request, arguments: NoArguments) async throws -> Post? {
return try await self.$referencePost.get(on: request.db)
}
}
4 changes: 4 additions & 0 deletions Sources/App/GraphQL/Relation/RegisteredUser+Resolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ extension RegisteredUser {
let token = try await getAndVerifyAccessToken(req: request)
return try await self.$followers.isAttached(toID: token.id, on: request.db)
}

func getNotifications(request: Request, arguments: NoArguments) async throws -> [Notification] {
return try await self.$notifications.query(on: request.db).all()
}
}
16 changes: 16 additions & 0 deletions Sources/App/GraphQL/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ let schema = try! Graphiti.Schema<Resolver, Request> {
Scalar(UUID.self)
Scalar(Date.self)

Enum(Notification.NotificationType.self) {
Value(.follow)
Value(.like)
Value(.comment)
Value(.mention)
}

Type(UnregisteredUser.self) {
Field("collegeId", at: \.id)
Field("name", at: \.name)
Expand Down Expand Up @@ -105,6 +112,15 @@ let schema = try! Graphiti.Schema<Resolver, Request> {
Field("items", at: \.items)
Field("metadata", at: \.metadata)
}

Type(Notification.self) {
Field("id", at : \.id)
Field("targetUser", at: Notification.getTargetUser)
Field("referenceUser", at: Notification.getReferenceUser)
Field("referencePost", at: Notification.getReferencePost)
Field("createdAt", at: \.createdAt?.timeIntervalSince1970)
Field("type", at: \.type)
}

Query {
Field("users", at: Resolver.getAllRegisteredUsers)
Expand Down
4 changes: 4 additions & 0 deletions Sources/App/Models/RegisteredUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public final class RegisteredUser: Model, Content {
@Siblings(through: LikedConfession.self, from: \.$user, to: \.$confession)
var likedConfessions: [Confession]

/// List of notifications
@Children(for: \.$targetUser)
var notifications: [Notification]

public init() { }

public init(collegeId: String, name: String, phone: String, email: String, personalEmail: String? = nil, branch: String, gender: String, pronouns: String? = nil, bio: String? = nil, intakeYear: Int, id: Int? = nil) {
Expand Down

0 comments on commit 43a4823

Please sign in to comment.