enableValidation() public static method

Useful only at development to catch type definition errors quickly.
public static enableValidation ( $allowCustomOptions = true )
Beispiel #1
0
// Test this using following command
// php -S localhost:8080 ./index.php
require_once '../../vendor/autoload.php';
use GraphQL\Examples\Blog\Types;
use GraphQL\Examples\Blog\AppContext;
use GraphQL\Examples\Blog\Data\DataSource;
use GraphQL\Schema;
use GraphQL\GraphQL;
use GraphQL\Type\Definition\Config;
use GraphQL\Error\FormattedError;
// Disable default PHP error reporting - we have better one for debug mode (see bellow)
ini_set('display_errors', 0);
if (!empty($_GET['debug'])) {
    // Enable additional validation of type configs
    // (disabled by default because it is costly)
    Config::enableValidation();
    // Catch custom errors (to report them in query results if debugging is enabled)
    $phpErrors = [];
    set_error_handler(function ($severity, $message, $file, $line) use(&$phpErrors) {
        $phpErrors[] = new ErrorException($message, 0, $severity, $file, $line);
    });
}
try {
    // Initialize our fake data source
    DataSource::init();
    // Prepare context that will be available in all field resolvers (as 3rd argument):
    $appContext = new AppContext();
    $appContext->viewer = DataSource::findUser('1');
    // simulated "currently logged-in user"
    $appContext->rootUrl = 'http://localhost:8080';
    $appContext->request = $_REQUEST;
Beispiel #2
0
 public function testProhibitsPuttingNonObjectTypesInUnions()
 {
     $int = Type::int();
     $badUnionTypes = [$int, new NonNull($int), new ListOfType($int), $this->interfaceType, $this->unionType, $this->enumType, $this->inputObjectType];
     Config::enableValidation();
     foreach ($badUnionTypes as $type) {
         try {
             new UnionType(['name' => 'BadUnion', 'types' => [$type]]);
             $this->fail('Expected exception not thrown');
         } catch (\Exception $e) {
             $this->assertSame('Expecting callable or instance of GraphQL\\Type\\Definition\\ObjectType at "types:0", but got "' . get_class($type) . '"', $e->getMessage());
         }
     }
     Config::disableValidation();
 }
Beispiel #3
0
 /**
  * @it prohibits putting non-Object types in unions
  */
 public function testProhibitsPuttingNonObjectTypesInUnions()
 {
     $int = Type::int();
     $badUnionTypes = [$int, new NonNull($int), new ListOfType($int), $this->interfaceType, $this->unionType, $this->enumType, $this->inputObjectType];
     // TODO: extract config validation to separate test
     Config::enableValidation();
     foreach ($badUnionTypes as $type) {
         try {
             new UnionType(['name' => 'BadUnion', 'types' => [$type]]);
             $this->fail('Expected exception not thrown');
         } catch (\Exception $e) {
             $this->assertSame('Error in "BadUnion" type definition: expecting callable or ObjectType definition at "types:0", but got "' . Utils::getVariableType($type) . '"', $e->getMessage());
         }
     }
     Config::disableValidation();
 }