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,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();
|
||||
Reference in New Issue
Block a user