Skip to content

Commit

Permalink
add mark notifications as read mutation resolver (#4169)
Browse files Browse the repository at this point in the history
  • Loading branch information
callensm authored Jun 18, 2023
1 parent 61a0ff3 commit 1511deb
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ export class Hasura {
throw new GraphQLError("Failed to create new notification");
}

return NodeBuilder.notification(resp.insert_auth_notifications_one.id, {
return NodeBuilder.notification({
body: b,
dbId: resp.insert_auth_notifications_one.id,
source,
title,
timestamp: timestamp.toISOString(),
Expand Down Expand Up @@ -268,8 +269,9 @@ export class Hasura {

// Create the list of notification type nodes for the connection
const nodes = resp.auth_notifications.map((n) =>
NodeBuilder.notification(n.id, {
NodeBuilder.notification({
body: n.body,
dbId: n.id,
source: n.xnft_id,
timestamp: new Date(n.timestamp as string).toISOString(),
title: n.title,
Expand Down
2 changes: 2 additions & 0 deletions backend/native/backpack-api/src/routes/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
friendshipTypeResolvers,
importPublicKeyMutationResolver,
jsonObjectScalar,
markNotificationsAsReadMutationResolver,
notificationTypeResolvers,
sendFriendRequestMutationResolver,
tokenListQueryResolver,
Expand All @@ -28,6 +29,7 @@ const mutationResolvers: MutationResolvers = {
authenticate: authenticateMutationResolver,
deauthenticate: deauthenticateMutationResolver,
importPublicKey: importPublicKeyMutationResolver,
markNotificationsAsRead: markNotificationsAsReadMutationResolver,
sendFriendRequest: sendFriendRequestMutationResolver,
};

Expand Down
7 changes: 2 additions & 5 deletions backend/native/backpack-api/src/routes/graphql/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ export abstract class NodeBuilder {
);
}

static notification(
dbId: unknown,
data: Omit<Notification, "id">
): Notification {
return this._createNode(`notification:${dbId}`, data);
static notification(data: Omit<Notification, "id">): Notification {
return this._createNode(`notification:${data.dbId}`, data);
}

static notificationAppData(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./authentication";
export * from "./friendship";
export * from "./notifications";
export * from "./wallets";
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { GraphQLResolveInfo } from "graphql";

import { updateNotificationSeen } from "../../../../db/notifications";
import type { ApiContext } from "../../context";
import type {
MutationMarkNotificationsAsReadArgs,
MutationResolvers,
} from "../../types";

/**
* Handler for the mutation to mark a set of notifications as viewed.
* @param {{}} _parent
* @param {MutationMarkNotificationsAsReadArgs} args
* @param {ApiContext} ctx
* @param {GraphQLResolveInfo} _info
* @returns
*/
export const markNotificationsAsReadMutationResolver: MutationResolvers["markNotificationsAsRead"] =
async (
_parent: {},
args: MutationMarkNotificationsAsReadArgs,
ctx: ApiContext,
_info: GraphQLResolveInfo
): Promise<number> => {
// TODO: move implementation into contextual Hasura client
const res = await updateNotificationSeen({
notificationIds: args.ids,
uuid: ctx.authorization.userId ?? "",
});
return res.update_auth_notifications?.affected_rows ?? 0;
};
16 changes: 16 additions & 0 deletions backend/native/backpack-api/src/routes/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ export type Mutation = {
deauthenticate: Scalars["String"];
/** Attempt to add a new wallet public key to the user account. */
importPublicKey?: Maybe<Scalars["Boolean"]>;
/** Set the `viewed` status of the argued notification IDs are `true`. */
markNotificationsAsRead: Scalars["Int"];
/** Allows users to send friend requests to another remote user. */
sendFriendRequest?: Maybe<Scalars["Boolean"]>;
};
Expand All @@ -181,6 +183,11 @@ export type MutationImportPublicKeyArgs = {
signature: Scalars["String"];
};

/** Root level mutation type. */
export type MutationMarkNotificationsAsReadArgs = {
ids: Array<Scalars["Int"]>;
};

/** Root level mutation type. */
export type MutationSendFriendRequestArgs = {
accept: Scalars["Boolean"];
Expand Down Expand Up @@ -258,6 +265,8 @@ export type Notification = Node & {
app?: Maybe<NotificationApplicationData>;
/** Arbitrary body data of the notification parsed as an object. */
body: Scalars["JSONObject"];
/** The database unique integer identifier. */
dbId: Scalars["Int"];
/** Globally unique identifier for a specific notification. */
id: Scalars["ID"];
/** The emitting source of the notification. */
Expand Down Expand Up @@ -985,6 +994,12 @@ export type MutationResolvers<
"address" | "providerId" | "signature"
>
>;
markNotificationsAsRead?: Resolver<
ResolversTypes["Int"],
ParentType,
ContextType,
RequireFields<MutationMarkNotificationsAsReadArgs, "ids">
>;
sendFriendRequest?: Resolver<
Maybe<ResolversTypes["Boolean"]>,
ParentType,
Expand Down Expand Up @@ -1092,6 +1107,7 @@ export type NotificationResolvers<
ContextType
>;
body?: Resolver<ResolversTypes["JSONObject"], ParentType, ContextType>;
dbId?: Resolver<ResolversTypes["Int"], ParentType, ContextType>;
id?: Resolver<ResolversTypes["ID"], ParentType, ContextType>;
source?: Resolver<ResolversTypes["String"], ParentType, ContextType>;
timestamp?: Resolver<ResolversTypes["String"], ParentType, ContextType>;
Expand Down
10 changes: 10 additions & 0 deletions backend/native/backpack-api/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ type Mutation {
signature: String!
): Boolean

"""
Set the `viewed` status of the argued notification IDs are `true`.
"""
markNotificationsAsRead(ids: [Int!]!): Int!

"""
Allows users to send friend requests to another remote user.
"""
Expand Down Expand Up @@ -562,6 +567,11 @@ type Notification implements Node {
"""
body: JSONObject!

"""
The database unique integer identifier.
"""
dbId: Int!

"""
The emitting source of the notification.
"""
Expand Down
9 changes: 9 additions & 0 deletions packages/data-components/src/apollo/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ export type Mutation = {
deauthenticate: Scalars["String"];
/** Attempt to add a new wallet public key to the user account. */
importPublicKey?: Maybe<Scalars["Boolean"]>;
/** Set the `viewed` status of the argued notification IDs are `true`. */
markNotificationsAsRead: Scalars["Int"];
/** Allows users to send friend requests to another remote user. */
sendFriendRequest?: Maybe<Scalars["Boolean"]>;
};
Expand All @@ -175,6 +177,11 @@ export type MutationImportPublicKeyArgs = {
signature: Scalars["String"];
};

/** Root level mutation type. */
export type MutationMarkNotificationsAsReadArgs = {
ids: Array<Scalars["Int"]>;
};

/** Root level mutation type. */
export type MutationSendFriendRequestArgs = {
accept: Scalars["Boolean"];
Expand Down Expand Up @@ -252,6 +259,8 @@ export type Notification = Node & {
app?: Maybe<NotificationApplicationData>;
/** Arbitrary body data of the notification parsed as an object. */
body: Scalars["JSONObject"];
/** The database unique integer identifier. */
dbId: Scalars["Int"];
/** Globally unique identifier for a specific notification. */
id: Scalars["ID"];
/** The emitting source of the notification. */
Expand Down

1 comment on commit 1511deb

@vercel
Copy link

@vercel vercel bot commented on 1511deb Jun 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.