File tree 5 files changed +38
-33
lines changed
5 files changed +38
-33
lines changed Original file line number Diff line number Diff line change @@ -98,7 +98,7 @@ Sample Report
98
98
### GraphQL Topics to Explore
99
99
- [x] basics of GraphQL (query, mutation)
100
100
- [ ] advance topics (fragments, unions, aliases)
101
- - [ ] filters using arguments, variables, directive
101
+ - [x ] filters using arguments, variables, directive
102
102
- [ ] setting default variable
103
103
- [ ] variables inside fragments
104
104
- [ ] use of directive to build dynamic/reusable query
Original file line number Diff line number Diff line change
1
+ import { userResponseFields } from './fragments' ;
2
+
1
3
export const queryUsersWithNodeDirectivePayload = ( includeNodes = true ) => {
2
4
return `{
3
5
users {
4
6
totalCount
5
7
nodes @include (if: ${ includeNodes } ) {
6
- email
7
- gender
8
- id
9
- name
10
- status
8
+ ${ userResponseFields }
11
9
}
12
10
}
13
11
}` ;
Original file line number Diff line number Diff line change
1
+ export const userFragments = `fragment userFragments on user {
2
+ id
3
+ name
4
+ gender
5
+ email
6
+ status
7
+ }` ;
8
+
9
+ export const userResponseFields = `id
10
+ name
11
+ gender
12
+ email
13
+ status` ;
14
+
15
+ //TODO: enhance this
16
+ export const fragmentBuilder = ( fragmentName : string , fragmentObject : string , fieldType : string ) => {
17
+ return `fragment ${ fragmentName } on ${ fieldType } {
18
+ ${ fragmentObject }
19
+ }` ;
20
+ } ;
Original file line number Diff line number Diff line change 1
1
import { IUser , IUserOmittedID } from '../types/users' ;
2
+ import { userFragments , userResponseFields } from './fragments' ;
2
3
3
4
export const createUserPayload = ( data : IUserOmittedID ) => {
4
- return `mutation {
5
+ return `
6
+ ${ userFragments }
7
+ mutation {
5
8
createUser(
6
9
input: {
7
10
name: "${ data . name } "
@@ -11,19 +14,17 @@ export const createUserPayload = (data: IUserOmittedID) => {
11
14
}
12
15
) {
13
16
user {
14
- id
15
- name
16
- gender
17
- email
18
- status
17
+ ...userFragments
19
18
}
20
19
}
21
20
}` ;
22
21
} ;
23
22
24
23
25
24
export const updateUserPayload = ( data : IUser ) => {
26
- return `mutation {
25
+ return `
26
+ ${ userFragments }
27
+ mutation {
27
28
updateUser(
28
29
input: {
29
30
id: ${ data . id }
@@ -34,11 +35,7 @@ export const updateUserPayload = (data: IUser) => {
34
35
}
35
36
) {
36
37
user {
37
- id
38
- name
39
- gender
40
- email
41
- status
38
+ ...userFragments
42
39
}
43
40
}
44
41
}` ;
@@ -52,11 +49,7 @@ export const deleteUserPayload = (id: number) => {
52
49
}
53
50
) {
54
51
user {
55
- id
56
- name
57
- gender
58
- email
59
- status
52
+ ${ userResponseFields }
60
53
}
61
54
}
62
55
}` ;
Original file line number Diff line number Diff line change
1
+ import { userResponseFields } from './fragments' ;
2
+
1
3
export const queryAllUserPayload = `{
2
4
users {
3
5
totalCount
4
6
nodes {
5
- email
6
- gender
7
- id
8
- name
9
- status
7
+ ${ userResponseFields }
10
8
}
11
9
}
12
10
}` ;
13
11
14
12
export const queryUserByIdPayload = ( id : number ) => {
15
13
return `query User {
16
14
user(id: "${ id } ") {
17
- email
18
- gender
19
- id
20
- name
21
- status
15
+ ${ userResponseFields }
22
16
}
23
17
}` ;
24
18
} ;
You can’t perform that action at this time.
0 commit comments