Skip to content

Refactor: Implement GraphQL fragments for WordPress API queries#280

Open
soumojit-D48 wants to merge 3 commits intokeploy:mainfrom
soumojit-D48:refactor/use-graphql-fragments
Open

Refactor: Implement GraphQL fragments for WordPress API queries#280
soumojit-D48 wants to merge 3 commits intokeploy:mainfrom
soumojit-D48:refactor/use-graphql-fragments

Conversation

@soumojit-D48
Copy link
Copy Markdown

What & Why

Refactored WordPress API queries to use centralized GraphQL fragments instead of duplicating field definitions across multiple functions.

Problem: Field definitions repeated in 10+ places
Solution: Created reusable fragment library
Result: ~200 lines of duplicate code eliminated

Changes

  • Created lib/graphql-fragments.ts with 8 reusable fragments
  • Refactored 11 query functions in lib/api.ts to use fragments
  • No breaking changes - function signatures unchanged

Fragments Added

  • AUTHOR_FIELDS_FRAGMENT - Author data
  • POST_PREVIEW_FRAGMENT - Post listings
  • FULL_POST_FRAGMENT - Complete post data
  • PAGINATION_INFO_FRAGMENT - Pagination
  • FEATURED_IMAGE_FRAGMENT, CATEGORY_FIELDS_FRAGMENT, SEO_FIELDS_FRAGMENT, TAG_FIELDS_FRAGMENT

Testing

✅ Tested locally - all pages work as expected:

  • Home page loads correctly
  • Technology & Community categories display posts
  • Post detail pages show all data
  • Author pages functional
  • Search works properly

No visual changes, no console errors.

Impact

Before:

// Repeated everywhere
author {
  node {
    name
    firstName
    lastName
    avatar { url }
  }
}

After:

${AUTHOR_FIELDS_FRAGMENT}
author {
  node {
    ...AuthorFields
  }
}

Benefits: Easier maintenance, single source of truth for schema changes, cleaner code.

- Created centralized fragment library in graphql-fragments.ts
- Refactored all API queries to use reusable fragments
- Improved maintainability and consistency across queries
@soumojit-D48
Copy link
Copy Markdown
Author

Hi @Surajiitmjnu @amaan-bhati @Achanandhi-M ,
I’ve opened this PR,
Please let me know it is actually helpfull or not, I am waiting for your review. Thanks!

@dhananjay6561 dhananjay6561 requested a review from Copilot April 6, 2026 12:05
@dhananjay6561
Copy link
Copy Markdown
Member

Hey @soumojit-D48 👋 — thanks for putting this PR together, we appreciate the effort!

We've gone ahead and requested a Copilot review on this. Here's some context from the reviewer:

Good refactor (-116 net lines). Risk: duplicate AuthorFields fragment in getPostAndMorePosts. Test end-to-end.

Once you've had a chance to go through the comments, please address the feedback and resolve the threads — and we'll get this across the line. Feel free to ask if anything's unclear. Happy coding! 💙

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors WPGraphQL query construction to use centralized GraphQL fragments, reducing duplicated field selections across WordPress API query functions.

Changes:

  • Added a shared fragment library (lib/graphql-fragments.ts) containing reusable fragments for authors, posts, pagination, images, categories, tags, and SEO.
  • Updated multiple query functions in lib/api.ts to spread fragment fields instead of repeating inline selections.
  • Standardized pagination field selection via a WPPageInfo fragment.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
lib/graphql-fragments.ts Introduces reusable WPGraphQL fragments for common field groups (author, preview/full post, pagination, etc.).
lib/api.ts Refactors existing query strings to embed fragment documents and use fragment spreads across many fetch helpers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/api.ts Outdated
Comment thread lib/api.ts
Comment thread lib/api.ts
@soumojit-D48
Copy link
Copy Markdown
Author

@dhananjay6561 Hi, please check the curent status.

@dhananjay6561
Copy link
Copy Markdown
Member

hi @soumojit-D48
please reply to all copilot comments and resolve the conversations as well

@soumojit-D48
Copy link
Copy Markdown
Author

@dhananjay6561, Addressed all feedback in commit 4fe6551. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants