Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into features/wp-grap…
Browse files Browse the repository at this point in the history
…hql#156-optionsQueries
  • Loading branch information
hughdevore committed Oct 18, 2017
2 parents 81c0e81 + 789ae81 commit 849b995
Show file tree
Hide file tree
Showing 36 changed files with 1,258 additions and 239 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ vendor/ivome/graphql-relay-php/*
!vendor/composer
vendor/composer/installed.json
!/tests
build/
build/
coverage/*
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

# WPGraphQL

<a href="https://www.wpgraphql.com" target="_blank">Website</a> • <a href="https://wp-graphql.gitbooks.io/wp-graphql/content/" target="_blank">Docs</a> • <a href="https://wp-graphql.github.io/wp-graphql-api-docs/" target="_blank">ApiGen Code Docs</a>
<a href="https://www.wpgraphql.com" target="_blank">Website</a> • <a href="https://wp-graphql.gitbooks.io/wp-graphql/content/" target="_blank">Docs</a> • <a href="https://wp-graphql.github.io/wp-graphql-api-docs/" target="_blank">ApiGen Code Docs</a> • <a href="https://wpgql-slack.herokuapp.com/" target="_blank">Slack</a>

GraphQL API for WordPress.

[![Build Status](https://travis-ci.org/wp-graphql/wp-graphql.svg?branch=master)](https://travis-ci.org/wp-graphql/wp-graphql)
[![Coverage Status](https://coveralls.io/repos/github/wp-graphql/wp-graphql/badge.svg?branch=master)](https://coveralls.io/github/wp-graphql/wp-graphql?branch=master)
[![WPGraphQL on Slack](https://slackin-dsxwfpqeja.now.sh/badge.svg)](https://slackin-dsxwfpqeja.now.sh/)

------

## Quick Install
Download and install like any WordPress plugin.

## Doucmentation
## Documentation

Documentation can be found [on the Wiki](https://github.com/wp-graphql/wp-graphql/wiki) on this repository.

Expand Down Expand Up @@ -63,25 +62,52 @@ my recommendation is the _GraphiQL_ desktop app below:
## POSSIBLE BREAKING CHANGES
Please note that as the plugin continues to take shape, there might be breaking changes at any point. Once the plugin reaches a stable 1.0.0 release, breaking changes should be minimized and communicated appropriately if they are required.

## Unit Testing
To run unit tests during development, you'll first need a testing database that you'd like to use.
## Unit Testing and Code Coverage

Open the command line and navigate to the plugin's directory. From within the plugin directory, run the following
commands to install the test suite, filling in the parameters appropriately to link to an existing test database or to
create a new test database:
Before anything is merged into the WPGraphQL code base it must pass all tests and have 100% code coverage. Travis-CI and Coveralls will check this when you create a pull request to the WPGraphQL repo. However, before that happens, you should ensure all of these requirements are met locally. The following will help you set up both testing and code coverage in your local environment.

### Prerequisites
To run unit tests and code coverage during development you'll need the following:

* [Composer](https://getcomposer.org/doc/00-intro.md)
* [phpunit](https://phpunit.de/)
* `composer global require phpunit/phpunit`
* [php-coveralls](https://github.com/php-coveralls/php-coveralls)
* `composer global require php-coveralls/php-coveralls`
* [Xdebug](https://xdebug.org/docs/install)

`composer install`
### Test Database
Open the command line, then from the root of your WordPress install, navigate to the `wp-content/plugins/wp-graphql` directory. From within the WPGraphQL plugin's directory run the following commands to install the test suite, filling in the parameters appropriately to link to an existing test database or to
create a new test database:

`bin/install-wp-tests.sh <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]`

For example:

`bin/install-wp-tests.sh wp-tests-docs root password localhost latest`

NOTE: You'll want the test database to be a true test database, not a database with valuable, existing information, as
the tests will create new data and clear out data, and you don't want to cause issues with a database you're actually
using for projects.

DEBUGGING: If you have run this command before in another branch you may already have a local copy of WordPress downloaded in your `/private/tmp` directory. If this is the case, please remove it and then run the install script again. Without removing this you may receive an error when running phpunit.

### Running the Tests and Checking Coverage
Now that we have the prerequisites installed we can run the tests and check our code coverage. The results of our tests will appear on the command line, however our code coverage information is stored in the coverage directory in the form of HTML. This HTML is generated by phpunit when it runs the tests by passing the `--coverage-html` flag.

`phpunit --coverage-html ./coverage`

The easiest way to check your code coverage is to drag, or right click and open, the `index.html` file from the coverage directory (`wp-graphql/coverage/index.html`) in your browser. This should render a dashboard that will allow you to view the code coverage of all files in the WPGraphQL repo.

### Running Individual Files
As you'll note, running all of the tests in the entire test suite can be time consuming. If you would like to run only one test file instead of all of them, simply pass the test file you're trying to test, like so:

`phpunit --coverage-html ./coverage ./tests/test-media-item-mutations.php`

## Shout Outs
This plugin brings the power of GraphQL (http://graphql.org/) to WordPress.

This plugin is based on the hard work of Jason Bahl and Ryan Kanner of Digital First Media (https://github.com/dfmedia),
This plugin is based on the hard work of Jason Bahl, Ryan Kanner, Hughie Devore and Peter Pak of Digital First Media (https://github.com/dfmedia),
and Edwin Cromley of BE-Webdesign (https://github.com/BE-Webdesign).

The plugin is built on top of the graphql-php library by Webonyx (https://github.com/webonyx/graphql-php) and makes use
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "GraphQL API for WordPress",
"type": "wordpress-plugin",
"license": "GPLv3",
"version": "0.0.20",
"authors": [
{
"name": "Jason Bahl",
Expand All @@ -13,6 +14,9 @@
},
{
"name": "Ryan Kanner"
},
{
"name": "Hughie Devore"
}
],
"require": {
Expand Down
1 change: 1 addition & 0 deletions src/Data/ConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static function get_connection( $query, array $array, $source, array $arg

$meta = self::get_array_meta( $query, $args );
$connection = ArrayConnection::connectionFromArraySlice( $array, $args, $meta );
$connection['nodes'] = $array;

return $connection;

Expand Down
10 changes: 10 additions & 0 deletions src/Data/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ public static function resolve_post_object( $id, $post_type ) {
throw new \Exception( sprintf( __( 'No %1$s was found with the ID: %2$s', 'wp-graphql' ), $id, $post_type ) );
}

/**
* Set the resolved post to the global $post. That way any filters that
* might be applied when resolving fields can rely on global post and
* post data being set up.
*
* @since 0.0.18
*/
$GLOBALS['post'] = $post_object;
setup_postdata( $post_object );

return $post_object;

}
Expand Down
38 changes: 35 additions & 3 deletions src/Router.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace WPGraphQL;

use GraphQL\Error\FormattedError;
Expand All @@ -23,6 +24,7 @@ class Router {
/**
* Set the default status code to 403 to represent unauthenticated requests. Authenticated requests
* should set the status code to 200.
*
* @var int
*/
public static $http_status_code = 403;
Expand Down Expand Up @@ -267,10 +269,26 @@ public static function process_http_request() {
*/
$response = [];
$graphql_results = [];
$request = '';
$operation_name = '';
$variables = [];
$user = null;

try {

if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'GET' ) {
/**
* Respond to pre-flight requests.
*
* @see: https://apollographql.slack.com/archives/C10HTKHPC/p1507649812000123
* @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests
*/
if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) {

self::$http_status_code = 200;
self::set_headers( self::$http_status_code );
exit;

} else if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'GET' ) {

$data = [
'query' => isset( $_GET['query'] ) ? sanitize_text_field( $_GET['query'] ) : '',
Expand Down Expand Up @@ -310,6 +328,7 @@ public static function process_http_request() {
$data['variables'] = ! empty( $decoded_variables ) && is_array( $decoded_variables ) ? $decoded_variables : null;

} else {

if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || 'POST' !== $_SERVER['REQUEST_METHOD'] ) {
$response['errors'] = __( 'WPGraphQL requires POST requests', 'wp-graphql' );
}
Expand Down Expand Up @@ -410,16 +429,29 @@ public static function process_http_request() {
*/
if ( false === headers_sent() ) {

/**
* Filter the $status_code before setting the headers
*
* @param int $status_code The status code to apply to the headers
* @param array $response The response of the GraphQL Request
* @param array $graphql_results The results of the GraphQL execution
* @param string $request The GraphQL Request
* @param string $operation_name The operation name of the GraphQL Request
* @param array $variables The variables applied to the GraphQL Request
* @param \WP_User $user The current user object
*/
$status_code = apply_filters( 'graphql_response_status_code', self::$http_status_code, $response, $graphql_results, $request, $operation_name, $variables, $user );

/**
* Set the response headers
*/
self::set_headers( self::$http_status_code );
self::set_headers( $status_code );

/**
* Send the JSON response
*/
wp_send_json( $response );
} elseif (defined( 'DOING_AJAX' ) && DOING_AJAX) {
} elseif ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
/**
* Headers will already be set if this function is called within AJAX.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Type/Comment/Connection/CommentConnectionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public static function connection() {
$connection = Relay::connectionDefinitions( [
'nodeType' => Types::comment(),
'name' => 'comments',
'connectionFields' => function() {
return [
'nodes' => [
'type' => Types::list_of( Types::comment() ),
'description' => __( 'The nodes of the connection, without the edges', 'wp-graphql' ),
'resolve' => function( $source, $args, $context, $info ) {
return ! empty( $source['nodes'] ) ? $source['nodes'] : [];
},
],
];
},
] );

/**
Expand Down
1 change: 1 addition & 0 deletions src/Type/Comment/Connection/CommentConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public static function get_connection( $query, array $items, $source, array $arg
'startCursor' => ! empty( $first_edge['cursor'] ) ? $first_edge['cursor'] : null,
'endCursor' => ! empty( $last_edge['cursor'] ) ? $last_edge['cursor'] : null,
],
'nodes' => $items,
];

return $connection;
Expand Down
66 changes: 66 additions & 0 deletions src/Type/Enum/PostObjectFieldFormatEnumType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
namespace WPGraphQL\Type\Enum;

use WPGraphQL\Type\WPEnumType;

/**
* Class PostObjectFieldFormatEnumType
*
* This defines an EnumType with allowed formats of post field data.
*
* @package WPGraphQL\Type\Enum
* @since 0.0.18
*/
class PostObjectFieldFormatEnumType extends WPEnumType {

/**
* This holds the enum values array.
*
* @var array $values
*/
private static $values;

public function __construct() {
$config = [
'name' => 'postObjectFieldFormat',
'description' => __( 'The format of post field data.', 'wp-graphql' ),
'values' => self::values(),
];
parent::__construct( $config );
}

/**
* Creates a list of formats of post field data.
*
* @return array
*/
private static function values() {

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

/**
* Post object field formats.
*
* @since 0.0.18
*/
self::$values = [
'raw' => [
'name' => 'RAW',
'description' => __( 'Provide the field value directly from database', 'wp-graphql' ),
'value' => 'raw',
],
'rendered' => [
'name' => 'RENDERED',
'description' => __( 'Apply the default WordPress rendering', 'wp-graphql' ),
'value' => 'rendered',
],
];

}

/**
* Return the $values
*/
return self::$values;
}
}
Loading

0 comments on commit 849b995

Please sign in to comment.