Skip to content

Commit

Permalink
feat!: Narrow seo field types to their implementations. (#96)
Browse files Browse the repository at this point in the history
* feat!: Narrow `seo` field types to their implementations.

* chore: rename to overload_graphql_field_type()

* fix: overload `HierarchicalContentNode.seo` directly
  • Loading branch information
justlevine authored May 11, 2024
1 parent 26c1233 commit a662dbd
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## [Unreleased]
- feat!: Narrow `seo` field types to their implementations.
- chore!: Bump minimum supported WPGraphQL version to v1.26.0.
- dev: Update `RedirectionConnectionResolver` for v1.26.0 compatibility.
- fix: Correctly resolve `rankMathSettings.homepage.description` field. Props @offminded 🙌
Expand Down
50 changes: 24 additions & 26 deletions docs/reference/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ query MyPosts {
}
robots
title
... on RankMathContentNodeSeo { # Only available on `contentNode` types.
isPillarContent
seoScore {
badgeHtml
hasFrontendScore
rating
score
}
isPillarContent
seoScore {
badgeHtml
hasFrontendScore
rating
score
}
}
author {
Expand Down Expand Up @@ -192,14 +190,12 @@ query MyPosts {
}
robots
title
... on RankMathContentNodeSeo { # Only available on `contentNode` types.
isPillarContent
seoScore {
badgeHtml
hasFrontendScore
rating
score
}
isPillarContent
seoScore {
badgeHtml
hasFrontendScore
rating
score
}
}
}
Expand Down Expand Up @@ -282,14 +278,12 @@ query MyCategories {
}
robots
title
... on RankMathContentNodeSeo { # Only available on `contentNode` types.
isPillarContent
seoScore {
badgeHtml
hasFrontendScore
rating
score
}
isPillarContent
seoScore {
badgeHtml
hasFrontendScore
rating
score
}
}
}
Expand Down Expand Up @@ -330,7 +324,10 @@ query MyCategories {

### Querying with `nodeByUri`

> **Note**: Currently, `nodeByUri` does not detect links associated with a Rank Math Redirection. This will be addressed [in a future release](https://github.com/AxeWP/wp-graphql-rank-math/issues/53).
> [!IMPORTANT]
> Currently, `nodeByUri` does not detect links associated with a Rank Math Redirection. This will be addressed [in a future release](https://github.com/AxeWP/wp-graphql-rank-math/issues/53).
>
> See [Querying Redirections](#querying-redirections) for more information.
```graphql
query MyNodeByUriQuery( $uri: String ) {
Expand Down Expand Up @@ -422,7 +419,8 @@ The Sitemap Module must be enabled in the [Rank Math settings](https://rankmath.

The Redirections Module [must be enabled] in the [Rank Math settings](https://rankmath.com/kb/setting-up-redirections/) for the sitemap data to be available.

> **Note**: Currently, `nodeByUri` does not detect links associated with a Rank Math Redirection. This will be addressed [in a future release](https://github.com/AxeWP/wp-graphql-rank-math/issues/53).
> [!NOTE]
> Currently, `nodeByUri` does not detect links associated with a Rank Math Redirection. This will be addressed [in a future release](https://github.com/AxeWP/wp-graphql-rank-math/issues/53).
>
> In the interim, we recommend handling redirects in your frontend app, such as with the [NextJS `redirects` config key](https://nextjs.org/docs/pages/api-reference/next-config-js/redirects).
Expand Down
7 changes: 7 additions & 0 deletions src/Type/WPInterface/NodeWithSeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use WPGraphQL\RankMath\Model\ContentTypeSeo;
use WPGraphQL\RankMath\Model\TermNodeSeo;
use WPGraphQL\RankMath\Model\UserSeo;
use WPGraphQL\RankMath\Type\WPInterface\ContentNodeSeo as WPInterfaceContentNodeSeo;
use WPGraphQL\RankMath\Utils\Utils;
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Abstracts\InterfaceType;
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Interfaces\TypeWithInterfaces;

Expand Down Expand Up @@ -50,6 +52,11 @@ public static function register(): void {
// @todo only apply to ContentTypes that have SEO data.

register_graphql_interfaces_to_types( self::type_name(), $types_with_seo );

// Narrow down ContentNode types.
Utils::overload_graphql_field_type( 'ContentNode', 'seo', WPInterfaceContentNodeSeo::get_type_name() );
// This is necessary because the filter doesn't work for inheritance.
Utils::overload_graphql_field_type( 'HierarchicalContentNode', 'seo', WPInterfaceContentNodeSeo::get_type_name() );
}

/**
Expand Down
25 changes: 19 additions & 6 deletions src/Type/WPObject/SeoObjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use WPGraphQL;
use WPGraphQL\RankMath\Type\WPInterface\ContentNodeSeo;
use WPGraphQL\RankMath\Type\WPInterface\Seo;
use WPGraphQL\RankMath\Utils\Utils;
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Interfaces\Registrable;
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Traits\TypeNameTrait;

Expand All @@ -37,9 +38,10 @@ public static function register(): void {

foreach ( $post_types as $post_type ) {
/** @var \WP_Post_Type $post_type */
// Register Post Objects seo.
$type_name_for_post_object = 'RankMath' . graphql_format_type_name( $post_type->graphql_single_name . 'ObjectSeo' );
// Register Post Object seo.
register_graphql_object_type(
'RankMath' . graphql_format_type_name( $post_type->graphql_single_name . 'ObjectSeo' ),
$type_name_for_post_object,
[
// translators: %s is the post type name.
'description' => sprintf( __( 'The %s post object SEO data', 'wp-graphql-rank-math' ), $post_type->name ),
Expand All @@ -49,9 +51,13 @@ public static function register(): void {
]
);

// Register Post Object's SEO field.
Utils::overload_graphql_field_type( $post_type->graphql_single_name, 'seo', $type_name_for_post_object );

// Register Post Type seo.
$type_name_for_post_type = 'RankMath' . graphql_format_type_name( $post_type->graphql_single_name . 'TypeSeo' );
register_graphql_object_type(
'RankMath' . graphql_format_type_name( $post_type->graphql_single_name . 'TypeSeo' ),
$type_name_for_post_type,
[
// translators: %s is the post type name.
'description' => sprintf( __( 'The %s post type object SEO data', 'wp-graphql-rank-math' ), $post_type->name ),
Expand All @@ -67,9 +73,9 @@ public static function register(): void {

foreach ( $taxonomies as $taxonomy ) {
/** @var \WP_Taxonomy $taxonomy */
$name = 'RankMath' . graphql_format_type_name( $taxonomy->graphql_single_name . 'TermSeo' );
$type_name_for_term = 'RankMath' . graphql_format_type_name( $taxonomy->graphql_single_name . 'TermSeo' );
register_graphql_object_type(
$name,
$type_name_for_term,
[
// translators: %s is the tax term name.
'description' => sprintf( __( 'The %s term object SEO data', 'wp-graphql-rank-math' ), $taxonomy->name ),
Expand All @@ -78,11 +84,15 @@ public static function register(): void {
'eagerlyLoadType' => true,
]
);

// Register Term Object's SEO field.
Utils::overload_graphql_field_type( $taxonomy->graphql_single_name, 'seo', $type_name_for_term );
}

// Register user object seo.
$type_name_for_user = 'RankMathUserSeo';
register_graphql_object_type(
graphql_format_type_name( 'RankMathUserSeo' ),
$type_name_for_user,
[
'description' => __( 'The user object SEO data', 'wp-graphql-rank-math' ),
'interfaces' => [ Seo::get_type_name() ],
Expand All @@ -103,5 +113,8 @@ public static function register(): void {
'eagerlyLoadType' => true,
]
);

// Register User Object's SEO field.
Utils::overload_graphql_field_type( 'User', 'seo', $type_name_for_user );
}
}
26 changes: 26 additions & 0 deletions src/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,30 @@ public static function base_url( string $path = '' ): string {

return user_trailingslashit( $base_url );
}

/**
* Overloads the field type of an existing GraphQL field.
*
* This is necessary because register_graphql_field() doesn't have a way to check inheritance.
*
* @see https://github.com/wp-graphql/wp-graphql/issues/3096
*
* @param string $object_type The WPGraphQL object type name where the field is located.
* @param string $field_name The field name to overload.
* @param string $new_type_name The new GraphQL type name to use.
*/
public static function overload_graphql_field_type( string $object_type, string $field_name, string $new_type_name ): void {
add_filter(
'graphql_' . $object_type . '_fields',
static function ( array $fields ) use ( $field_name, $new_type_name ) {
if ( isset( $fields[ $field_name ] ) ) {
$fields[ $field_name ]['type'] = $new_type_name;
}

return $fields;
},
10,
1
);
}
}
10 changes: 4 additions & 6 deletions tests/functional/UserSeoQueryCept.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,15 @@
title
}
}
... on RankMathUserSeo {
additionalProfiles
facebookProfileUrl
twitterUserName
}
additionalProfiles
facebookProfileUrl
twitterUserName
}
}
}
',
'variables' => [
'id' => $user_id,
'id' => $user_id,
],
]
)
Expand Down
14 changes: 6 additions & 8 deletions tests/wpunit/ContentNodeSeoQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ protected function get_query() : string {
jsonLd {
raw
}
... on RankMathContentNodeSeo {
isPillarContent
seoScore {
badgeHtml
hasFrontendScore
rating
score
}
isPillarContent
seoScore {
badgeHtml
hasFrontendScore
rating
score
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions tests/wpunit/UserSeoQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ public function testUserSeo() {
}
robots
title
... on RankMathUserSeo {
additionalProfiles
facebookProfileUrl
twitterUserName
}
additionalProfiles
facebookProfileUrl
twitterUserName
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php return array(
'root' => array(
'name' => 'axepress/wp-graphql-rank-math',
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => 'c9b62d7260ed1cb9ad215e36675e6f1581395548',
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '26c12338bf979a09d681555c5d8ee84419e94e6d',
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -20,9 +20,9 @@
'dev_requirement' => false,
),
'axepress/wp-graphql-rank-math' => array(
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => 'c9b62d7260ed1cb9ad215e36675e6f1581395548',
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '26c12338bf979a09d681555c5d8ee84419e94e6d',
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down

0 comments on commit a662dbd

Please sign in to comment.