Skip to content

Conversation

justlevine
Copy link
Collaborator

@justlevine justlevine commented May 19, 2025

What does this implement/fix? Explain your changes.

This PR refactors AppContext in the following ways:

  1. Only instantiate DataLoaders when they're used instead of instantiating all of them):
  • Deprecates the graphql_data_loaders filter in favor of the new graphql_data_loader_classes filter. (so you can replace the loader before it's instantiated
  • Accessing AppContext::$loaders has been deprecated, in lieu of changing the property's visibility to protected/private in a future breaking release.
  1. Updates the PHPStan typehinting for ::get_loader() to correctly type-hint the loader instance for the given key.
    E.g. Previously, this would have thrown a PHPStan error that ::get_public_users() doesn't exist on AbstractDataLoader
      $users = $context->get_loader( 'user' )->get_public_users( [ ] );

This is a nonbreaking change. Users still relying on graphql_data_loaders will have all their AppContext::$loaders pre-instantiated as before.

Does this close any currently open issues?

Any other comments?

  • This PR should open the way for type hinting/inference for Connection Resolvers, deferred Models, etc.

@justlevine justlevine requested a review from Copilot May 19, 2025 14:32
Copy link
Contributor

@Copilot 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

This PR refactors AppContext, updating how DataLoader instances are instantiated and improving PHPStan type hinting. Key changes include deprecating the graphql_data_loaders filter in favor of graphql_data_loader_classes, the introduction of a DEFAULT_LOADERS constant for loader class definitions, and enhanced type hinting for the get_loader() method.

Reviewed Changes

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

File Description
src/Data/Cursor/AbstractCursor.php Updates PHPDoc for the $field parameter to enforce array<string,mixed> instead of array<string,mixed>
src/AppContext.php Refactors DataLoader instantiation, introduces DEFAULT_LOADERS constant, and improves PHPStan generics in get_loader()
Comments suppressed due to low confidence (1)

src/AppContext.php:190

  • Instantiating loaders without passing the AppContext instance may lead to issues if the loader constructors require context (as seen previously). Consider passing $this to the loader constructors if they expect it.
return new $loader_class();

@justlevine justlevine changed the title dev: improve DataLoader typing and instantiation in AppContext refactor: improve DataLoader typing and instantiation in AppContext May 19, 2025
@justlevine justlevine changed the title refactor: improve DataLoader typing and instantiation in AppContext refactor: improve data loader instantiation and typing in AppContext May 19, 2025
@coveralls
Copy link

coveralls commented May 19, 2025

Coverage Status

coverage: 84.119% (-0.1%) from 84.234%
when pulling 25e9bf9 on justlevine:dev/app-context-cleanup
into 161565b on wp-graphql:develop.

@justlevine justlevine changed the title refactor: improve data loader instantiation and typing in AppContext perf: refactor AppContext to lazy-load dataloaders May 19, 2025
@justlevine justlevine requested a review from Copilot May 19, 2025 15:13
Copy link
Contributor

@Copilot 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

This PR refactors AppContext to lazy-load DataLoader instances instead of pre-instantiating them, deprecates the old graphql_data_loaders filter in favor of graphql_data_loader_classes, and tightens a docblock in AbstractCursor.

  • Introduces DEFAULT_LOADERS and a loader_classes property to hold loader class strings.
  • Adds prepare_data_loaders to apply filters and defer instantiation.
  • Updates get_loader to instantiate on first use and adds a magic __get for deprecation warnings.
  • Refines the docblock for is_valid_offset_and_node’s $field parameter in AbstractCursor.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Data/Cursor/AbstractCursor.php Tightened the @param docblock for is_valid_offset_and_node from mixed
src/AppContext.php Refactored to lazy-load loaders, introduced loader_classes/prepare_data_loaders, deprecated the old filter, and added __get deprecation warning
Comments suppressed due to low confidence (3)

src/AppContext.php:123

  • The $loaders property is declared public, so the magic __get method will never be invoked to warn about deprecation. Consider changing its visibility to protected or private to ensure __get is triggered.
public $loaders = [];

src/AppContext.php:172

  • Consider adding unit tests for the new lazy-loading behavior in prepare_data_loaders, verifying that loader classes are applied via filters and instances are created on first access.
private function prepare_data_loaders(): void {

src/Data/Cursor/AbstractCursor.php:152

  • The docblock restricts $field to array<string,mixed>, but the method signature previously allowed mixed; ensure this tighter type reflects actual usage or update the validation logic accordingly.
* @param array<string,mixed> $field Threshold configuration.

justlevine and others added 2 commits May 19, 2025 18:19
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@justlevine justlevine requested a review from Copilot May 19, 2025 15:21
@justlevine justlevine added status: in review Awaiting review before merging or closing needs: reviewer response This needs the attention of a codeowner or maintainer labels May 19, 2025
Copy link

Code Climate has analyzed commit 25e9bf9 and detected 0 issues on this pull request.

View more on Code Climate.

Copy link
Contributor

@Copilot 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

This PR refactors the AppContext to lazy-load data loaders and improves PHPStan type hinting while deprecating the old graphql_data_loaders filter. Key changes include:

  • Deferring loader instantiation until accessed and introducing a new graphql_data_loader_classes filter.
  • Updating docblocks and type hints for better static analysis and code clarity.
  • Fixing documentation typos and clarifying inline comments.

Reviewed Changes

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

File Description
src/Data/Cursor/AbstractCursor.php Updated parameter type hint for field configuration in docblock.
src/AppContext.php Refactored lazy-loading of data loaders and updated constructor/docblocks accordingly.
docs/using-data-from-custom-database-tables.md Corrected typos and improved clarity in documentation examples.
Comments suppressed due to low confidence (2)

docs/using-data-from-custom-database-tables.md:569

  • Typo detected: 'Noticication' should be corrected to 'Notification'.
'toType' => 'Noticication',

src/Data/Cursor/AbstractCursor.php:152

  • The updated type hint restricts $field to arrays. Please verify that all call sites pass an array to prevent runtime issues.
 * @param array<string,mixed> $field Threshold configuration.

@justlevine justlevine requested review from jasonbahl and kidunot89 May 19, 2025 15:22
@justlevine justlevine added type: enhancement Improvements to existing functionality scope: performance Enhancing speed and efficiency labels May 19, 2025
@jasonbahl jasonbahl merged commit 192feef into wp-graphql:develop Jun 2, 2025
36 of 38 checks passed
@justlevine justlevine deleted the dev/app-context-cleanup branch June 2, 2025 20:50
justlevine pushed a commit to justlevine/wp-graphql that referenced this pull request Jun 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: reviewer response This needs the attention of a codeowner or maintainer scope: performance Enhancing speed and efficiency status: in review Awaiting review before merging or closing type: enhancement Improvements to existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants