Skip to content

Commit 7541ae8

Browse files
author
Salah Eddine Lalami
authored
✨ Feature/add data to resolvers (#7)
* add data to resolvers * add data demo to resolvers
1 parent 9d1ea7f commit 7541ae8

File tree

13 files changed

+191
-109
lines changed

13 files changed

+191
-109
lines changed

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"printWidth": 100,
3-
"trailingComma": "none",
3+
"trailingComma": "es5",
44
"tabWidth": 2,
55
"semi": true,
66
"singleQuote": true

data/index.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const mainPhoto =
2+
'https://images.unsplash.com/photo-1566665797739-1674de7a421a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8YmVkcm9vbXxlbnwwfHwwfHw%3D&auto=format&fit=crop&w=600&h=400&q=60';
3+
4+
const photos = [mainPhoto];
5+
6+
export const users = [
7+
{
8+
id: '1',
9+
name: 'Salah Lalami',
10+
email: 'hello@lalamisdn.com',
11+
photo:
12+
'https://res.cloudinary.com/practicaldev/image/fetch/s--AksklHZW--/c_fill,f_auto,fl_progressive,h_320,q_auto,w_320/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/661197/1fd0af9f-3b2f-4b12-a59e-edca17c567b7.jpeg',
13+
},
14+
{
15+
id: '2',
16+
name: 'Wes bos',
17+
email: 'wes@wesbos.com',
18+
photo: 'https://pbs.twimg.com/profile_images/877525007185858562/7G9vGTca_400x400.jpg',
19+
},
20+
{
21+
id: '3',
22+
name: 'kent c dods',
23+
email: 'kent@kcc.io',
24+
photo: 'https://pbs.twimg.com/profile_images/1444988463216922631/IDffhy4i_400x400.jpg',
25+
},
26+
];
27+
28+
export const reviews = [
29+
{
30+
id: '1',
31+
author: users[0],
32+
feedback: 'amazing appartment , close to centre , restaurent, metro station',
33+
rate: 5,
34+
place: '1',
35+
},
36+
{
37+
id: '2',
38+
author: users[0],
39+
feedback: 'really cool place , Thank you',
40+
rate: 5,
41+
place: '1',
42+
},
43+
{
44+
id: '3',
45+
author: users[2],
46+
feedback: 'Just Love It',
47+
rate: 4,
48+
place: '1',
49+
},
50+
];
51+
52+
export const places = [
53+
{
54+
id: '1',
55+
owner: users[0],
56+
type: 'appartment',
57+
desciption: 'Cozy appatment with two bedrooms in central of London',
58+
mainPhoto,
59+
photos,
60+
priceByNight: 89,
61+
reviews,
62+
},
63+
];

data/test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const users = [
2+
{
3+
id: '1',
4+
name: 'Salah Lalami',
5+
email: 'hello@lalamisdn.com',
6+
photo:
7+
'https://res.cloudinary.com/practicaldev/image/fetch/s--AksklHZW--/c_fill,f_auto,fl_progressive,h_320,q_auto,w_320/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/661197/1fd0af9f-3b2f-4b12-a59e-edca17c567b7.jpeg',
8+
},
9+
{
10+
id: '2',
11+
name: 'Wes bos',
12+
email: 'wes@wesbos.com',
13+
photo: 'https://pbs.twimg.com/profile_images/877525007185858562/7G9vGTca_400x400.jpg',
14+
},
15+
{
16+
id: '3',
17+
name: 'kent c dods',
18+
email: 'kent@kcc.io',
19+
photo: 'https://pbs.twimg.com/profile_images/1444988463216922631/IDffhy4i_400x400.jpg',
20+
},
21+
];
22+
const reviews = [
23+
{
24+
id: '1',
25+
author: users[0],
26+
feedback: 'amazing appartment , close to centre , restaurent, metro station',
27+
rate: 5,
28+
place: '1',
29+
},
30+
{
31+
id: '2',
32+
author: users[0],
33+
feedback: 'really cool place , Thank you',
34+
rate: 5,
35+
place: '1',
36+
},
37+
{
38+
id: '3',
39+
author: users[2],
40+
feedback: 'Just Love It',
41+
rate: 4,
42+
place: '1',
43+
},
44+
];
45+
46+
console.log(reviews.filter((x) => x.author.id == '1'));

graphql/generated/schemaType.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,57 @@ export type Scalars = {
1414

1515
export type Mutation = {
1616
__typename?: 'Mutation';
17-
addPropriety?: Maybe<Propriety>;
17+
addPlace?: Maybe<Place>;
1818
addReview?: Maybe<Review>;
1919
addUser: User;
2020
};
2121

22-
export type MutationAddProprietyArgs = {
23-
body?: InputMaybe<InputProprietyType>;
22+
23+
export type MutationAddPlaceArgs = {
24+
body?: InputMaybe<InputPlaceType>;
2425
};
2526

27+
2628
export type MutationAddReviewArgs = {
2729
body?: InputMaybe<InputReviewType>;
2830
};
2931

32+
3033
export type MutationAddUserArgs = {
3134
body?: InputMaybe<InputUserType>;
3235
};
3336

34-
export type Propriety = {
35-
__typename?: 'Propriety';
37+
export type Place = {
38+
__typename?: 'Place';
3639
desciption?: Maybe<Scalars['String']>;
3740
id?: Maybe<Scalars['ID']>;
3841
mainPhoto?: Maybe<Scalars['String']>;
3942
owner?: Maybe<User>;
4043
photos?: Maybe<Array<Maybe<Scalars['String']>>>;
4144
priceByNight?: Maybe<Scalars['Float']>;
4245
reviews?: Maybe<Array<Maybe<Review>>>;
43-
title?: Maybe<Scalars['String']>;
44-
type?: Maybe<Scalars['String']>;
4546
};
4647

4748
export type Query = {
4849
__typename?: 'Query';
49-
propriety?: Maybe<Propriety>;
50-
proprietyList?: Maybe<Array<Maybe<Propriety>>>;
50+
place?: Maybe<Place>;
51+
placeList?: Maybe<Array<Maybe<Place>>>;
5152
reviewByUser?: Maybe<Array<Maybe<Review>>>;
5253
user: User;
5354
userList: Array<User>;
5455
};
5556

56-
export type QueryProprietyArgs = {
57+
58+
export type QueryPlaceArgs = {
5759
id?: InputMaybe<Scalars['ID']>;
5860
};
5961

62+
6063
export type QueryReviewByUserArgs = {
6164
id?: InputMaybe<Scalars['ID']>;
6265
};
6366

67+
6468
export type QueryUserArgs = {
6569
id: Scalars['ID'];
6670
};
@@ -70,7 +74,7 @@ export type Review = {
7074
author?: Maybe<User>;
7175
feedback?: Maybe<Scalars['String']>;
7276
id?: Maybe<Scalars['ID']>;
73-
propriety?: Maybe<Propriety>;
77+
place?: Maybe<Scalars['ID']>;
7478
rate?: Maybe<Scalars['Float']>;
7579
};
7680

@@ -82,19 +86,18 @@ export type User = {
8286
photo: Scalars['String'];
8387
};
8488

85-
export type InputProprietyType = {
89+
export type InputPlaceType = {
8690
desciption?: InputMaybe<Scalars['String']>;
8791
mainPhoto?: InputMaybe<Scalars['String']>;
8892
photos?: InputMaybe<Array<InputMaybe<Scalars['String']>>>;
8993
priceByNight?: InputMaybe<Scalars['Float']>;
90-
title?: InputMaybe<Scalars['String']>;
9194
type?: InputMaybe<Scalars['String']>;
9295
};
9396

9497
export type InputReviewType = {
95-
authorId?: InputMaybe<Scalars['ID']>;
9698
feedback?: InputMaybe<Scalars['String']>;
97-
proprietyId?: InputMaybe<Scalars['ID']>;
99+
id?: InputMaybe<Scalars['ID']>;
100+
place?: InputMaybe<Scalars['ID']>;
98101
rate?: InputMaybe<Scalars['Float']>;
99102
};
100103

graphql/resolvers/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { combineResolvers, resolverType } from 'fast-graphql';
22

3-
import propriety from './propriety';
43
import user from './user';
4+
import place from './place';
55
import review from './review';
66

7-
const resolvers: resolverType[] = [propriety, user, review];
7+
const resolvers: resolverType[] = [place, user, review];
88

99
const cominedResolvers = combineResolvers({ resolvers });
1010

graphql/resolvers/place.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { resolverType } from 'fast-graphql';
2+
import { places } from '@/data';
3+
4+
import * as schema from '@/graphql/generated/schemaType';
5+
6+
const Query = {
7+
placeList: (parent: any, args: any, ctx: any): schema.Query['placeList'] => {
8+
return places;
9+
},
10+
11+
place: (parent: any, args: any, ctx: any) => {
12+
return places.find((x) => x.id == args.id);
13+
},
14+
};
15+
16+
const Mutation = {
17+
addPlace: (parent: any, args: any, ctx: any) => {
18+
return { title: 'this is addPlace mutuation' };
19+
},
20+
};
21+
22+
const resolver: resolverType = { Query, Mutation };
23+
24+
export default resolver;

graphql/resolvers/propriety.ts

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

graphql/resolvers/review.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { resolverType } from 'fast-graphql';
2+
import { reviews } from '@/data';
23

34
const Query = {
4-
reviewByUser: (id: any) => {
5-
return { id: 'reviewByUser' };
6-
}
5+
reviewByUser: (parent: any, args: any, ctx: any) => {
6+
return reviews.filter((x) => x.id == args.id);
7+
},
78
};
89

910
const Mutation = {
10-
addReview: (body: any) => {
11+
addReview: (parent: any, args: any, ctx: any) => {
1112
return { name: 'this is addReview mutuation' };
12-
}
13+
},
1314
};
1415

1516
const resolver: resolverType = { Query, Mutation };

graphql/resolvers/user.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import { resolverType } from 'fast-graphql';
2-
2+
import { users } from '@/data';
33
import * as schemaType from '@/graphql/generated/schemaType';
44

55
const Query = {
6-
userList: (): schemaType.Query['userList'] => {
7-
return [{ email: 'Nextjs', id: 'sqdqsd', photo: 'qsdqsdqs' }];
6+
userList: (parent: any, args: any, ctx: any) => {
7+
return users;
88
},
99

10-
user: (parent: any, args: any, ctx: any): schemaType.Query['user'] => {
11-
const { id } = args;
12-
console.log('🚀 ~ file: user.ts ~ line 17 ~ id', id);
13-
return { email: 'Nextjs', id: 'sqdqsd', photo: 'qsdqsdqs' };
14-
}
10+
user: (parent: any, args: any, ctx: any) => {
11+
return users.find((x) => x.id == args.id);
12+
},
1513
};
1614

1715
const Mutation = {
18-
addUser: (body: any) => {
16+
addUser: (parent: any, args: any, ctx: any) => {
1917
return { name: 'this is addUser mutuation' };
20-
}
18+
},
2119
};
2220

2321
const resolver: resolverType = { Query, Mutation };

graphql/schema.graphql

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,46 @@
1-
type Propriety {
1+
type Place {
22
id: ID
33
owner: User
4-
type: String
5-
title: String
64
desciption: String
75
mainPhoto: String
86
photos: [String]
97
priceByNight: Float
108
reviews: [Review]
119
}
1210

13-
input inputProprietyType {
11+
input inputPlaceType {
1412
type: String
15-
title: String
1613
desciption: String
1714
mainPhoto: String
1815
photos: [String]
1916
priceByNight: Float
2017
}
2118

2219
type Mutation {
23-
addPropriety(body: inputProprietyType): Propriety
20+
addPlace(body: inputPlaceType): Place
2421
}
2522

2623
type Query {
27-
proprietyList: [Propriety]
28-
propriety(id: ID): Propriety
24+
placeList: [Place]
25+
place(id: ID): Place
2926
}
3027
type Review {
3128
id: ID
3229
author: User
3330
feedback: String
3431
rate: Float
35-
propriety: Propriety
32+
place: ID
3633
}
3734

3835
type Query {
3936
reviewByUser(id: ID): [Review]
4037
}
4138

4239
input inputReviewType {
43-
authorId: ID
40+
id: ID
4441
feedback: String
4542
rate: Float
46-
proprietyId: ID
43+
place: ID
4744
}
4845

4946
type Mutation {

0 commit comments

Comments
 (0)