-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to #16 Add GraphQL layer to the repository. * **Dependencies**: Add `@nestjs/graphql`, `graphql`, and `@nestjs/apollo` to `package.json`. * **Schema**: Create `src/graphql/schema.graphql` with type definitions for Customer, Employee, and Shipper models, including queries and mutations. * **Resolvers**: Create `src/graphql/resolvers.ts` with resolvers for Customer, Employee, and Shipper models, implementing queries and mutations. * **App Module**: Modify `src/app.module.ts` to import and configure `GraphQLModule` with `forRoot` method, and add `Resolvers` to `providers` array. * **Tests**: Add `src/graphql/graphql.test.ts` with tests for GraphQL endpoints, including queries and mutations for Customer, Employee, and Shipper models.
- Loading branch information
1 parent
898c931
commit 3ad7ffe
Showing
5 changed files
with
413 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
import { INestApplication } from '@nestjs/common' | ||
import { Test } from '@nestjs/testing' | ||
import * as request from 'supertest' | ||
import { AppModule } from '../app.module' | ||
|
||
describe('GraphQL', () => { | ||
let app: INestApplication | ||
|
||
beforeAll(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile() | ||
|
||
app = moduleRef.createNestApplication() | ||
await app.init() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
describe('Customer', () => { | ||
it('should get a customer by id', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
query: ` | ||
query { | ||
getCustomer(id: "1") { | ||
id | ||
companyName | ||
contactName | ||
contactTitle | ||
address | ||
city | ||
region | ||
postalCode | ||
country | ||
phone | ||
fax | ||
} | ||
} | ||
`, | ||
}) | ||
.expect(200) | ||
|
||
expect(response.body.data.getCustomer).toBeDefined() | ||
}) | ||
|
||
it('should create a customer', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
query: ` | ||
mutation { | ||
createCustomer(input: { | ||
companyName: "Test Company" | ||
contactName: "Test Contact" | ||
contactTitle: "Test Title" | ||
address: "Test Address" | ||
city: "Test City" | ||
region: "Test Region" | ||
postalCode: "Test PostalCode" | ||
country: "Test Country" | ||
phone: "Test Phone" | ||
fax: "Test Fax" | ||
}) { | ||
id | ||
companyName | ||
contactName | ||
contactTitle | ||
address | ||
city | ||
region | ||
postalCode | ||
country | ||
phone | ||
fax | ||
} | ||
} | ||
`, | ||
}) | ||
.expect(200) | ||
|
||
expect(response.body.data.createCustomer).toBeDefined() | ||
}) | ||
}) | ||
|
||
describe('Employee', () => { | ||
it('should get an employee by id', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
query: ` | ||
query { | ||
getEmployee(id: "1") { | ||
id | ||
lastName | ||
firstName | ||
title | ||
titleOfCourtesy | ||
birthDate | ||
hireDate | ||
address | ||
city | ||
region | ||
postalCode | ||
country | ||
homePhone | ||
extension | ||
notes | ||
reportsTo | ||
} | ||
} | ||
`, | ||
}) | ||
.expect(200) | ||
|
||
expect(response.body.data.getEmployee).toBeDefined() | ||
}) | ||
|
||
it('should create an employee', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
query: ` | ||
mutation { | ||
createEmployee(input: { | ||
lastName: "Test LastName" | ||
firstName: "Test FirstName" | ||
title: "Test Title" | ||
titleOfCourtesy: "Test Courtesy" | ||
birthDate: "2000-01-01" | ||
hireDate: "2020-01-01" | ||
address: "Test Address" | ||
city: "Test City" | ||
region: "Test Region" | ||
postalCode: "Test PostalCode" | ||
country: "Test Country" | ||
homePhone: "Test Phone" | ||
extension: "Test Extension" | ||
notes: "Test Notes" | ||
reportsTo: "1" | ||
}) { | ||
id | ||
lastName | ||
firstName | ||
title | ||
titleOfCourtesy | ||
birthDate | ||
hireDate | ||
address | ||
city | ||
region | ||
postalCode | ||
country | ||
homePhone | ||
extension | ||
notes | ||
reportsTo | ||
} | ||
} | ||
`, | ||
}) | ||
.expect(200) | ||
|
||
expect(response.body.data.createEmployee).toBeDefined() | ||
}) | ||
}) | ||
|
||
describe('Shipper', () => { | ||
it('should get a shipper by id', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
query: ` | ||
query { | ||
getShipper(id: "1") { | ||
id | ||
companyName | ||
phone | ||
} | ||
} | ||
`, | ||
}) | ||
.expect(200) | ||
|
||
expect(response.body.data.getShipper).toBeDefined() | ||
}) | ||
|
||
it('should create a shipper', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
query: ` | ||
mutation { | ||
createShipper(input: { | ||
companyName: "Test Company" | ||
phone: "Test Phone" | ||
}) { | ||
id | ||
companyName | ||
phone | ||
} | ||
} | ||
`, | ||
}) | ||
.expect(200) | ||
|
||
expect(response.body.data.createShipper).toBeDefined() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql'; | ||
import { CustomerService } from '../services/customer.service'; | ||
import { EmployeeService } from '../services/employee.service'; | ||
import { ShipperService } from '../services/shipper.service'; | ||
import { Customer } from '../models/customer.model'; | ||
import { Employee } from '../models/employee.model'; | ||
import { Shipper } from '../models/shipper.model'; | ||
import { CustomerInput } from '../dto/customer.input'; | ||
import { EmployeeInput } from '../dto/employee.input'; | ||
import { ShipperInput } from '../dto/shipper.input'; | ||
|
||
@Resolver() | ||
export class Resolvers { | ||
constructor( | ||
private readonly customerService: CustomerService, | ||
private readonly employeeService: EmployeeService, | ||
private readonly shipperService: ShipperService, | ||
) {} | ||
|
||
@Query(() => Customer) | ||
async getCustomer(@Args('id') id: string) { | ||
return this.customerService.findOne(id); | ||
} | ||
|
||
@Query(() => [Customer]) | ||
async getCustomers() { | ||
return this.customerService.findAll(); | ||
} | ||
|
||
@Mutation(() => Customer) | ||
async createCustomer(@Args('input') input: CustomerInput) { | ||
return this.customerService.create(input); | ||
} | ||
|
||
@Mutation(() => Customer) | ||
async updateCustomer(@Args('id') id: string, @Args('input') input: CustomerInput) { | ||
return this.customerService.update(id, input); | ||
} | ||
|
||
@Mutation(() => Boolean) | ||
async deleteCustomer(@Args('id') id: string) { | ||
return this.customerService.delete(id); | ||
} | ||
|
||
@Query(() => Employee) | ||
async getEmployee(@Args('id') id: string) { | ||
return this.employeeService.findOne(id); | ||
} | ||
|
||
@Query(() => [Employee]) | ||
async getEmployees() { | ||
return this.employeeService.findAll(); | ||
} | ||
|
||
@Mutation(() => Employee) | ||
async createEmployee(@Args('input') input: EmployeeInput) { | ||
return this.employeeService.create(input); | ||
} | ||
|
||
@Mutation(() => Employee) | ||
async updateEmployee(@Args('id') id: string, @Args('input') input: EmployeeInput) { | ||
return this.employeeService.update(id, input); | ||
} | ||
|
||
@Mutation(() => Boolean) | ||
async deleteEmployee(@Args('id') id: string) { | ||
return this.employeeService.delete(id); | ||
} | ||
|
||
@Query(() => Shipper) | ||
async getShipper(@Args('id') id: string) { | ||
return this.shipperService.findOne(id); | ||
} | ||
|
||
@Query(() => [Shipper]) | ||
async getShippers() { | ||
return this.shipperService.findAll(); | ||
} | ||
|
||
@Mutation(() => Shipper) | ||
async createShipper(@Args('input') input: ShipperInput) { | ||
return this.shipperService.create(input); | ||
} | ||
|
||
@Mutation(() => Shipper) | ||
async updateShipper(@Args('id') id: string, @Args('input') input: ShipperInput) { | ||
return this.shipperService.update(id, input); | ||
} | ||
|
||
@Mutation(() => Boolean) | ||
async deleteShipper(@Args('id') id: string) { | ||
return this.shipperService.delete(id); | ||
} | ||
} |
Oops, something went wrong.