graphql api tested & user resolver, schema and mongo interfaces tested

This commit is contained in:
2025-11-14 10:30:01 +03:00
parent 053586c5cc
commit 45f6b7a1ef
26 changed files with 1211 additions and 489 deletions

View File

@@ -0,0 +1,37 @@
import { Person } from "@/models/People";
import { GraphQLError } from "graphql";
export const personResolvers = {
Query: {
persons: async () => {
try {
return await Person.find();
} catch (err: any) {
throw new GraphQLError(err.message);
}
},
person: async (_: any, { id }: { id: string }) => {
try {
const person = await Person.findById(id);
if (!person) throw new GraphQLError("Person not found");
return person;
} catch (err: any) {
throw new GraphQLError(err.message);
}
},
},
Mutation: {
createPerson: async (_: any, { input }: any) => {
try {
const person = await Person.create({
...input,
birthDate: new Date(input.birthDate),
});
return person;
} catch (err: any) {
throw new GraphQLError(err.message);
}
},
},
};

View File

@@ -1,19 +1,22 @@
// graphql/resolvers/userResolvers.ts
import { connectDB } from '@/lib/mongodb';
import User from '@/models/User';
import { Users, IUser } from '@/models/Users';
import { Types } from 'mongoose';
export const userResolvers = {
Query: {
users: async () => {
await connectDB();
const users = await User.find().lean();
const users = await Users.find().populate("person").lean();
return users;
},
},
Mutation: {
addUser: async (_: any, { name, email }: { name: string; email: string }) => {
createUser: async (parent: any, args: { input: IUser }) => {
await connectDB();
const user = new User({ name, email });
const user = new Users({
...args.input,
person: new Types.ObjectId(args.input.person),
});
await user.save();
return user;
},