updated graphql
This commit is contained in:
parent
2e329d8dfe
commit
a67170daa8
|
|
@ -1,49 +1,76 @@
|
||||||
import { Prop } from '@nestjs/mongoose';
|
import { Prop } from '@nestjs/mongoose';
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
|
import { Field } from '@nestjs/graphql';
|
||||||
|
import { ObjectType } from '@nestjs/graphql';
|
||||||
|
|
||||||
|
@ObjectType({ isAbstract: true })
|
||||||
export class Base {
|
export class Base {
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: randomUUID, unique: true })
|
@Prop({ default: randomUUID, unique: true })
|
||||||
uuid: string;
|
uuid: string;
|
||||||
|
|
||||||
|
@Field()
|
||||||
|
@Prop({ default: () => new Date(Date.now()) })
|
||||||
|
createdAt: Date;
|
||||||
|
|
||||||
|
@Field()
|
||||||
|
@Prop({ default: () => new Date(Date.now()) })
|
||||||
|
updatedAt: Date;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: () => new Date(Date.now()) })
|
@Prop({ default: () => new Date(Date.now()) })
|
||||||
expiryStarts: Date;
|
expiryStarts: Date;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: () => new Date('2099-12-31') })
|
@Prop({ default: () => new Date('2099-12-31') })
|
||||||
expiryEnds: Date;
|
expiryEnds: Date;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: false })
|
@Prop({ default: false })
|
||||||
isConfirmed: boolean;
|
isConfirmed: boolean;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: false })
|
@Prop({ default: false })
|
||||||
deleted: boolean;
|
deleted: boolean;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: true })
|
@Prop({ default: true })
|
||||||
active: boolean;
|
active: boolean;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: randomUUID })
|
@Prop({ default: randomUUID })
|
||||||
crypUuId: string;
|
crypUuId: string;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: randomUUID })
|
@Prop({ default: randomUUID })
|
||||||
createdCredentialsToken: string;
|
createdCredentialsToken: string;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: randomUUID })
|
@Prop({ default: randomUUID })
|
||||||
updatedCredentialsToken: string;
|
updatedCredentialsToken: string;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: randomUUID })
|
@Prop({ default: randomUUID })
|
||||||
confirmedCredentialsToken: string;
|
confirmedCredentialsToken: string;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: false })
|
@Prop({ default: false })
|
||||||
isNotificationSend: boolean;
|
isNotificationSend: boolean;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: false })
|
@Prop({ default: false })
|
||||||
isEmailSend: boolean;
|
isEmailSend: boolean;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: 0 })
|
@Prop({ default: 0 })
|
||||||
refInt: number;
|
refInt: number;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: randomUUID })
|
@Prop({ default: randomUUID })
|
||||||
refId: string;
|
refId: string;
|
||||||
|
|
||||||
|
@Field()
|
||||||
@Prop({ default: 0 })
|
@Prop({ default: 0 })
|
||||||
replicationId: number;
|
replicationId: number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,9 +72,6 @@ export class User extends Base {
|
||||||
@Prop({ type: Types.ObjectId, ref: Person.name, required: true })
|
@Prop({ type: Types.ObjectId, ref: Person.name, required: true })
|
||||||
person: Types.ObjectId;
|
person: Types.ObjectId;
|
||||||
|
|
||||||
@Field(() => ID, { nullable: true })
|
|
||||||
@Prop({ type: Types.ObjectId, ref: UserType.name })
|
|
||||||
type?: Types.ObjectId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type UserDocument = User & Document;
|
export type UserDocument = User & Document;
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,11 @@ export class PeopleService {
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async findAll(projection?: any): Promise<PersonDocument[]> {
|
async findAll(projection?: any): Promise<PersonDocument[]> {
|
||||||
return this.personModel.find({}, projection, { lean: false }).populate({ path: 'person', select: projection?.person }).exec();
|
return this.personModel.find({}, projection, { lean: false }).exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
async findById(id: Types.ObjectId, projection?: any): Promise<PersonDocument | null> {
|
async findById(id: Types.ObjectId, projection?: any): Promise<PersonDocument | null> {
|
||||||
return this.personModel.findById(id, projection, { lean: false }).populate({ path: 'person', select: projection?.person }).exec();
|
return this.personModel.findById(id, projection, { lean: false }).exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(input: CreatePersonInput): Promise<PersonDocument> {
|
async create(input: CreatePersonInput): Promise<PersonDocument> {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
mutation CreatePerson {
|
||||||
|
createPerson(input: {
|
||||||
|
firstName: "John",
|
||||||
|
surname: "Doe",
|
||||||
|
middleName: "Michael",
|
||||||
|
sexCode: "M",
|
||||||
|
personRef: "REF12345",
|
||||||
|
personTag: "TAG001",
|
||||||
|
fatherName: "Robert",
|
||||||
|
motherName: "Jane",
|
||||||
|
countryCode: "US",
|
||||||
|
nationalIdentityId: "12345678901",
|
||||||
|
birthPlace: "New York",
|
||||||
|
birthDate: "1990-01-01T00:00:00.000Z",
|
||||||
|
taxNo: "987654321",
|
||||||
|
birthname: "Johnathan"
|
||||||
|
}) {
|
||||||
|
_id
|
||||||
|
firstName
|
||||||
|
surname
|
||||||
|
birthDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
query GetPerson {
|
||||||
|
Person(id: "69175eec7baea04628ad126c") {
|
||||||
|
_id
|
||||||
|
firstName
|
||||||
|
surname
|
||||||
|
middleName
|
||||||
|
sexCode
|
||||||
|
birthDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
query {
|
||||||
|
Persons {
|
||||||
|
firstName
|
||||||
|
surname
|
||||||
|
middleName
|
||||||
|
sexCode
|
||||||
|
personRef
|
||||||
|
personTag
|
||||||
|
fatherName
|
||||||
|
motherName
|
||||||
|
countryCode
|
||||||
|
nationalIdentityId
|
||||||
|
birthPlace
|
||||||
|
birthDate
|
||||||
|
taxNo
|
||||||
|
birthname
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
mutation {
|
||||||
|
createUser(input: {
|
||||||
|
password: "secret123",
|
||||||
|
history: ["login1"],
|
||||||
|
tag: "admin",
|
||||||
|
email: "test@example.com",
|
||||||
|
phone: "555123456",
|
||||||
|
collectionTokens: {
|
||||||
|
default: "default-token",
|
||||||
|
tokens: [{ prefix: "main", token: "abc123" }]
|
||||||
|
},
|
||||||
|
person: "64f8b2a4e1234567890abcdef"
|
||||||
|
}) {
|
||||||
|
_id
|
||||||
|
password
|
||||||
|
tag
|
||||||
|
email
|
||||||
|
phone
|
||||||
|
tag
|
||||||
|
collectionTokens {
|
||||||
|
default
|
||||||
|
tokens {
|
||||||
|
prefix
|
||||||
|
token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
person
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
|
import { Resolver, Query, Args, ID, Info, Mutation, Int } from '@nestjs/graphql';
|
||||||
import { Types } from 'mongoose';
|
import { Types } from 'mongoose';
|
||||||
import { User } from '@/models/user.model';
|
import { User } from '@/models/user.model';
|
||||||
import { UsersService } from '@/users/users.service';
|
import { UsersService } from '@/users/users.service';
|
||||||
|
|
@ -11,9 +11,16 @@ export class UsersResolver {
|
||||||
|
|
||||||
constructor(private readonly usersService: UsersService) { }
|
constructor(private readonly usersService: UsersService) { }
|
||||||
|
|
||||||
|
// Add pagination skip and limit arguments
|
||||||
@Query(() => [User], { name: 'users' })
|
@Query(() => [User], { name: 'users' })
|
||||||
async getUsers(@Info() info: GraphQLResolveInfo): Promise<User[]> {
|
async getUsers(
|
||||||
const fields = graphqlFields(info); const projection = this.usersService.buildProjection(fields); return this.usersService.findAll(projection);
|
@Info() info: GraphQLResolveInfo,
|
||||||
|
@Args('skip', { type: () => Int, defaultValue: 0 }) skip: number,
|
||||||
|
@Args('limit', { type: () => Int, defaultValue: 10 }) limit: number,
|
||||||
|
): Promise<User[]> {
|
||||||
|
const fields = graphqlFields(info);
|
||||||
|
const projection = this.usersService.buildProjection(fields);
|
||||||
|
return this.usersService.findAll(projection, skip, limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Query(() => User, { name: 'user', nullable: true })
|
@Query(() => User, { name: 'user', nullable: true })
|
||||||
|
|
|
||||||
|
|
@ -7,29 +7,14 @@ import { CreateUserInput } from './dto/create-user.input';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UsersService {
|
export class UsersService {
|
||||||
|
|
||||||
constructor(
|
constructor(@InjectModel(User.name) private readonly userModel: Model<UserDocument>) { }
|
||||||
@InjectModel(User.name) private readonly userModel: Model<UserDocument>
|
|
||||||
) { }
|
|
||||||
|
|
||||||
async findAll(projection?: any): Promise<UserDocument[]> {
|
async findAll(projection?: any, skip: number = 0, limit: number = 10): Promise<UserDocument[]> { return this.userModel.find({}, projection, { lean: false }).skip(skip).limit(limit).exec() }
|
||||||
return this.userModel.find({}, projection, { lean: false }).populate({ path: 'person', select: projection?.person }).exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
async findById(id: Types.ObjectId, projection?: any): Promise<UserDocument | null> {
|
async findById(id: Types.ObjectId, projection?: any): Promise<UserDocument | null> { return this.userModel.findById(id, projection, { lean: false }).populate({ path: 'person', select: projection?.person }).exec() }
|
||||||
return this.userModel.findById(id, projection, { lean: false }).populate({ path: 'person', select: projection?.person }).exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
async create(input: CreateUserInput): Promise<UserDocument> {
|
async create(input: CreateUserInput): Promise<UserDocument> { const user = new this.userModel(input); return user.save() }
|
||||||
const user = new this.userModel(input);
|
|
||||||
return user.save();
|
buildProjection(fields: Record<string, any>): any { const projection: any = {}; for (const key in fields) { projection[key] = 1 }; return projection }
|
||||||
}
|
|
||||||
|
|
||||||
buildProjection(fields: Record<string, any>): any {
|
|
||||||
const projection: any = {};
|
|
||||||
for (const key in fields) {
|
|
||||||
if (key === 'person' && typeof fields[key] === 'object') { for (const subField of Object.keys(fields[key])) { projection[`person.${subField}`] = 1 } }
|
|
||||||
else { projection[key] = 1 }
|
|
||||||
}
|
|
||||||
return projection;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,3 +34,5 @@ You can check out [the Next.js GitHub repository](https://github.com/vercel/next
|
||||||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
||||||
|
|
||||||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
|
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
|
||||||
|
|
||||||
|
new Date('2025-11-14T18:21:35.212Z').toLocaleString("en-US", { timeZone: "Europe/Istanbul", hour12: false, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit" });
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue