Skip to content

Commit

Permalink
utilized fragments to reuse request object
Browse files Browse the repository at this point in the history
  • Loading branch information
sadabnepal committed May 14, 2024
1 parent cafead6 commit 17aaa34
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Sample Report
### GraphQL Topics to Explore
- [x] basics of GraphQL (query, mutation)
- [ ] advance topics (fragments, unions, aliases)
- [ ] filters using arguments, variables, directive
- [x] filters using arguments, variables, directive
- [ ] setting default variable
- [ ] variables inside fragments
- [ ] use of directive to build dynamic/reusable query
Expand Down
8 changes: 3 additions & 5 deletions tests/payload/directives.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { userResponseFields } from './fragments';

export const queryUsersWithNodeDirectivePayload = (includeNodes = true) => {
return `{
users {
totalCount
nodes @include (if: ${includeNodes}) {
email
gender
id
name
status
${userResponseFields}
}
}
}`;
Expand Down
20 changes: 20 additions & 0 deletions tests/payload/fragments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const userFragments = `fragment userFragments on user {
id
name
gender
email
status
}`;

export const userResponseFields = `id
name
gender
email
status`;

//TODO: enhance this
export const fragmentBuilder = (fragmentName: string, fragmentObject: string, fieldType: string) => {
return `fragment ${fragmentName} on ${fieldType} {
${fragmentObject}
}`;
};
27 changes: 10 additions & 17 deletions tests/payload/mutation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { IUser, IUserOmittedID } from '../types/users';
import { userFragments, userResponseFields } from './fragments';

export const createUserPayload = (data: IUserOmittedID) => {
return `mutation {
return `
${userFragments}
mutation {
createUser(
input: {
name: "${data.name}"
Expand All @@ -11,19 +14,17 @@ export const createUserPayload = (data: IUserOmittedID) => {
}
) {
user {
id
name
gender
email
status
...userFragments
}
}
}`;
};


export const updateUserPayload = (data: IUser) => {
return `mutation {
return `
${userFragments}
mutation {
updateUser(
input: {
id: ${data.id}
Expand All @@ -34,11 +35,7 @@ export const updateUserPayload = (data: IUser) => {
}
) {
user {
id
name
gender
email
status
...userFragments
}
}
}`;
Expand All @@ -52,11 +49,7 @@ export const deleteUserPayload = (id: number) => {
}
) {
user {
id
name
gender
email
status
${userResponseFields}
}
}
}`;
Expand Down
14 changes: 4 additions & 10 deletions tests/payload/queries.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { userResponseFields } from './fragments';

export const queryAllUserPayload = `{
users {
totalCount
nodes {
email
gender
id
name
status
${userResponseFields}
}
}
}`;

export const queryUserByIdPayload = (id: number) => {
return `query User {
user(id: "${id}") {
email
gender
id
name
status
${userResponseFields}
}
}`;
};

0 comments on commit 17aaa34

Please sign in to comment.