graphql api tested & user resolver, schema and mongo interfaces tested
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { mergeTypeDefs, mergeResolvers } from "@graphql-tools/merge";
|
||||
import { userTypeDefs } from "./schema/userSchema";
|
||||
import { userTypeDefs } from "./schema/userTypeDefs";
|
||||
import { userResolvers } from "./resolvers/userResolvers";
|
||||
import { personTypeDefs } from "./schema/personTypeDefs";
|
||||
import { personResolvers } from "./resolvers/personResolvers";
|
||||
|
||||
export const typeDefs = mergeTypeDefs([userTypeDefs]);
|
||||
export const resolvers = mergeResolvers([userResolvers]);
|
||||
export const typeDefs = mergeTypeDefs([userTypeDefs, personTypeDefs]);
|
||||
export const resolvers = mergeResolvers([userResolvers, personResolvers]);
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import clientPromise from '@/lib/mongodb';
|
||||
|
||||
export const resolvers = {
|
||||
Query: {
|
||||
users: async () => {
|
||||
const client = await clientPromise;
|
||||
const db = client.db('test');
|
||||
return db.collection('users').find().toArray();
|
||||
},
|
||||
},
|
||||
};
|
||||
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;
|
||||
},
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { gql } from 'graphql-tag';
|
||||
|
||||
export const typeDefs = gql`
|
||||
type User {
|
||||
_id: ID!
|
||||
name: String!
|
||||
}
|
||||
|
||||
type Query {
|
||||
users: [User!]!
|
||||
}
|
||||
`;
|
||||
49
graphql/schema/personTypeDefs.ts
Normal file
49
graphql/schema/personTypeDefs.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { gql } from "graphql-tag";
|
||||
|
||||
export const personTypeDefs = gql`
|
||||
scalar Date
|
||||
|
||||
type Person {
|
||||
id: ID!
|
||||
firstName: String!
|
||||
surname: String!
|
||||
middleName: String!
|
||||
sexCode: String!
|
||||
personRef: String!
|
||||
personTag: String!
|
||||
fatherName: String!
|
||||
motherName: String!
|
||||
countryCode: String!
|
||||
nationalIdentityId: String!
|
||||
birthPlace: String!
|
||||
birthDate: Date!
|
||||
taxNo: String!
|
||||
birthname: String!
|
||||
}
|
||||
|
||||
input CreatePersonInput {
|
||||
firstName: String!
|
||||
surname: String!
|
||||
middleName: String!
|
||||
sexCode: String!
|
||||
personRef: String!
|
||||
personTag: String!
|
||||
fatherName: String!
|
||||
motherName: String!
|
||||
countryCode: String!
|
||||
nationalIdentityId: String!
|
||||
birthPlace: String!
|
||||
birthDate: Date!
|
||||
taxNo: String!
|
||||
birthname: String!
|
||||
}
|
||||
|
||||
type Query {
|
||||
persons: [Person!]!
|
||||
person(id: ID!): Person
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
createPerson(input: CreatePersonInput!): Person!
|
||||
}
|
||||
`;
|
||||
@@ -1,13 +0,0 @@
|
||||
import { gql } from "graphql-tag";
|
||||
|
||||
export const userTypeDefs = gql`
|
||||
type User {
|
||||
_id: ID!
|
||||
name: String!
|
||||
email: String!
|
||||
}
|
||||
|
||||
type Query {
|
||||
users: [User!]!
|
||||
}
|
||||
`;
|
||||
75
graphql/schema/userTypeDefs.ts
Normal file
75
graphql/schema/userTypeDefs.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { gql } from "graphql-tag";
|
||||
|
||||
export const userTypeDefs = gql`
|
||||
"""Represents a single token entry with a prefix and value"""
|
||||
type CollectionTokenItem {
|
||||
prefix: String!
|
||||
token: String!
|
||||
}
|
||||
|
||||
"""Represents the collection of tokens assigned to a user"""
|
||||
type CollectionToken {
|
||||
tokens: [CollectionTokenItem!]!
|
||||
default: String!
|
||||
}
|
||||
|
||||
type Person {
|
||||
id: ID!
|
||||
firstName: String!
|
||||
surname: String!
|
||||
middleName: String!
|
||||
}
|
||||
|
||||
"""User model with references and metadata"""
|
||||
type User {
|
||||
id: ID!
|
||||
uuid: String!
|
||||
expiresAt: String
|
||||
resetToken: String
|
||||
password: String!
|
||||
history: [String!]
|
||||
tag: String!
|
||||
email: String!
|
||||
phone: String!
|
||||
collectionTokens: CollectionToken!
|
||||
person: Person!
|
||||
type: ID
|
||||
createdAt: String
|
||||
updatedAt: String
|
||||
}
|
||||
|
||||
"""Input type for a single token entry"""
|
||||
input CollectionTokenItemInput {
|
||||
prefix: String!
|
||||
token: String!
|
||||
}
|
||||
|
||||
"""Input type for a user's token collection"""
|
||||
input CollectionTokenInput {
|
||||
tokens: [CollectionTokenItemInput!]!
|
||||
default: String!
|
||||
}
|
||||
|
||||
"""Input for creating a new user"""
|
||||
input CreateUserInput {
|
||||
password: String!
|
||||
history: [String!]
|
||||
tag: String!
|
||||
email: String!
|
||||
phone: String!
|
||||
collectionTokens: CollectionTokenInput!
|
||||
person: ID!
|
||||
type: ID
|
||||
}
|
||||
|
||||
"""Queries"""
|
||||
type Query {
|
||||
users: [User!]!
|
||||
user(id: ID!): User
|
||||
}
|
||||
|
||||
"""Mutations"""
|
||||
type Mutation {
|
||||
createUser(input: CreateUserInput!): User!
|
||||
}
|
||||
`;
|
||||
Reference in New Issue
Block a user