Skip to content

Commit 2bbaf3a

Browse files
authored
Merge pull request #3 from samuel27m/feat/add-recipe
feat: can add recipes now
2 parents b3a7468 + 17273ad commit 2bbaf3a

File tree

6 files changed

+70
-25
lines changed

6 files changed

+70
-25
lines changed

src/entities/Recipe.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { ObjectType, Field, ID } from 'type-graphql';
1+
import { ObjectType, Field, Int } from 'type-graphql';
22
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
33

44
@Entity()
55
@ObjectType()
66
export class Recipe {
77
@PrimaryGeneratedColumn()
8-
@Field(type => ID)
9-
id: string;
8+
@Field(() => Int)
9+
id: number;
1010

1111
@Column()
1212
@Field()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Field, InputType } from 'type-graphql';
2+
import { Recipe } from '../../entities/Recipe';
3+
4+
@InputType({ description: 'New recipe data' })
5+
export default class AddRecipeInput implements Partial<Recipe> {
6+
@Field()
7+
title: string;
8+
9+
@Field({ nullable: true })
10+
description?: string;
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Repository } from 'typeorm';
2+
import { Resolver, Query, Mutation, Arg } from 'type-graphql';
3+
import { AppDataSource } from '../../database';
4+
import { Recipe } from '../../entities/Recipe';
5+
import AddRecipeInput from '../inputs/AddRecipeInput';
6+
7+
@Resolver(Recipe)
8+
export class RecipeResolver {
9+
private repository: Repository<Recipe>;
10+
constructor() {
11+
this.repository = AppDataSource.getRepository(Recipe);
12+
}
13+
14+
@Query(() => [Recipe])
15+
async recipes(): Promise<Recipe[]> {
16+
return await this.repository.find();
17+
}
18+
19+
@Mutation(() => Recipe)
20+
async addRecipe(@Arg('data') newRecipeData: AddRecipeInput): Promise<Recipe> {
21+
return await this.repository.save(newRecipeData);
22+
}
23+
}

src/resolvers/RecipeResolver.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/services/ApolloServices/ApolloServerService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ApolloServer } from 'apollo-server-express';
22
import { buildSchema } from 'type-graphql';
3-
import { RecipeResolver } from '../../resolvers/RecipeResolver';
3+
import { RecipeResolver } from '../../graphql/resolvers/RecipeResolver';
44

55
async function createApolloServer(): Promise<ApolloServer> {
66
const schema = await buildSchema({

tests/resolvers/RecipeResolver.spec.ts renamed to tests/graphql/RecipeResolver.spec.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,45 @@ import { Recipe } from '../../src/entities/Recipe';
66
jest.mock('../../src/database');
77

88
describe('RecipeResolver', () => {
9-
beforeAll(async () => {
9+
beforeEach(async () => {
1010
await initialiseDataSource();
11-
// create recipe
12-
const recipe = new Recipe();
13-
recipe.title = 'my test';
14-
recipe.description = 'description test';
15-
16-
const repository = AppDataSource.getRepository(Recipe);
17-
await repository.save(recipe);
1811
});
1912

20-
afterAll(async () => {
13+
afterEach(async () => {
2114
await destroyDataSource();
2215
});
2316

17+
it('adds a recipe', async () => {
18+
const title = 'another one';
19+
const apolloServer = await createApolloServer();
20+
const query = `mutation {
21+
addRecipe(data: {title: "${title}"}) {
22+
id,
23+
title,
24+
description
25+
}
26+
}`;
27+
28+
const result = await apolloServer.executeOperation({
29+
query,
30+
});
31+
32+
const addedRecipe = result.data?.addRecipe;
33+
34+
expect(addedRecipe).not.toBeNull();
35+
expect(addedRecipe.title).toEqual(title);
36+
expect(addedRecipe.description).toBeNull();
37+
});
38+
2439
it('gets recipes', async () => {
40+
// create a recipe
41+
const recipe: Omit<Recipe, 'id'> = {
42+
title: 'testing',
43+
description: 'test',
44+
};
45+
const repository = AppDataSource.getRepository(Recipe);
46+
await repository.save(recipe);
47+
2548
const apolloServer = await createApolloServer();
2649
const query = `{
2750
recipes {

0 commit comments

Comments
 (0)