Skip to content

Commit

Permalink
Merge pull request #140 from jasonbahl/feature/#4-post-mutations
Browse files Browse the repository at this point in the history
PostObjectMutations
  • Loading branch information
jasonbahl committed May 26, 2017
2 parents f565f3e + d968f6e commit 6cb33f7
Show file tree
Hide file tree
Showing 61 changed files with 1,869 additions and 490 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ matrix:
# Composer package installation
install:
# Install composer packages, will also trigger dump-autoload
- composer install --no-interaction
- composer install
# Install coveralls.phar
- wget -c -nc --retry-connrefused --tries=0 https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
- chmod +x coveralls.phar
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/AppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@ class AppContext {
*/
public $request;

/**
* Stores additional $config properties
* @var \mixed $config
* @access public
*/
public $config;

/**
* AppContext constructor.
*/
public function __construct() {

$this->config = apply_filters( 'graphql_app_context_config', $this->config );

}

}
10 changes: 0 additions & 10 deletions src/Data/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ public function graphql_wp_query_cursor_pagination_support( $where, \WP_Query $q
} else {
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID {$compare} %d", $cursor_offset );
}

}

}

return $where;
Expand Down Expand Up @@ -125,9 +123,7 @@ public function graphql_wp_term_query_cursor_pagination_support( array $pieces,
} else {
$pieces['where'] .= sprintf( ' AND t.term_id %1$s %2$d', $compare, $cursor_offset );
}

}

}

return $pieces;
Expand Down Expand Up @@ -161,18 +157,12 @@ public function graphql_wp_comments_query_cursor_pagination_support( array $piec
// Get the $cursor_post
$cursor_comment = get_comment( $cursor_offset );

// var_dump( $pieces['where'] );

if ( ! empty( $cursor_comment ) && ! empty( $cursor_comment->comment_date ) ) {
$pieces['where'] .= sprintf( ' AND comment_date %1$s= "%3$s" AND NOT ( comment_date %2$s= "%3$s" AND comment_ID %2$s= %4$d )', $compare, $compare_opposite, esc_html( $cursor_comment->comment_date ), absint( $cursor_offset ) );
} else {
$pieces['where'] .= sprintf( ' AND comment_ID %1$s %2$d', $compare, $cursor_offset );
}

// var_dump( $pieces['where'] );

}

}

return $pieces;
Expand Down
40 changes: 9 additions & 31 deletions src/Data/ConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function resolve( $source, $args, AppContext $context, ResolveInfo
$query_args = static::get_query_args( $source, $args, $context, $info );
$query = static::get_query( $query_args );
$array_slice = self::get_array_slice( $query, $args );
$connection = static::get_connection( $array_slice, $args, $query );
$connection = static::get_connection( $query, $array_slice, $source, $args, $context, $info );

/**
* Filter the connection, and provide heaps of info to make it easy to filter very specific cases
Expand Down Expand Up @@ -66,13 +66,16 @@ public static function resolve( $source, $args, AppContext $context, ResolveInfo
/**
* Take an array return a connection
*
* @param array $array
* @param array $args
* @param $query
* @param mixed $query The query being performed
* @param array $array The array of connection items
* @param mixed $source The source being passed down the resolve tree
* @param array $args The args for the field being resolved
* @param AppContext $context The context being passed down the Resolve tree
* @param ResolveInfo $info The ResolveInfo for the field being resolved
*
* @return array
*/
public static function get_connection( array $array, array $args, $query ) {
public static function get_connection( $query, array $array, $source, array $args, AppContext $context, ResolveInfo $info ) {

$meta = self::get_array_meta( $query, $args );
$connection = ArrayConnection::connectionFromArray( $array, $args, $meta );
Expand All @@ -81,32 +84,6 @@ public static function get_connection( array $array, array $args, $query ) {

}

/**
* Returns the query_order to use when making queries
*
* If First is set, select the amount of records asked for from the front of the data set by using the
* specified order.
*
* If Last is set, select the amount of records asked for from the back of the data set by reversing the
* specified order.
*
*
*/
public static function get_query_order( $args, $query_args ) {

$possible_orders = [ 'ASC', 'DESC' ];
$query_order = ( ! empty( $query_args['order'] && in_array( $query_args['order'], $possible_orders, true ) ) ) ? $query_args['order'] : 'DESC';
if ( ! empty( $args['last'] ) && empty( $args['before'] && empty( $args['after'] ) ) ) {
if ( 'ASC' === $query_order ) {
$query_order = 'DESC';
} elseif ( 'DESC' === $query_order ) {
$query_order = 'ASC';
}
}

return $query_order;
}

/**
* This returns a slice of the query results based on the posts retrieved and the $args passed to the query
*
Expand Down Expand Up @@ -136,6 +113,7 @@ public static function get_array_slice( $query, array $args ) {
// the \WP_User_Query doesn't have proper filters to allow for true cursor based pagination
case $item instanceof \WP_User:
$array_slice[] = $item;
break;
default:
$array_slice[] = $item;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ public function process_http_request() {
$response['errors'] = __( 'GraphQL Queries must be a POST Request with a valid query', 'wp-graphql' );
}

$request = isset( $data['query'] ) ? $data['query'] : null;
$operation_name = isset( $data['operationName'] ) ? $data['operationName'] : null;
$variables = isset( $data['variables'] ) ? $data['variables'] : null;
$request = isset( $data['query'] ) ? $data['query'] : '';
$operation_name = isset( $data['operationName'] ) ? $data['operationName'] : '';
$variables = isset( $data['variables'] ) ? $data['variables'] : '';

/**
* Process the GraphQL request
Expand Down Expand Up @@ -322,7 +322,7 @@ public function process_http_request() {
} else {
$response['errors'] = [ FormattedError::create( 'Unexpected error' ) ];
}
}
} // End try().

/**
* Run an action after the HTTP Response is ready to be sent back. This might be a good place for tools
Expand Down
39 changes: 22 additions & 17 deletions src/Type/Avatar/AvatarType.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace WPGraphQL\Type\Avatar;

use GraphQL\Type\Definition\ResolveInfo;
Expand All @@ -8,39 +9,44 @@

/**
* Class AvatarType
*
* @package WPGraphQL\Type
* @since 0.0.5
* @since 0.0.5
*/
class AvatarType extends WPObjectType {

/**
* Holds the type name
*
* @var string $type_name
*/
private static $type_name;

/**
* This holds the field definitions
*
* @var array $fields
* @since 0.0.5
*/
private static $fields;

/**
* WPObjectType constructor.
*
* @since 0.0.5
*/
public function __construct() {

/**
* Set the type_name
*
* @since 0.0.5
*/
self::$type_name = 'avatar';

$config = [
'name' => self::$type_name,
'fields' => self::fields(),
'name' => self::$type_name,
'fields' => self::fields(),
'description' => esc_html__( 'Avatars are profile images for users. WordPress by default uses the Gravatar service to host and fetch avatars from.', 'wp-graphql' ),
];

Expand All @@ -59,56 +65,55 @@ public function __construct() {
*/
private static function fields() {

if ( null === self::$fields ) {

if ( null === self::$fields ) :
self::$fields = function() {
$fields = [
'size' => [
'size' => [
'type' => Types::int(),
'description' => esc_html__( 'The size of the avatar in pixels. A value of 96 will match a 96px x 96px gravatar image.', 'wp-graphql' ),
],
'height' => [
'height' => [
'type' => Types::int(),
'description' => esc_html__( 'Height of the avatar image.', 'wp-graphql' ),
],
'width' => [
'width' => [
'type' => Types::int(),
'description' => esc_html__( 'Width of the avatar image.', 'wp-graphql' ),
],
'default' => [
'default' => [
'type' => Types::string(),
'description' => esc_html__( "URL for the default image or a default type. Accepts '404' (return a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster), 'wavatar' (cartoon face), 'indenticon' (the 'quilt'), 'mystery', 'mm', or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or 'gravatar_default' (the Gravatar logo).", 'wp-graphql' ),
],
'forceDefault' => [
'type' => Types::boolean(),
'description' => esc_html__( 'Whether to always show the default image, never the Gravatar.', 'wp-graphql' ),
'resolve' => function( $avatar, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $avatar['force_default'] ) ? $avatar['force_default'] : null;
'resolve' => function( $avatar, array $args, AppContext $context, ResolveInfo $info ) {
return ( ! empty( $avatar['force_default'] ) && true === $avatar['force_default'] ) ? true : false;
},
],
'rating' => [
'rating' => [
'type' => Types::string(),
'description' => esc_html__( "What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are judged in that order.", 'wp-graphql' ),
],
'scheme' => [
'scheme' => [
'type' => Types::string(),
'description' => esc_html__( 'Type of url scheme to use. Typically HTTP vs. HTTPS.', 'wp-graphql' ),
],
'extraAttr' => [
'type' => Types::string(),
'description' => esc_html__( 'HTML attributes to insert in the IMG element. Is not sanitized.', 'wp-graphql' ),
'resolve' => function( $avatar, array $args, AppContext $context, ResolveInfo $info ) {
'resolve' => function( $avatar, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $avatar['extra_attr'] ) ? $avatar['extra_attr'] : null;
},
],
'foundAvatar' => [
'type' => Types::boolean(),
'description' => esc_html__( 'Whether the avatar was successfully found.', 'wp-graphql' ),
'resolve' => function( $avatar, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $avatar['found_avatar'] ) ? $avatar['found_avatar'] : null;
return ! empty( $avatar['found_avatar'] && true === $avatar['found_avatar'] ) ? true : false;
},
],
'url' => [
'url' => [
'type' => Types::string(),
'description' => esc_html__( 'URL for the gravatar image source.', 'wp-graphql' ),
],
Expand All @@ -123,7 +128,7 @@ private static function fields() {
return self::prepare_fields( $fields, self::$type_name );

};
}
endif;

return self::$fields;

Expand Down
2 changes: 1 addition & 1 deletion src/Type/Comment/CommentQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ public static function root_query() {

}

}
}
8 changes: 2 additions & 6 deletions src/Type/Comment/CommentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Connections;
use WPGraphQL\Type\Comment\Connection\CommentConnectionDefinition;
use WPGraphQL\Type\WPObjectType;
use WPGraphQL\Types;
Expand Down Expand Up @@ -54,10 +53,8 @@ public function __construct() {
*/
private static function fields() {

if ( null === self::$fields ) {

if ( null === self::$fields ) :
self::$fields = function() {

$fields = [
'id' => [
'type' => Types::non_null( Types::id() ),
Expand Down Expand Up @@ -168,8 +165,7 @@ private static function fields() {
*/
return self::prepare_fields( $fields, self::$type_name );
};
}

endif;
return self::$fields;
}

Expand Down
Loading

0 comments on commit 6cb33f7

Please sign in to comment.