Skip to content

Conversation

@soumojit-D48
Copy link

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
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!

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.

1 participant