Skip to content

Commit

Permalink
Added a field level resolver for Users
Browse files Browse the repository at this point in the history
  • Loading branch information
Atharva-Kanherkar committed Apr 18, 2024
1 parent b2d0e8d commit 200066a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/resolvers/AgendaItem/Users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { AgendaItemResolvers } from "../../types/generatedGraphQLTypes";
import { User } from "../../models";

export const users: AgendaItemResolvers["users"] = async (parent) => {
const userIds = parent.users; // Assuming parent.users is an array of user ids
const users = await User.find({ _id: { $in: userIds } }); // Assuming User.find() returns a promise
return users;
};
2 changes: 2 additions & 0 deletions src/resolvers/AgendaItem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { createdBy } from "./createdBy";
import { updatedBy } from "./updatedBy";
import { relatedEvent } from "./relatedEvent";
import { categories } from "./categories";
import { users } from "./Users";

export const AgendaItem: AgendaItemResolvers = {
organization,
createdBy,
updatedBy,
relatedEvent,
categories,
users,
};
76 changes: 76 additions & 0 deletions tests/resolvers/AgendaItem/Users.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type mongoose from "mongoose";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { AgendaItemModel, Organization, Event } from "../../../src/models";
import { users as usersResolver } from "../../../src/resolvers/AgendaItem/Users";
import type { TestAgendaItemType } from "../../helpers/agendaItem";
import { connect, disconnect } from "../../helpers/db";
import type {
TestOrganizationType,
createTestUser,
type TestUserType,
} from "../../helpers/userAndOrg";

import type { TestEventType } from "../../helpers/events";

let MONGOOSE_INSTANCE: typeof mongoose;
let testUser1: TestUserType;
let testUser2: TestUserType;
let testAgendaItem: TestAgendaItemType;
let testOrganization: TestOrganizationType;
let testEvent: TestEventType;

beforeAll(async () => {
MONGOOSE_INSTANCE = await connect();
testUser1 = await createTestUser();

Check failure on line 24 in tests/resolvers/AgendaItem/Users.spec.ts

View workflow job for this annotation

GitHub Actions / Testing Application (20.x)

tests/resolvers/AgendaItem/Users.spec.ts

ReferenceError: createTestUser is not defined ❯ tests/resolvers/AgendaItem/Users.spec.ts:24:3
testUser2 = await createTestUser();
testOrganization = await Organization.create({
name: "name",
description: "description",
isPublic: true,
creator: testUser1?._id,
admins: [testUser1?._id, testUser2?._id],
members: [testUser1?._id, testUser2?._id],
creatorId: testUser1?._id,
});
testEvent = await Event.create({
title: "title",
description: "description",
allDay: true,
startDate: new Date(),
recurring: true,
isPublic: true,
isRegisterable: true,
creator: testUser1?._id,
admins: [testUser1?._id, testUser2?._id],
registrants: [],
organization: testOrganization?._id,
creatorId: testUser1?._id,
});

testAgendaItem = await AgendaItemModel.create({
title: "Regular Agenda Item",
description: "Description for the regular agenda item",
duration: "1 hour",
relatedEventId: testEvent?._id,
sequence: 1,
itemType: "Regular",
organizationId: testOrganization?._id,
isNote: false,
createdBy: testUser1?._id,
createdAt: Date.now(),
updatedBy: testUser1?._id,
updatedAt: Date.now(),
users: [testUser1?._id, testUser2?._id], // Assuming users field is an array of user ids
});
});

afterAll(async () => {
await disconnect(MONGOOSE_INSTANCE);
});

describe("AgendaItem.Users", () => {
it("Should return the users of the agenda item", async () => {
const users = await usersResolver?.(testAgendaItem, {}, {});
expect(users).toEqual([testUser1, testUser2]);
});
});

0 comments on commit 200066a

Please sign in to comment.