mongoose graphql shacdn next setup completed
This commit is contained in:
6
graphql/index.ts
Normal file
6
graphql/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { mergeTypeDefs, mergeResolvers } from "@graphql-tools/merge";
|
||||
import { userTypeDefs } from "./schema/userSchema";
|
||||
import { userResolvers } from "./resolvers/userResolvers";
|
||||
|
||||
export const typeDefs = mergeTypeDefs([userTypeDefs]);
|
||||
export const resolvers = mergeResolvers([userResolvers]);
|
||||
11
graphql/resolvers.ts
Normal file
11
graphql/resolvers.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
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();
|
||||
},
|
||||
},
|
||||
};
|
||||
21
graphql/resolvers/userResolvers.ts
Normal file
21
graphql/resolvers/userResolvers.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
// graphql/resolvers/userResolvers.ts
|
||||
import { connectDB } from '@/lib/mongodb';
|
||||
import User from '@/models/User';
|
||||
|
||||
export const userResolvers = {
|
||||
Query: {
|
||||
users: async () => {
|
||||
await connectDB();
|
||||
const users = await User.find().lean();
|
||||
return users;
|
||||
},
|
||||
},
|
||||
Mutation: {
|
||||
addUser: async (_: any, { name, email }: { name: string; email: string }) => {
|
||||
await connectDB();
|
||||
const user = new User({ name, email });
|
||||
await user.save();
|
||||
return user;
|
||||
},
|
||||
},
|
||||
};
|
||||
12
graphql/schema.ts
Normal file
12
graphql/schema.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { gql } from 'graphql-tag';
|
||||
|
||||
export const typeDefs = gql`
|
||||
type User {
|
||||
_id: ID!
|
||||
name: String!
|
||||
}
|
||||
|
||||
type Query {
|
||||
users: [User!]!
|
||||
}
|
||||
`;
|
||||
13
graphql/schema/userSchema.ts
Normal file
13
graphql/schema/userSchema.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { gql } from "graphql-tag";
|
||||
|
||||
export const userTypeDefs = gql`
|
||||
type User {
|
||||
_id: ID!
|
||||
name: String!
|
||||
email: String!
|
||||
}
|
||||
|
||||
type Query {
|
||||
users: [User!]!
|
||||
}
|
||||
`;
|
||||
Reference in New Issue
Block a user