Skip to content

Commit

Permalink
chore: rebase remaining changes
Browse files Browse the repository at this point in the history
  • Loading branch information
justlevine committed May 4, 2024
1 parent 765dfcf commit eeaed24
Show file tree
Hide file tree
Showing 17 changed files with 829 additions and 538 deletions.
561 changes: 196 additions & 365 deletions src/Data/Connection/AbstractConnectionResolver.php

Large diffs are not rendered by default.

24 changes: 10 additions & 14 deletions src/Data/Connection/CommentConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,12 @@ protected function prepare_query_args( array $args ): array {
/**
* Filters the query args used by the connection.
*
* @param array<string,mixed> $query_args array of query_args being passed to the
* @param mixed $source source passed down from the resolve tree
* @param array<string,mixed> $args array of arguments input in the field as part of the GraphQL query
* @param \WPGraphQL\AppContext $context object passed down the resolve tree
* @param \GraphQL\Type\Definition\ResolveInfo $info info about fields passed down the resolve tree
* @param array<string,mixed> $query_args The query args to be used with the executable query to get data.
* @param self $resolver The AbstractConnectionResolver instance.
*
* @since 0.0.6
*/
return apply_filters( 'graphql_comment_connection_query_args', $query_args, $this->source, $args, $this->context, $this->info );
return apply_filters( 'graphql_comment_connection_query_args', $query_args, $this );
}

/**
Expand All @@ -153,13 +150,9 @@ protected function loader_name(): string {
/**
* {@inheritDoc}
*/
public function get_ids_from_query() {
/**
* @todo This is for b/c. We can just use $this->get_query().
*/
$queried = isset( $this->query ) ? $this->query : $this->get_query();
public function get_ids_from_query(): array {
$queried = $this->get_query();
$comments = $queried->get_comments();

/** @var int[]|string[] $ids */
$ids = ! empty( $comments ) ? $comments : [];

Expand Down Expand Up @@ -289,9 +282,12 @@ public function sanitize_input_fields( array $args ) {
* This allows plugins/themes to hook in and alter what $args should be allowed to be passed
* from a GraphQL Query to the get_terms query
*
* @param array<string,mixed> $query_args The array of query arguments
* @param self $resolver The instance of the CommentConnectionResolver
*
* @since 0.0.5
*/
$query_args = apply_filters( 'graphql_map_input_fields_to_wp_comment_query', $query_args, $args, $this->source, $this->get_args(), $this->context, $this->info );
$query_args = apply_filters( 'graphql_map_input_fields_to_wp_comment_query', $query_args, $this );

return ! empty( $query_args ) && is_array( $query_args ) ? $query_args : [];
}
Expand All @@ -301,7 +297,7 @@ public function sanitize_input_fields( array $args ) {
*
* @param int $offset The ID of the node used for the cursor offset.
*/
public function is_valid_offset( $offset ) {
public function is_valid_offset( $offset ): bool {
return ! empty( get_comment( $offset ) );
}
}
213 changes: 213 additions & 0 deletions src/Data/Connection/ConnectionResolverShim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?php

namespace WPGraphQL\Data\Connection;

use Exception;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;

/**
* Class ConnectionResolverShim - Shim for ConnectionResolver 2.0
*
* Note: This is NOT a true shim, but rather illustrates the changes that need to be made to convert classes to use the new ConnectionResolver.
*
* Issues like changes in method signatures (visibility, arg/return types, etc) are not addressed here.
*
* @package WPGraphQL\Data\Connection
* @extends \WPGraphQL\Data\Connection\AbstractConnectionResolver<mixed[]|object|mixed>
*/
abstract class ConnectionResolverShim extends AbstractConnectionResolver {

/**
* Returns the source of the connection
*
* @deprecated @todo
* @return mixed
*
* @codeCoverageIgnore
*/
public function getSource() {
_deprecated_function( __METHOD__, '@todo', parent::class . '::get_source()' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return $this->get_source();
}

/**
* Get the loader name
*
* @deprecated @todo
*
* @return \WPGraphQL\Data\Loader\AbstractDataLoader
*
* @codeCoverageIgnore
*/
protected function getLoader() {
_deprecated_function( __METHOD__, '@todo', parent::class . '::get_loader()' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

return $this->get_loader();
}

/**
* Returns the $args passed to the connection
*
* @deprecated @todo
*
* @return array<string,mixed>
*
* @codeCoverageIgnore
*/
public function getArgs(): array {
_deprecated_function( __METHOD__, '1.11.0', static::class . '::get_args()' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return $this->get_args();
}

/**
* Returns the AppContext of the connection
*
* @deprecated @todo
*
* @codeCoverageIgnore
*/
public function getContext(): AppContext {
_deprecated_function( __METHOD__, '@todo', parent::class . '::get_context()' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return $this->get_context();
}

/**
* Returns the ResolveInfo of the connection
*
* @deprecated @todo
*
* @codeCoverageIgnore
*/
public function getInfo(): ResolveInfo {
_deprecated_function( __METHOD__, '@todo', parent::class . '::get_info()' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return $this->get_info();
}

/**
* Returns whether the connection should execute
*
* @deprecated @todo
*
* @codeCoverageIgnore
*/
public function getShouldExecute(): bool {
_deprecated_function( __METHOD__, '@todo', parent::class . '::get_should_execute()' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return $this->get_should_execute();
}

/**
* @param string $key The key of the query arg to set
* @param mixed $value The value of the query arg to set
*
* @return static
*
* @deprecated 0.3.0
*
* @codeCoverageIgnore
*/
public function setQueryArg( $key, $value ) {
_deprecated_function( __METHOD__, '0.3.0', static::class . '::set_query_arg()' );

return $this->set_query_arg( $key, $value );
}

/**
* {@inheritDoc}
*
* Method `get_loader_name()` is no longer abstract.
*
* Overloading should be done via `loader_name()`.
*
* @throws \Exception
*/
protected function loader_name(): string {
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' ),
static::class
)
);
}

/**
* {@inheritDoc}
*
* Method `get_query_args()` is no longer abstract.
*
* Overloading should be done via `prepare_query_args()`.
*
* @throws \Exception
*/
protected function prepare_query_args( array $args ): array {
throw new Exception(
sprintf(
// translators: %s is the name of the connection resolver class.
esc_html__( 'Class %s does not implement a valid method `prepare_query_args()`.', 'wp-graphql' ),
static::class
)
);
}

/**
* Method `get_query()` is no longer abstract. Overloading (if necesary) should be done via `query()` and `query_class()`.
*/

/**
* Method `should_execute()` is now protected and no longer abstract. It defaults to `true`.
*
* Overload `should_execute()` or `pre_should_execute()` should only occur if there is a reason the connecton should not always execute..
*/

/**
* This method is now abstract.
*
* {@inheritDoc}
*
* @throws \Exception
*/
public function get_ids_from_query(): array {
throw new Exception(
sprintf(
// translators: %s is the name of the connection resolver class.
esc_html__( 'Class %s does not implement a valid method `get_ids_from_query()`.', 'wp-graphql' ),
static::class
)
);
}

/**
* This returns the offset to be used in the $query_args based on the $args passed to the
* GraphQL query.
*
* @deprecated 1.9.0
*
* @codeCoverageIgnore
*
* @return int|mixed
*/
public function get_offset() {
_deprecated_function( __METHOD__, '1.9.0', static::class . '::get_offset_for_cursor()' );

$args = $this->get_args();

// Using shorthand since this is for deprecated code.
$cursor = $args['after'] ?? null;
$cursor = $cursor ?: ( $args['before'] ?? null );

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

/**
* Method get_nodes() should now be overloaded `prepare_nodes()`.
*/

/**
* Method `get_edges()` should be overloaded `prepare_edges()`.
*/

/**
* Method `get_page_info()` should now be overloaded via `prepare_page_info()`.
*/
}
8 changes: 4 additions & 4 deletions src/Data/Connection/ContentTypeConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class ContentTypeConnectionResolver extends AbstractConnectionResolver {
/**
* {@inheritDoc}
*/
public function get_ids_from_query() {
public function get_ids_from_query(): array {
$ids = [];
$queried = $this->query;
$queried = $this->get_query();

if ( empty( $queried ) ) {
return $ids;
Expand All @@ -37,7 +37,7 @@ protected function prepare_query_args( array $args ): array {
/**
* {@inheritDoc}
*/
protected function query( array $query_args ) {
public function query( array $query_args ) {
if ( isset( $query_args['contentTypeNames'] ) && is_array( $query_args['contentTypeNames'] ) ) {
return $query_args['contentTypeNames'];
}
Expand All @@ -61,7 +61,7 @@ protected function loader_name(): string {
*
* @param string $offset The offset (post type name) to check.
*/
public function is_valid_offset( $offset ) {
public function is_valid_offset( $offset ): bool {
return (bool) get_post_type_object( $offset );
}
}
6 changes: 3 additions & 3 deletions src/Data/Connection/EnqueuedScriptsConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EnqueuedScriptsConnectionResolver extends AbstractConnectionResolver {
/**
* {@inheritDoc}
*/
public function get_ids_from_query() {
public function get_ids_from_query(): array {
$ids = [];
$queried = $this->get_query();

Expand Down Expand Up @@ -60,14 +60,14 @@ protected function max_query_amount(): int {
*
* @param ?\_WP_Dependency $model The model to check.
*/
protected function is_valid_model( $model ) {
protected function is_valid_model( $model ): bool {
return isset( $model->handle );
}

/**
* {@inheritDoc}
*/
public function is_valid_offset( $offset ) {
public function is_valid_offset( $offset ): bool {
global $wp_scripts;
return isset( $wp_scripts->registered[ $offset ] );
}
Expand Down
47 changes: 2 additions & 45 deletions src/Data/Connection/EnqueuedStylesheetConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,8 @@
* Class EnqueuedStylesheetConnectionResolver
*
* @package WPGraphQL\Data\Connection
* @extends \WPGraphQL\Data\Connection\AbstractConnectionResolver<string[]>
*/
class EnqueuedStylesheetConnectionResolver extends AbstractConnectionResolver {
/**
* {@inheritDoc}
*/
public function get_ids_from_query() {
$ids = [];
$queried = $this->get_query();

if ( empty( $queried ) ) {
return $ids;
}

foreach ( $queried as $key => $item ) {
$ids[ $key ] = $item;
}

return $ids;
}

/**
* {@inheritDoc}
*/
protected function prepare_query_args( array $args ): array {
// If any args are added to filter/sort the connection
return [];
}

class EnqueuedStylesheetConnectionResolver extends EnqueuedScriptsConnectionResolver {
/**
* {@inheritDoc}
*/
Expand All @@ -51,23 +24,7 @@ protected function loader_name(): string {
/**
* {@inheritDoc}
*/
protected function max_query_amount(): int {
return 1000;
}

/**
* {@inheritDoc}
*
* @param ?\_WP_Dependency $model
*/
protected function is_valid_model( $model ) {
return isset( $model->handle );
}

/**
* {@inheritDoc}
*/
public function is_valid_offset( $offset ) {
public function is_valid_offset( $offset ): bool {
global $wp_styles;
return isset( $wp_styles->registered[ $offset ] );
}
Expand Down

0 comments on commit eeaed24

Please sign in to comment.