graphql api tested & user resolver, schema and mongo interfaces tested
This commit is contained in:
37
graphql/resolvers/personResolvers.ts
Normal file
37
graphql/resolvers/personResolvers.ts
Normal 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);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -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;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user