Skip to content

Commit 1e72adc

Browse files
author
hirsch88
committed
Add pets relations
1 parent c0c1b08 commit 1e72adc

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

src/api/services/PetService.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Pet } from '../models/Pet';
55
import { events } from '../subscribers/events';
66
import { EventDispatcher, EventDispatcherInterface } from '../../decorators/EventDispatcher';
77
import { Logger, LoggerInterface } from '../../decorators/Logger';
8+
import { User } from '../models/User';
89

910

1011
@Service()
@@ -21,6 +22,15 @@ export class PetService {
2122
return this.petRepository.find();
2223
}
2324

25+
public findByUser(user: User): Promise<Pet[]> {
26+
this.log.info('Find all pets of the user', user.toString());
27+
return this.petRepository.find({
28+
where: {
29+
userId: user.id,
30+
},
31+
});
32+
}
33+
2434
public findOne(id: string): Promise<Pet | undefined> {
2535
this.log.info('Find all pets');
2636
return this.petRepository.findOne({ id });

src/api/types/PetType.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {
2+
GraphQLID,
3+
GraphQLString,
4+
GraphQLInt,
5+
GraphQLObjectType,
6+
} from 'graphql';
7+
8+
export const PetType = new GraphQLObjectType({
9+
name: 'Pet',
10+
description: 'A single pet.',
11+
fields: {
12+
id: {
13+
type: GraphQLID,
14+
description: 'The ID',
15+
},
16+
name: {
17+
type: GraphQLString,
18+
description: 'The name of the pet.',
19+
},
20+
age: {
21+
type: GraphQLInt,
22+
description: 'The age of the pet in years.',
23+
},
24+
},
25+
});

src/api/types/UserType.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import { PetService } from './../services/PetService';
12
import {
23
GraphQLID,
34
GraphQLString,
45
GraphQLObjectType,
6+
GraphQLList,
57
} from 'graphql';
8+
import { PetType } from './PetType';
9+
import { User } from '../models/User';
10+
import { GraphQLContext } from '../../lib/graphql';
611

712
export const UserType = new GraphQLObjectType({
813
name: 'User',
@@ -24,9 +29,11 @@ export const UserType = new GraphQLObjectType({
2429
type: GraphQLString,
2530
description: 'The email of this user.',
2631
},
27-
// pets: {
28-
// type: GraphQLString,
29-
// description: 'The personal number of this user.‚',
30-
// },
32+
pets: {
33+
type: new GraphQLList(PetType),
34+
description: 'The pets of a user',
35+
resolve: (user: User, args: any, context: GraphQLContext<any, any>) =>
36+
context.container.get<PetService>(PetService).findByUser(user),
37+
},
3138
},
3239
});

src/types/auth0.d.ts

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

0 commit comments

Comments
 (0)