Add Pothos-generated User GraphQL schema and resolver
This commit adds the Pothos-generated User GraphQL schema and resolver. The schema is defined in the `src/graphql/graphql.schema.ts` file, and the resolver is defined in the `src/graphql/graphql.resolver.ts` file. The schema includes the `User` object type with fields for `id`, `name`, and `email`. The resolver includes a query field `users` that resolves to fetch all users from the Prisma database.
This commit is contained in:
@@ -1,22 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GraphQLModule } from '@nestjs/graphql';
|
||||
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { GraphqlModule } from './graphql/graphql.module';
|
||||
import { join } from 'path';
|
||||
import { PrismaService } from './prisma/prisma.service';
|
||||
import { PrismaModule } from './prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
GraphQLModule.forRoot<ApolloDriverConfig>({
|
||||
driver: ApolloDriver,
|
||||
playground: true,
|
||||
autoSchemaFile: join(process.cwd(), 'src/graphql/schema.gql'),
|
||||
}),
|
||||
GraphqlModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService, PrismaService],
|
||||
imports: [PrismaModule, GraphqlModule],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@@ -1,8 +1,26 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GraphqlService } from './graphql.service';
|
||||
import { GraphqlResolver } from './graphql.resolver';
|
||||
import { GraphQLModule } from '@nestjs/graphql';
|
||||
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
|
||||
import { schema } from './graphql.schema'; // Assuming you have schema defined in a separate file
|
||||
|
||||
@Module({
|
||||
providers: [GraphqlService, GraphqlResolver],
|
||||
imports: [
|
||||
GraphQLModule.forRoot<ApolloDriverConfig>({
|
||||
driver: ApolloDriver, // Specify the driver
|
||||
schema, // Your Pothos-generated schema
|
||||
playground: {
|
||||
workspaceName: 'EPESS',
|
||||
tabs: [
|
||||
{
|
||||
endpoint: '/graphql',
|
||||
query: `# Welcome to the EPESS GraphQL API
|
||||
`,
|
||||
},
|
||||
],
|
||||
},
|
||||
introspection: true,
|
||||
debug: true,
|
||||
}),
|
||||
],
|
||||
})
|
||||
export class GraphqlModule {}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { GraphqlResolver } from './graphql.resolver';
|
||||
|
||||
describe('GraphqlResolver', () => {
|
||||
let resolver: GraphqlResolver;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [GraphqlResolver],
|
||||
}).compile();
|
||||
|
||||
resolver = module.get<GraphqlResolver>(GraphqlResolver);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(resolver).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -1,39 +0,0 @@
|
||||
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql';
|
||||
import { PrismaService } from 'src/prisma/prisma.service';
|
||||
import { User } from 'src/types/user.type';
|
||||
import { Role } from '@prisma/client';
|
||||
|
||||
@Resolver('User')
|
||||
export class GraphqlResolver {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
@Query(() => [User])
|
||||
async users(): Promise<
|
||||
{
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
password: string;
|
||||
phoneNumber: string;
|
||||
oauthToken: string | null;
|
||||
role: Role;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}[]
|
||||
> {
|
||||
return this.prismaService.user.findMany();
|
||||
}
|
||||
|
||||
@Mutation(() => User)
|
||||
async createUser(
|
||||
@Args('email') email: string,
|
||||
@Args('name') name: string,
|
||||
): Promise<User> {
|
||||
return this.prismaService.user.create({
|
||||
data: {
|
||||
email,
|
||||
name,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
3
src/graphql/graphql.schema.ts
Normal file
3
src/graphql/graphql.schema.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { builder } from '../types/pothos.type';
|
||||
|
||||
export const schema = builder.toSchema();
|
||||
9
src/prisma/prisma.module.ts
Normal file
9
src/prisma/prisma.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { PrismaService } from './prisma.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [PrismaService],
|
||||
exports: [PrismaService],
|
||||
})
|
||||
export class PrismaModule {}
|
||||
@@ -1,15 +1,16 @@
|
||||
import { Injectable, OnModuleInit, INestApplication } from '@nestjs/common';
|
||||
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient implements OnModuleInit {
|
||||
export class PrismaService
|
||||
extends PrismaClient
|
||||
implements OnModuleInit, OnModuleDestroy
|
||||
{
|
||||
async onModuleInit() {
|
||||
await this.$connect();
|
||||
}
|
||||
|
||||
async enableShutdownHooks(app: INestApplication) {
|
||||
this.$on('beforeExit', async () => {
|
||||
await app.close();
|
||||
});
|
||||
async onModuleDestroy() {
|
||||
await this.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
30
src/types/pothos.type.ts
Normal file
30
src/types/pothos.type.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import SchemaBuilder from '@pothos/core';
|
||||
import PrismaPlugin from '@pothos/plugin-prisma';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export const builder = new SchemaBuilder({
|
||||
plugins: [PrismaPlugin],
|
||||
prisma: {
|
||||
client: prisma,
|
||||
},
|
||||
});
|
||||
|
||||
// Define a basic query type
|
||||
builder.queryType({
|
||||
fields: (t) => ({
|
||||
hello: t.string({
|
||||
resolve: () => 'Hello, world!',
|
||||
}),
|
||||
}),
|
||||
});
|
||||
|
||||
// Define a basic mutation type
|
||||
builder.mutationType({
|
||||
fields: (t) => ({
|
||||
exampleMutation: t.boolean({
|
||||
resolve: () => true,
|
||||
}),
|
||||
}),
|
||||
});
|
||||
@@ -1,51 +1,30 @@
|
||||
import { ObjectType, Field, Int } from '@nestjs/graphql';
|
||||
import {
|
||||
User as PrismaUser,
|
||||
Role,
|
||||
Order,
|
||||
UploadedDocument,
|
||||
Chat,
|
||||
Feedback,
|
||||
} from '@prisma/client';
|
||||
import { builder } from '../types/pothos.type';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@ObjectType()
|
||||
export class User implements PrismaUser {
|
||||
@Field(() => Int)
|
||||
id: string;
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
})
|
||||
export class UserSchema {
|
||||
constructor(private prisma: PrismaService) {
|
||||
// Define the User GraphQL object type based on Prisma schema
|
||||
builder.prismaObject('User' as never, {
|
||||
fields: (t) => ({
|
||||
id: t.exposeInt('id' as never),
|
||||
name: t.exposeString('name' as never),
|
||||
email: t.exposeString('email' as never),
|
||||
}),
|
||||
});
|
||||
|
||||
@Field()
|
||||
name: string;
|
||||
|
||||
@Field()
|
||||
email: string;
|
||||
|
||||
@Field()
|
||||
password: string;
|
||||
|
||||
@Field()
|
||||
phoneNumber: string;
|
||||
|
||||
@Field()
|
||||
oauthToken: string | null;
|
||||
|
||||
@Field()
|
||||
role: Role;
|
||||
|
||||
@Field()
|
||||
createdAt: Date;
|
||||
|
||||
@Field()
|
||||
updatedAt: Date;
|
||||
|
||||
@Field()
|
||||
orders: Order[];
|
||||
|
||||
@Field()
|
||||
feedbacks: Feedback[];
|
||||
|
||||
@Field()
|
||||
chats: Chat[];
|
||||
|
||||
@Field()
|
||||
documents: UploadedDocument[];
|
||||
// Define the "users" query that resolves using Prisma
|
||||
builder.queryField('users', (t) =>
|
||||
t.prismaField({
|
||||
type: ['User' as never],
|
||||
resolve: async () => {
|
||||
return this.prisma.user.findMany() as any;
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user