Skip to content

Commit

Permalink
Merge pull request #3083 from wp-graphql/dev/cr2-backport/deprecate-c…
Browse files Browse the repository at this point in the history
…amelcased

refactor: deprecate `camelCase` methods in `AbstractConnectionResolver` for `snake_case` equivalents
  • Loading branch information
jasonbahl committed Apr 22, 2024
2 parents 0504a22 + 89491b6 commit f7309af
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 58 deletions.
210 changes: 164 additions & 46 deletions src/Data/Connection/AbstractConnectionResolver.php
Expand Up @@ -68,10 +68,19 @@ abstract class AbstractConnectionResolver {
*/
protected $should_execute = true;

/**
* The loader name.
*
* Defaults to `loader_name()` and filterable by `graphql_connection_loader_name`.
*
* @var ?string
*/
protected $loader_name;

/**
* The loader the resolver is configured to use.
*
* @var \WPGraphQL\Data\Loader\AbstractDataLoader
* @var ?\WPGraphQL\Data\Loader\AbstractDataLoader
*/
protected $loader;

Expand Down Expand Up @@ -169,7 +178,7 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
}

// Get the loader for the Connection.
$this->loader = $this->getLoader();
$this->loader = $this->get_loader();

/**
*
Expand Down Expand Up @@ -200,12 +209,14 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
}

/**
* Returns the source of the connection
* The name of the loader to use for this connection.
*
* @return mixed
* Filterable by `graphql_connection_loader_name`.
*
* @todo This is protected for backwards compatibility, but should be abstract and implemented by the child classes.
*/
public function getSource() {
return $this->source;
protected function loader_name(): string {
return '';
}

/**
Expand All @@ -219,36 +230,6 @@ public function get_args(): array {
return $this->args;
}

/**
* Returns the AppContext of the connection
*/
public function getContext(): AppContext {
return $this->context;
}

/**
* Returns the ResolveInfo of the connection
*/
public function getInfo(): ResolveInfo {
return $this->info;
}

/**
* Returns whether the connection should execute
*/
public function getShouldExecute(): bool {
return $this->should_execute;
}

/**
* Get_loader_name
*
* Return the name of the loader to be used with the connection resolver
*
* @return string
*/
abstract public function get_loader_name();

/**
* Get_query_args
*
Expand Down Expand Up @@ -365,7 +346,6 @@ public function get_offset_for_cursor( string $cursor = null ) { // phpcs:ignore
return is_numeric( $offset ) ? absint( $offset ) : $offset;
}


/**
* Validates Model.
*
Expand All @@ -380,6 +360,81 @@ protected function is_valid_model( $model ) {
return isset( $model->fields ) && ! empty( $model->fields );
}

/**
* Returns the source of the connection
*
* @return mixed
*/
public function get_source() {
return $this->source;
}

/**
* Returns the AppContext of the connection.
*/
public function get_context(): AppContext {
return $this->context;
}

/**
* Returns the ResolveInfo of the connection.
*/
public function get_info(): ResolveInfo {
return $this->info;
}

/**
* Returns the loader name.
*
* If $loader_name is not initialized, this plugin will initialize it.
*
* @return string
*
* @throws \Exception
*/
public function get_loader_name() {
// Only initialize the loader_name property once.
if ( ! isset( $this->loader_name ) ) {
$name = $this->loader_name();

// This is a b/c check because `loader_name()` is not abstract.
if ( empty( $name ) ) {
throw new \Exception(
sprintf(
// translators: %s is the name of the connection resolver class.
esc_html__( 'Class %s does not implement a valid method `loader_name()`.', 'wp-graphql' ),
esc_html( static::class )
)
);
}

/**
* Filters the loader name.
* This is the name of the registered DataLoader that will be used to load the data for the connection.
*
* @param string $loader_name The name of the loader.
* @param self $resolver The AbstractConnectionResolver instance.
*/
$name = apply_filters( 'graphql_connection_loader_name', $name, $this );

// Bail if the loader name is invalid.
if ( empty( $name ) || ! is_string( $name ) ) {
throw new \Exception( esc_html__( 'The Connection Resolver needs to define a loader name', 'wp-graphql' ) );
}

$this->loader_name = $name;
}

return $this->loader_name;
}

/**
* Returns whether the connection should execute.
*/
public function get_should_execute(): bool {
return $this->should_execute;
}

/**
* Returns the $args passed to the connection, before any modifications.
*
Expand Down Expand Up @@ -596,16 +651,19 @@ public function one_to_one() {
/**
* Returns the loader.
*
* If $loader is not initialized, this method will initialize it.
*
* @return \WPGraphQL\Data\Loader\AbstractDataLoader
* @throws \Exception
*/
protected function getLoader() {
$name = $this->get_loader_name();
if ( empty( $name ) || ! is_string( $name ) ) {
throw new Exception( esc_html__( 'The Connection Resolver needs to define a loader name', 'wp-graphql' ) );
protected function get_loader() {
// If the loader isn't set, set it.
if ( ! isset( $this->loader ) ) {
$name = $this->get_loader_name();

$this->loader = $this->context->get_loader( $name );
}

return $this->context->get_loader( $name );
return $this->loader;
}

/**
Expand Down Expand Up @@ -678,7 +736,7 @@ public function get_connection() {
return new Deferred(
function () {
if ( ! empty( $this->ids ) ) {
$this->loader->load_many( $this->ids );
$this->get_loader()->load_many( $this->ids );
}

/**
Expand Down Expand Up @@ -792,7 +850,7 @@ public function execute_and_get_ids() {
/**
* Buffer the IDs for deferred resolution
*/
$this->loader->buffer( $this->ids );
$this->get_loader()->buffer( $this->ids );

return $this->ids;
}
Expand Down Expand Up @@ -890,7 +948,7 @@ public function get_ids_for_nodes() {
* @throws \Exception
*/
public function get_node_by_id( $id ) {
return $this->loader->load( $id );
return $this->get_loader()->load( $id );
}

/**
Expand Down Expand Up @@ -1068,4 +1126,64 @@ public function get_offset() {

return $this->get_offset_for_cursor( $cursor );
}

/**
* Returns the source of the connection.
*
* @deprecated @todo in favor of $this->get_source().
*
* @return mixed
*/
public function getSource() {
_deprecated_function( __METHOD__, '@todo', static::class . '::get_source()' );

return $this->get_source();
}

/**
* Returns the AppContext of the connection.
*
* @deprecated @todo in favor of $this->get_context().
*/
public function getContext(): AppContext {
_deprecated_function( __METHOD__, '@todo', static::class . '::get_context()' );

return $this->get_context();
}

/**
* Returns the ResolveInfo of the connection.
*
* @deprecated @todo in favor of $this->get_info().
*/
public function getInfo(): ResolveInfo {
_deprecated_function( __METHOD__, '@todo', static::class . '::get_info()' );

return $this->get_info();
}

/**
* Returns whether the connection should execute.
*
* @deprecated @todo in favor of $this->get_should_execute().
*/
public function getShouldExecute(): bool {
_deprecated_function( __METHOD__, '@todo', static::class . '::should_execute()' );

return $this->get_should_execute();
}

/**
* Returns the loader.
*
* @deprecated @todo in favor of $this->get_loader().
*
* @return \WPGraphQL\Data\Loader\AbstractDataLoader
* @throws \Exception
*/
protected function getLoader() {
_deprecated_function( __METHOD__, '@todo', static::class . '::get_loader()' );

return $this->get_loader();
}
}
2 changes: 1 addition & 1 deletion src/Data/Connection/CommentConnectionResolver.php
Expand Up @@ -157,7 +157,7 @@ public function get_query() {
/**
* {@inheritDoc}
*/
public function get_loader_name() {
protected function loader_name(): string {
return 'comment';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/ContentTypeConnectionResolver.php
Expand Up @@ -61,7 +61,7 @@ public function get_query() {
/**
* {@inheritDoc}
*/
public function get_loader_name() {
protected function loader_name(): string {
return 'post_type';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/EnqueuedScriptsConnectionResolver.php
Expand Up @@ -45,7 +45,7 @@ public function get_query() {
/**
* {@inheritDoc}
*/
public function get_loader_name() {
protected function loader_name(): string {
return 'enqueued_script';
}

Expand Down
Expand Up @@ -52,7 +52,7 @@ public function get_query() {
/**
* {@inheritDoc}
*/
public function get_loader_name() {
protected function loader_name(): string {
return 'enqueued_stylesheet';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/PluginConnectionResolver.php
Expand Up @@ -225,7 +225,7 @@ static function ( $plugin ) use ( $s ) {
/**
* {@inheritDoc}
*/
public function get_loader_name() {
protected function loader_name(): string {
return 'plugin';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/PostObjectConnectionResolver.php
Expand Up @@ -69,7 +69,7 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
/**
* {@inheritDoc}
*/
public function get_loader_name() {
protected function loader_name(): string {
return 'post';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/TaxonomyConnectionResolver.php
Expand Up @@ -62,7 +62,7 @@ public function get_query() {
/**
* {@inheritDoc}
*/
public function get_loader_name() {
protected function loader_name(): string {
return 'taxonomy';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/TermObjectConnectionResolver.php
Expand Up @@ -173,7 +173,7 @@ public function get_ids_from_query() {
/**
* {@inheritDoc}
*/
public function get_loader_name() {
protected function loader_name(): string {
return 'term';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/ThemeConnectionResolver.php
Expand Up @@ -56,7 +56,7 @@ public function get_query() {
/**
* {@inheritDoc}
*/
public function get_loader_name() {
protected function loader_name(): string {
return 'theme';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/UserConnectionResolver.php
Expand Up @@ -23,7 +23,7 @@ class UserConnectionResolver extends AbstractConnectionResolver {
/**
* {@inheritDoc}
*/
public function get_loader_name() {
protected function loader_name(): string {
return 'user';
}

Expand Down

0 comments on commit f7309af

Please sign in to comment.