Refactor: Implement GraphQL fragments for WordPress API queries#280
Refactor: Implement GraphQL fragments for WordPress API queries#280soumojit-D48 wants to merge 3 commits intokeploy:mainfrom
Conversation
- Created centralized fragment library in graphql-fragments.ts - Refactored all API queries to use reusable fragments - Improved maintainability and consistency across queries
|
Hi @Surajiitmjnu @amaan-bhati @Achanandhi-M , |
|
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:
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! 💙 |
There was a problem hiding this comment.
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.tsto spread fragment fields instead of repeating inline selections. - Standardized pagination field selection via a
WPPageInfofragment.
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.
Signed-off-by: Soumojit Das <soumojitdas107@gmail.com>
|
@dhananjay6561 Hi, please check the curent status. |
|
hi @soumojit-D48 |
|
@dhananjay6561, Addressed all feedback in commit 4fe6551. Thanks! |
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
lib/graphql-fragments.tswith 8 reusable fragmentslib/api.tsto use fragmentsFragments Added
AUTHOR_FIELDS_FRAGMENT- Author dataPOST_PREVIEW_FRAGMENT- Post listingsFULL_POST_FRAGMENT- Complete post dataPAGINATION_INFO_FRAGMENT- PaginationFEATURED_IMAGE_FRAGMENT,CATEGORY_FIELDS_FRAGMENT,SEO_FIELDS_FRAGMENT,TAG_FIELDS_FRAGMENTTesting
✅ Tested locally - all pages work as expected:
No visual changes, no console errors.
Impact
Before:
After:
Benefits: Easier maintenance, single source of truth for schema changes, cleaner code.