Skip to content

Commit

Permalink
dev!: (hollistic) refactor AbstractConnectionResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
justlevine committed Mar 4, 2023
1 parent ead0acb commit 9c2ed83
Show file tree
Hide file tree
Showing 16 changed files with 1,608 additions and 1,445 deletions.
1,407 changes: 775 additions & 632 deletions src/Data/Connection/AbstractConnectionResolver.php

Large diffs are not rendered by default.

103 changes: 34 additions & 69 deletions src/Data/Connection/CommentConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* @package WPGraphQL\Data\Connection
*/
class CommentConnectionResolver extends AbstractConnectionResolver {

/**
* {@inheritDoc}
*
Expand All @@ -26,13 +25,20 @@ class CommentConnectionResolver extends AbstractConnectionResolver {
/**
* {@inheritDoc}
*/
public function get_query_args() {
protected function loader_name() : string {
return 'comment';
}

/**
* {@inheritDoc}
*/
protected function prepare_query_args( array $args ) : array {

/**
* Prepare for later use
*/
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
$first = ! empty( $this->args['first'] ) ? $this->args['first'] : null;
$last = ! empty( $args['last'] ) ? $args['last'] : null;
$first = ! empty( $args['first'] ) ? $args['first'] : null;

$query_args = [];

Expand All @@ -59,18 +65,18 @@ public function get_query_args() {
$query_args['orderby'] = 'comment_date';

/**
* Take any of the $this->args that were part of the GraphQL query and map their
* Take any of the $args that were part of the GraphQL query and map their
* GraphQL names to the WP_Term_Query names to be used in the WP_Term_Query
*
* @since 0.0.5
*/
$input_fields = [];
if ( ! empty( $this->args['where'] ) ) {
$input_fields = $this->sanitize_input_fields( $this->args['where'] );
if ( ! empty( $args['where'] ) ) {
$input_fields = $this->sanitize_input_fields( $args['where'] );
}

/**
* Merge the default $query_args with the $this->args that were entered
* Merge the default $query_args with the $args that were entered
* in the query.
*
* @since 0.0.5
Expand Down Expand Up @@ -116,14 +122,14 @@ public function get_query_args() {
$query_args['graphql_before_cursor'] = $this->get_before_offset();

/**
* Pass the graphql $this->args to the WP_Query
* Pass the graphql $args to the WP_Query
*/
$query_args['graphql_args'] = $this->args;
$query_args['graphql_args'] = $args;

// encode the graphql args as a cache domain to ensure the
// graphql_args are used to identify different queries.
// see: https://core.trac.wordpress.org/ticket/35075
$encoded_args = wp_json_encode( $this->args );
$encoded_args = wp_json_encode( $args );
$query_args['cache_domain'] = ! empty( $encoded_args ) ? 'graphql:' . md5( $encoded_args ) : 'graphql';

/**
Expand All @@ -133,45 +139,29 @@ public function get_query_args() {
$query_args['fields'] = 'ids';

/**
* Filter the query_args that should be applied to the query. This filter is applied AFTER the input args from
* the GraphQL Query have been applied and has the potential to override the GraphQL Query Input Args.
*
* @param array $query_args array of query_args being passed to the
* @param mixed $source source passed down from the resolve tree
* @param array $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
* Filters the query args used by the connection.
*
* @since 0.0.6
* @param array $query_args The query args to be used with the executable query to get data.
* This should take in the GraphQL args and return args for use in fetching the data.
* @param self $resolver Instance of the ConnectionResolver
*/
return apply_filters( 'graphql_comment_connection_query_args', $query_args, $this->source, $this->args, $this->context, $this->info );
return apply_filters( 'graphql_comment_connection_query_args', $query_args, $this );
}

/**
* Get_query
*
* Return the instance of the WP_Comment_Query
* {@inheritDoc}
*
* @return \WP_Comment_Query
* @throws \Exception
*/
public function get_query() {
return new WP_Comment_Query( $this->query_args );
}

/**
* Return the name of the loader
*
* @return string
*/
public function get_loader_name() {
return 'comment';
protected function query( array $query_args ) {
return new WP_Comment_Query( $query_args );
}

/**
* {@inheritDoc}
*/
public function get_ids_from_query() {
public function get_ids_from_query() : array {
/** @var array $ids */
$ids = ! empty( $this->query->get_comments() ) ? $this->query->get_comments() : [];

Expand All @@ -184,28 +174,16 @@ public function get_ids_from_query() {
}

/**
* This can be used to determine whether the connection query should even execute.
*
* For example, if the $source were a post_type that didn't support comments, we could prevent
* the connection query from even executing. In our case, we prevent comments from even showing
* in the Schema for post types that don't have comment support, so we don't need to worry
* about that, but there may be other situations where we'd need to prevent it.
*
* @return boolean
* {@inheritDoc}
*/
public function should_execute() {
return true;
public function is_valid_offset( $offset ) : bool {
return ! empty( get_comment( $offset ) );
}


/**
* Filters the GraphQL args before they are used in get_query_args().
*
* @return array
* {@inheritDoc}
*/
public function get_args(): array {
$args = $this->args;

protected function prepare_args( array $args ) : array {
if ( ! empty( $args['where'] ) ) {
// Ensure all IDs are converted to database IDs.
foreach ( $args['where'] as $input_key => $input_value ) {
Expand Down Expand Up @@ -252,11 +230,10 @@ public function get_args(): array {
}

/**
*
* Filters the GraphQL args before they are used in get_query_args().
*
* @param array $args The GraphQL args passed to the resolver.
* @param \WPGraphQL\Data\Connection\CommentConnectionResolver $connection_resolver Instance of the ConnectionResolver
* @param array $args The GraphQL args passed to the resolver.
* @param \WPGraphQL\Data\Connection\AbstractConnectionResolver $connection_resolver Instance of the ConnectionResolver.
*
* @since 1.11.0
*/
Expand Down Expand Up @@ -322,17 +299,5 @@ public function sanitize_input_fields( array $args ) {

}

/**
* Determine whether or not the the offset is valid, i.e the comment corresponding to the
* offset exists. Offset is equivalent to comment_id. So this function is equivalent to
* checking if the comment with the given ID exists.
*
* @param int $offset The ID of the node used for the cursor offset
*
* @return bool
*/
public function is_valid_offset( $offset ) {
return ! empty( get_comment( $offset ) );
}

}
64 changes: 24 additions & 40 deletions src/Data/Connection/ContentTypeConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,51 @@ class ContentTypeConnectionResolver extends AbstractConnectionResolver {
/**
* {@inheritDoc}
*/
public function get_ids_from_query() {

$ids = [];
$queried = $this->query;

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

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

return $ids;
protected function loader_name() : string {
return 'post_type';
}

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


/**
* Get the items from the source
* {@inheritDoc}
*
* @return array
*/
public function get_query() {
public function query( array $query_args ) {

if ( isset( $this->query_args['contentTypeNames'] ) && is_array( $this->query_args['contentTypeNames'] ) ) {
return $this->query_args['contentTypeNames'];
if ( isset( $query_args['contentTypeNames'] ) && is_array( $query_args['contentTypeNames'] ) ) {
return $query_args['contentTypeNames'];
}

if ( isset( $this->query_args['name'] ) ) {
return [ $this->query_args['name'] ];
if ( isset( $query_args['name'] ) ) {
return [ $query_args['name'] ];
}

$query_args = $this->query_args;
return \WPGraphQL::get_allowed_post_types( 'names', $query_args );
}

/**
* The name of the loader to load the data
*
* @return string
* {@inheritDoc}
*/
public function get_loader_name() {
return 'post_type';
public function get_ids_from_query() : array {
$ids = [];
$queried = $this->query;

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

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

return $ids;
}

/**
Expand All @@ -77,17 +71,7 @@ public function get_loader_name() {
*
* @return bool
*/
public function is_valid_offset( $offset ) {
public function is_valid_offset( $offset ) : bool {
return (bool) get_post_type_object( $offset );
}

/**
* Determine if the query should execute
*
* @return bool
*/
public function should_execute() {
return true;
}

}

0 comments on commit 9c2ed83

Please sign in to comment.