Skip to content

Commit

Permalink
Merge pull request #6 from harness-software/feature/473-update-wpgrap…
Browse files Browse the repository at this point in the history
…hql-for-posts2posts-to-include-input-arguments-Natac-update

feature/473 update wpgraphql for posts2posts to include input arguments Natac update
  • Loading branch information
natac13 authored Oct 1, 2021
2 parents e4c8e48 + 8255cce commit 78ed127
Show file tree
Hide file tree
Showing 26 changed files with 4,182 additions and 102 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

!.env.dist
!tests
.devcontainer.json
.DS_Store
.env
.env.*
.idea
.log/
.vscode
*.sql
*.tar.gz
.scannerwork
*.zip
build/
composer.lock
coverage/*
node_modules/
phpcs.xml
phpunit.xml
schema.graphql
sonar-project.properties
sonar-scanner
tests/*.suite.yml
Thumbs.db
wp-cli.local.yml
vendor/*
!vendor/composer
!vendor/composer/*
!vendor/autoload.php
4 changes: 2 additions & 2 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<!-- Value: replace the function, class, and variable prefixes used. Separate multiple prefixes with a comma. -->
<property name="prefixes" type="array" value="Harness"/>
<property name="prefixes" type="array" value="WPGraphQLPostsToPosts"/>
</properties>
</rule>
<rule ref="WordPress.WP.I18n">
<properties>
<!-- Value: replace the text domain used. -->
<property name="text_domain" type="array" value="harness"/>
<property name="text_domain" type="array" value="wp-graphql-posts-to-posts"/>
</properties>
</rule>
<rule ref="WordPress.WhiteSpace.ControlStructureSpacing">
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ query getUsers {
## Minimum Software Requirements

- PHP 7.4+
- [WPGraphQL](https://github.com/wp-graphql/wp-graphql) 0.12.1+
- [WPGraphQL](https://github.com/wp-graphql/wp-graphql) 1.0.0+
- [Posts 2 Posts](https://wordpress.org/plugins/posts-to-posts/) 1.6.6+

## Future Enhancements
Expand Down
35 changes: 34 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,43 @@
"email": "sean@harnessup.com"
}
],
"require": {},
"require": {
"php": ">=7.4"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"wp-coding-standards/wpcs": "^2.3",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"squizlabs/php_codesniffer": "^3.6",
"phpstan/phpstan": "^0.12",
"szepeviktor/phpstan-wordpress": "^0.7",
"phpstan/extension-installer": "^1.1",
"simpod/php-coveralls-mirror": "^3.0",
"poolshark/wp-graphql-stubs": "^0.0.2"
},
"autoload": {
"psr-4": {
"WPGraphQLPostsToPosts\\": "src/"
}
},
"config": {
"optimize-autoloader": true,
"process-timeout": 0
},
"scripts": {
"install-test-env": "bash bin/install-test-env.sh",
"lint": "vendor/bin/phpcs",
"phpcs-i": [
"php ./vendor/bin/phpcs -i"
],
"check-cs": [
"php ./vendor/bin/phpcs src"
],
"fix-cs": [
"php ./vendor/bin/phpcbf src"
],
"phpstan": [
"phpstan analyze --ansi --memory-limit=1G"
]
}
}
17 changes: 17 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
parameters:
level: 8
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
featureToggles:
disableRuntimeReflectionProvider: true
bootstrapFiles:
- wp-graphql-posts-to-posts.php
paths:
- wp-graphql-posts-to-posts.php
- src/
scanDirectories:
- ../wp-graphql/
- ../posts-to-posts/
ignoreErrors:
- '#^Call to an undefined method object::disconnect\(\).$#'
- '#^Call to an undefined method object::connect\(\).$#'
21 changes: 21 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== WPGraphQL for Posts 2 Posts ===
Contributors: kellenmace, natac13, justlevine
Tags: GraphQL, Gatsby, Headless, Posts2Posts
Requires at least: 5.4.1
Tested up to: 5.8
Requires PHP: 7.4
Requires Posts To Posts: 1.6.6+
Requires WPGraphQL: 1.0.0+
Stable tag: 0.2.0
Maintained at: https://github.com/harness-software/wp-graphql-posts-to-posts
License: GPL-3
License URI: https://www.gnu.org/licenses/gpl-3.0.html

== Description ==
WordPress plugin that creates GraphQL connections for all of your [Posts 2 Posts](https://wordpress.org/plugins/posts-to-posts/) connections. For more information, see [README.md](https://github.com/harness-software/wp-graphql-posts-to-posts/blob/master/README.md)

== Upgrade Notice ==
== Frequently Asked Questions ==
== Screenshots ==

== Changelog ==
64 changes: 15 additions & 49 deletions src/Connections/ConnectionsRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,23 @@

namespace WPGraphQLPostsToPosts\Connections;

use P2P_Connection_Type;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Data\Connection;
use WPGraphQL\Model\User;
use WPGraphQLPostsToPosts\Interfaces\Hookable;
use WPGraphQLPostsToPosts\Traits\ObjectsTrait;
use WPGraphQLPostsToPosts\Types\Fields;

class ConnectionsRegistrar implements Hookable {
/**
* Registered Posts2Posts connections.
*
* @var array
*/
private $p2p_connections = [];

/**
* Post types exposed in the GraphQL schema.
*
* @var array
*/
private $post_types = [];
use ObjectsTrait;

public function register_hooks() {
add_action( 'p2p_registered_connection_type', [ $this, 'capture_p2p_connections' ], 10, 2 );
add_action( 'graphql_register_types', [ $this, 'set_post_types_property' ] );
add_action( 'graphql_register_types', [ $this, 'register_connections' ], 11 );
public function register_hooks() : void {
add_action( get_graphql_register_action(), [ $this, 'register_connections' ], 11 );
}

public function capture_p2p_connections( P2P_Connection_Type $ctype, array $args ) : void {
$this->p2p_connections[] = $args;
}

public function set_post_types_property() {
$this->post_types = get_post_types( [ 'show_in_graphql' => true ], 'objects' );
}

public function register_connections() {
$p2p_connections_to_map = array_filter( $this->p2p_connections, [ $this, 'should_create_connection' ] );
public function register_connections() : void {
$p2p_connections_to_map = Fields::get_p2p_connections();

foreach ( $p2p_connections_to_map as $p2p_connection ) {
// Register from -> to connection.
Expand Down Expand Up @@ -69,13 +48,13 @@ public function register_connections() {
private function register_connection( array $args ) : void {
register_graphql_connection(
[
'fromType' => $this->get_graphql_single_name( $args['from_object_name'] ),
'toType' => $this->get_graphql_single_name( $args['to_object_name'] ),
'fromType' => self::get_graphql_single_name( $args['from_object_name'] ),
'toType' => self::get_graphql_single_name( $args['to_object_name'] ),
'fromFieldName' => graphql_format_field_name( $args['connection_name'] . 'Connection' ),
'resolve' => function( $source, array $request_args, AppContext $context, ResolveInfo $info ) use ( $args ) {
// We need to query for connected users.
if ( 'user' === $args['to_object_name'] ) {
$resolver = new Connection\UserConnectionResolver( $source, $request_args, $context, $info, $args['to_object_name'] );
$resolver = new Connection\UserConnectionResolver( $source, $request_args, $context, $info );
// We need to query for connected posts.
} else {
$resolver = new Connection\PostObjectConnectionResolver( $source, $request_args, $context, $info, $args['to_object_name'] );
Expand All @@ -94,27 +73,14 @@ private function register_connection( array $args ) : void {
);
}

private function should_create_connection( array $connection ) : bool {
return $this->should_connect_object( $connection['from'] )
&& $this->should_connect_object( $connection['to'] );
}

private function should_connect_object( string $object_name ) : bool {
return 'user' === $object_name || $this->is_post_type_in_schema( $object_name );
}

private function is_post_type_in_schema( string $post_type_name ) : bool {
$post_type_names = array_map( fn( $post_type ) => $post_type->name, $this->post_types );

return in_array( $post_type_name, $post_type_names, true );
}

private function get_graphql_single_name( string $object_name ) : string {
private static function get_graphql_single_name( string $object_name ) : string {
if ( 'user' === $object_name ) {
return 'User';
}

$post_object = $this->array_find( $this->post_types, fn( $post_type ) => $post_type->name === $object_name );
$post_types = self::get_post_types();

$post_object = self::array_find( $post_types, fn( $post_type ) => $post_type->name === $object_name );

return $post_object->graphql_single_name;
}
Expand All @@ -128,7 +94,7 @@ private function get_graphql_single_name( string $object_name ) : string {
*
* @return mixed The value of the element, or null if not found.
*/
private function array_find( array $array, callable $callback ) {
private static function array_find( array $array, callable $callback ) {
foreach ( $array as $key => $value ) {
if ( $callback( $value, $key, $array ) ) {
return $value;
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/Hookable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface Hookable {
*
* @return void
*/
public function register_hooks();
public function register_hooks() : void;
}
60 changes: 60 additions & 0 deletions src/Mutations/AbstractMutation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Abstract Mutation class.
*/
namespace WPGraphQLPostsToPosts\Mutations;

use GraphQL\Error\UserError;
use WPGraphQLPostsToPosts\Interfaces\Hookable;
use WPGraphQLPostsToPosts\Traits\ObjectsTrait;

abstract class AbstractMutation implements Hookable {
use ObjectsTrait;

public function register_hooks() : void {
add_action( get_graphql_register_action(), [ $this, 'register_input_fields' ] );
}

abstract public function register_input_fields() : void;

/**
* Register the P2P Connection
*
* @param integer $from_id
* @param integer $to_id
* @param string $connected_type
*
* @throws UserError No Posts2Posts connection for $connected_type.
*/
public function connect_p2p_type( int $from_id, int $to_id, string $connected_type ) : void {
$p2p_type = p2p_type( $connected_type );

if ( ! is_object( $p2p_type ) ) {
throw new UserError(
// translators: P2P connnection type name.
sprintf( __( 'No Posts2Posts connection type found for %s', 'wp-graphql-posts-to-posts' ), $connected_type )
);
}

$p2p_type->connect(
$from_id,
$to_id,
);
}

public function disconnect_p2p_type( int $from_id, int $to_id, string $connected_type ) : void {
$p2p_type = p2p_type( $connected_type );

if ( ! is_object( $p2p_type ) ) {
throw new UserError(
// translators: P2P connnection type name.
sprintf( __( 'No Posts2Posts connection type found for %s', 'wp-graphql-posts-to-posts' ), $connected_type )
);
}

$p2p_type->disconnect(
$from_id,
$to_id,
);
}
}
Loading

0 comments on commit 78ed127

Please sign in to comment.