From 17aaa34035722ca13c14175185a1d91f00976903 Mon Sep 17 00:00:00 2001 From: MD SADAB SAQIB Date: Wed, 15 May 2024 00:00:04 +1000 Subject: [PATCH] utilized fragments to reuse request object --- readme.md | 2 +- tests/payload/directives.ts | 8 +++----- tests/payload/fragments.ts | 20 ++++++++++++++++++++ tests/payload/mutation.ts | 27 ++++++++++----------------- tests/payload/queries.ts | 14 ++++---------- 5 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 tests/payload/fragments.ts diff --git a/readme.md b/readme.md index 13ca1ea..7ede0ef 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/tests/payload/directives.ts b/tests/payload/directives.ts index 98be4b9..36e11ab 100644 --- a/tests/payload/directives.ts +++ b/tests/payload/directives.ts @@ -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} } } }`; diff --git a/tests/payload/fragments.ts b/tests/payload/fragments.ts new file mode 100644 index 0000000..35845e9 --- /dev/null +++ b/tests/payload/fragments.ts @@ -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} + }`; +}; \ No newline at end of file diff --git a/tests/payload/mutation.ts b/tests/payload/mutation.ts index cd6d904..12a4fde 100644 --- a/tests/payload/mutation.ts +++ b/tests/payload/mutation.ts @@ -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}" @@ -11,11 +14,7 @@ export const createUserPayload = (data: IUserOmittedID) => { } ) { user { - id - name - gender - email - status + ...userFragments } } }`; @@ -23,7 +22,9 @@ export const createUserPayload = (data: IUserOmittedID) => { export const updateUserPayload = (data: IUser) => { - return `mutation { + return ` + ${userFragments} + mutation { updateUser( input: { id: ${data.id} @@ -34,11 +35,7 @@ export const updateUserPayload = (data: IUser) => { } ) { user { - id - name - gender - email - status + ...userFragments } } }`; @@ -52,11 +49,7 @@ export const deleteUserPayload = (id: number) => { } ) { user { - id - name - gender - email - status + ${userResponseFields} } } }`; diff --git a/tests/payload/queries.ts b/tests/payload/queries.ts index 9c5d518..1c19ad7 100644 --- a/tests/payload/queries.ts +++ b/tests/payload/queries.ts @@ -1,12 +1,10 @@ +import { userResponseFields } from './fragments'; + export const queryAllUserPayload = `{ users { totalCount nodes { - email - gender - id - name - status + ${userResponseFields} } } }`; @@ -14,11 +12,7 @@ export const queryAllUserPayload = `{ export const queryUserByIdPayload = (id: number) => { return `query User { user(id: "${id}") { - email - gender - id - name - status + ${userResponseFields} } }`; }; \ No newline at end of file