Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions quotevote-backend/__tests__/unit/graphql-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
GraphQLSchema,
GraphQLString,
printSchema,
GraphQLEnumType,
} from 'graphql';
import RosterMock from '~/data/models/Roster';
import type { GraphQLContext } from '~/types/graphql';
import {
DateScalar,
domainTypeDefs,
Expand Down Expand Up @@ -54,6 +57,18 @@ import {
VoteType,
} from '~/data/types';

jest.mock('~/data/models/Roster');
jest.mock('~/data/models/User');
jest.mock('~/data/models/Post');
jest.mock('~/data/models/Comment');
jest.mock('~/data/models/Vote');
jest.mock('~/data/models/Quote');
jest.mock('~/data/models/MessageRoom');
jest.mock('~/data/models/Message');
jest.mock('~/data/models/Typing');
jest.mock('~/data/models/Reaction');
jest.mock('~/data/models/Presence');

const LEGACY_TYPE_NAMES = [
'Activity',
'Activities',
Expand Down Expand Up @@ -194,6 +209,16 @@ describe('GraphQL domain typedefs (7.28 migration)', () => {
'TypingResponse',
'PresenceStatus',
'RosterStatus',
'AccountStatus',
'VoteType',
'MessageType',
'NotificationType',
'ActivityEventType',
'GroupPrivacy',
'InviteStatus',
'ReportStatus',
'ReportSeverity',
'ReportReason',
'JSON',
'Date',
]) {
Expand All @@ -214,4 +239,72 @@ describe('GraphQL domain typedefs (7.28 migration)', () => {
expect(printSchema(roundTripped)).toContain('type Post');
});
});

describe('runtime enum values and resolvers validation', () => {
it('verifies GraphQLEnumType value mappings match constants', () => {
const typeMap = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: { _placeholder: { type: GraphQLString } },
}),
types: [...domainTypes],
}).getTypeMap();

// Check PresenceStatusEnum
const presenceEnum = typeMap.PresenceStatus as GraphQLEnumType;
expect(presenceEnum).toBeDefined();
expect(presenceEnum.getValue('online').value).toBe('online');
expect(presenceEnum.getValue('away').value).toBe('away');

// Check AccountStatusEnum
const accountEnum = typeMap.AccountStatus as GraphQLEnumType;
expect(accountEnum).toBeDefined();
expect(accountEnum.getValue('suspended').value).toBe('suspended');
expect(accountEnum.getValue('pending').value).toBe('pending');

// Check MessageTypeEnum
const messageEnum = typeMap.MessageType as GraphQLEnumType;
expect(messageEnum).toBeDefined();
expect(messageEnum.getValue('SYSTEM').value).toBe('SYSTEM');

// Check NotificationTypeEnum
const notificationEnum = typeMap.NotificationType as GraphQLEnumType;
expect(notificationEnum).toBeDefined();
expect(notificationEnum.getValue('SYSTEM').value).toBe('SYSTEM');
expect(notificationEnum.getValue('VOTE').value).toBe('VOTE');
});

it('verifies relationship resolvers correctly fetch and filter database models', async () => {
const findSpy = jest.spyOn(RosterMock, 'find').mockImplementation((() => ({
lean: () => Promise.resolve([{ _id: 'roster1', userId: 'user1', buddyId: 'user2' }])
})) as unknown as typeof RosterMock.find);

const groupFields = GroupType.getFields();
expect(groupFields.rosters).toBeDefined();
expect(groupFields.rosters.resolve).toBeInstanceOf(Function);

const fakeGroup = {
creatorId: 'creator123',
allowedUserIds: ['user123', 'user456'],
};

const resolveFn = groupFields.rosters.resolve as (
source: unknown,
args: Record<string, unknown>,
context: GraphQLContext,
info: unknown
) => unknown;

const result = await resolveFn(fakeGroup, {}, {} as GraphQLContext, {});
expect(findSpy).toHaveBeenCalledWith({
$or: [
{ userId: { $in: ['creator123', 'user123', 'user456'] } },
{ buddyId: { $in: ['creator123', 'user123', 'user456'] } },
]
});
expect(result).toEqual([{ _id: 'roster1', userId: 'user1', buddyId: 'user2' }]);

findSpy.mockRestore();
});
});
});
34 changes: 28 additions & 6 deletions quotevote-backend/app/data/types/Activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import { PostType } from './Post';
import { VoteType } from './Vote';
import { QuoteType } from './Quote';
import { CommentType } from './Comment';
import { ActivityEventTypeEnum } from './enums';

import User from '../models/User';
import Post from '../models/Post';
import Vote from '../models/Vote';
import Quote from '../models/Quote';
import Comment from '../models/Comment';

interface ActivityShape extends Common.Activity {
post?: Common.Post;
Expand All @@ -25,18 +32,33 @@ export const ActivityType: GraphQLObjectType<ActivityShape, GraphQLContext> = ne
fields: (): GraphQLFieldConfigMap<ActivityShape, GraphQLContext> => ({
_id: { type: GraphQLString },
created: { type: DateScalar },
activityType: { type: GraphQLString },
activityType: { type: ActivityEventTypeEnum },
postId: { type: GraphQLString },
post: { type: PostType },
post: {
type: PostType,
resolve: (act) => act.post ?? (act.postId ? Post.findById(act.postId).lean() : null),
},
voteId: { type: GraphQLString },
vote: { type: VoteType },
vote: {
type: VoteType,
resolve: (act) => act.vote ?? (act.voteId ? Vote.findById(act.voteId).lean() : null),
},
quoteId: { type: GraphQLString },
quote: { type: QuoteType },
quote: {
type: QuoteType,
resolve: (act) => act.quote ?? (act.quoteId ? Quote.findById(act.quoteId).lean() : null),
},
commentId: { type: GraphQLString },
comment: { type: CommentType },
comment: {
type: CommentType,
resolve: (act) => act.comment ?? (act.commentId ? Comment.findById(act.commentId).lean() : null),
},
content: { type: GraphQLString },
userId: { type: GraphQLString },
user: { type: UserType },
user: {
type: UserType,
resolve: (act) => act.user ?? User.findById(act.userId).lean(),
},
}),
});

Expand Down
13 changes: 12 additions & 1 deletion quotevote-backend/app/data/types/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import type { GraphQLContext } from '~/types/graphql';
import type * as Common from '~/types/common';
import { DateScalar } from './scalars';
import { UserType } from './User';
import { PostType } from './Post';

import User from '../models/User';
import Post from '../models/Post';

export const CommentType: GraphQLObjectType<Common.Comment, GraphQLContext> = new GraphQLObjectType<
Common.Comment,
Expand All @@ -27,7 +31,14 @@ export const CommentType: GraphQLObjectType<Common.Comment, GraphQLContext> = ne
url: { type: GraphQLString },
reaction: { type: GraphQLString },
deleted: { type: GraphQLBoolean },
user: { type: UserType },
user: {
type: UserType,
resolve: (comment) => (comment as Common.Comment & { user?: Common.User }).user ?? User.findById(comment.userId).lean(),
},
post: {
type: PostType,
resolve: (comment) => Post.findById(comment.postId).lean(),
},
}),
});

Expand Down
34 changes: 33 additions & 1 deletion quotevote-backend/app/data/types/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {
import type { GraphQLContext } from '~/types/graphql';
import type * as Common from '~/types/common';
import { UserType } from './User';
import { RosterType } from './Roster';
import { GroupPrivacyEnum } from './enums';

import User from '../models/User';
import Roster from '../models/Roster';

interface GroupShape extends Common.Group {
pendingUsers?: Common.User[];
Expand All @@ -32,7 +37,7 @@ export const GroupType: GraphQLObjectType<GroupShape, GraphQLContext> = new Grap
resolve: (g) => g.description ?? '',
},
url: { type: new GraphQLNonNull(GraphQLString), resolve: (g) => g.url ?? '' },
privacy: { type: new GraphQLNonNull(GraphQLString) },
privacy: { type: new GraphQLNonNull(GroupPrivacyEnum) },
allowedUserIds: {
type: new GraphQLList(new GraphQLNonNull(GraphQLString)),
resolve: (g) => g.allowedUserIds ?? [],
Expand All @@ -45,6 +50,33 @@ export const GroupType: GraphQLObjectType<GroupShape, GraphQLContext> = new Grap
type: new GraphQLList(UserType),
resolve: (g) => g.pendingUsers ?? [],
},
creator: {
type: UserType,
resolve: (group) => User.findById(group.creatorId).lean(),
},
adminUsers: {
type: new GraphQLList(UserType),
resolve: (group) => User.find({ _id: { $in: group.adminIds ?? [] } }).lean(),
},
allowedUsers: {
type: new GraphQLList(UserType),
resolve: (group) => User.find({ _id: { $in: group.allowedUserIds ?? [] } }).lean(),
},
rosters: {
type: new GraphQLList(RosterType),
resolve: (group) => {
const memberIds = [
group.creatorId,
...(group.allowedUserIds ?? []),
];
return Roster.find({
$or: [
{ userId: { $in: memberIds } },
{ buddyId: { $in: memberIds } },
],
}).lean();
},
},
}),
});

Expand Down
28 changes: 26 additions & 2 deletions quotevote-backend/app/data/types/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import type { GraphQLContext } from '~/types/graphql';
import type * as Common from '~/types/common';
import { DateScalar } from './scalars';
import { UserType } from './User';
import { MessageRoomType } from './MessageRoom';
import { ReactionType } from './Reaction';
import { PresenceType } from './Presence';
import { MessageTypeEnum } from './enums';

import User from '../models/User';
import MessageRoom from '../models/MessageRoom';
import Reaction from '../models/Reaction';
import Presence from '../models/Presence';

interface MessageShape extends Common.Message {
userAvatar?: string;
Expand Down Expand Up @@ -46,10 +55,25 @@ export const MessageType: GraphQLObjectType<MessageShape, GraphQLContext> = new
title: { type: GraphQLString },
text: { type: GraphQLString },
created: { type: DateScalar },
type: { type: GraphQLString },
type: { type: MessageTypeEnum },
mutation_type: { type: GraphQLString },
deleted: { type: GraphQLBoolean },
user: { type: UserType },
user: {
type: UserType,
resolve: (msg) => (msg as Common.Message & { user?: Common.User }).user ?? User.findById(msg.userId).lean(),
},
messageRoom: {
type: MessageRoomType,
resolve: (msg) => MessageRoom.findById(msg.messageRoomId).lean(),
},
reactions: {
type: new GraphQLList(ReactionType),
resolve: (msg) => Reaction.find({ messageId: msg._id }).lean(),
},
presence: {
type: PresenceType,
resolve: (msg) => Presence.findOne({ userId: msg.userId }).lean(),
},
readBy: { type: new GraphQLList(GraphQLString), resolve: (m) => m.readBy ?? [] },
readByDetailed: {
type: new GraphQLList(ReadByDetailedEntryType),
Expand Down
25 changes: 23 additions & 2 deletions quotevote-backend/app/data/types/MessageRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import type { GraphQLContext } from '~/types/graphql';
import type * as Common from '~/types/common';
import { DateScalar, JSONScalar } from './scalars';
import { MessageType } from './Message';
import { UserType } from './User';
import { TypingIndicatorType } from './TypingIndicator';
import { PostType } from './Post';
import { MessageTypeEnum } from './enums';

import User from '../models/User';
import Message from '../models/Message';
import Typing from '../models/Typing';
import Post from '../models/Post';

interface PostDetailsShape {
_id?: string;
Expand Down Expand Up @@ -45,7 +54,7 @@ export const MessageRoomType: GraphQLObjectType<MessageRoomShape, GraphQLContext
fields: (): GraphQLFieldConfigMap<MessageRoomShape, GraphQLContext> => ({
_id: { type: new GraphQLNonNull(GraphQLID) },
users: { type: new GraphQLList(GraphQLString), resolve: (r) => r.users ?? [] },
messageType: { type: GraphQLString },
messageType: { type: MessageTypeEnum },
created: { type: DateScalar },
lastActivity: { type: DateScalar },
lastMessageTime: { type: DateScalar },
Expand All @@ -55,9 +64,21 @@ export const MessageRoomType: GraphQLObjectType<MessageRoomShape, GraphQLContext
postId: { type: GraphQLString },
messages: {
type: new GraphQLList(MessageType),
resolve: (r) => r.messages ?? [],
resolve: (r) => r.messages ?? Message.find({ messageRoomId: r._id }).sort({ created: 1 }).lean(),
},
postDetails: { type: PostDetailsType },
usersData: {
type: new GraphQLList(UserType),
resolve: (r) => User.find({ _id: { $in: r.users ?? [] } }).lean(),
},
typingIndicators: {
type: new GraphQLList(TypingIndicatorType),
resolve: (r) => Typing.find({ messageRoomId: r._id }).lean(),
},
post: {
type: PostType,
resolve: (r) => Post.findById(r.postId).lean(),
},
}),
});

Expand Down
20 changes: 17 additions & 3 deletions quotevote-backend/app/data/types/Notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type * as Common from '~/types/common';
import { DateScalar } from './scalars';
import { UserType } from './User';
import { PostType } from './Post';
import { NotificationTypeEnum } from './enums';

import User from '../models/User';
import Post from '../models/Post';

interface NotificationShape extends Common.Notification {
userBy?: Common.User;
Expand All @@ -18,9 +22,19 @@ export const NotificationType: GraphQLObjectType<NotificationShape, GraphQLConte
_id: { type: GraphQLString },
userId: { type: GraphQLString },
userIdBy: { type: GraphQLString },
userBy: { type: UserType },
post: { type: PostType },
notificationType: { type: GraphQLString },
userBy: {
type: UserType,
resolve: (notif) => notif.userBy ?? User.findById(notif.userIdBy).lean(),
},
user: {
type: UserType,
resolve: (notif) => User.findById(notif.userId).lean(),
},
post: {
type: PostType,
resolve: (notif) => notif.post ?? (notif.postId ? Post.findById(notif.postId).lean() : null),
},
notificationType: { type: NotificationTypeEnum },
label: { type: GraphQLString },
status: { type: GraphQLString },
created: { type: DateScalar },
Expand Down
Loading
Loading