2017-07-14 15:08:47 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Tests\Server;
|
|
|
|
|
2017-08-17 12:42:28 +03:00
|
|
|
use GraphQL\Error\Debug;
|
2017-07-14 15:08:47 +03:00
|
|
|
use GraphQL\Error\Error;
|
2017-07-19 15:30:39 +03:00
|
|
|
use GraphQL\Error\InvariantViolation;
|
2017-07-14 15:08:47 +03:00
|
|
|
use GraphQL\Executor\ExecutionResult;
|
2017-08-15 16:45:23 +03:00
|
|
|
use GraphQL\Language\AST\DocumentNode;
|
2017-07-14 15:08:47 +03:00
|
|
|
use GraphQL\Language\Parser;
|
|
|
|
use GraphQL\Server\Helper;
|
|
|
|
use GraphQL\Server\OperationParams;
|
2017-08-15 14:05:09 +03:00
|
|
|
use GraphQL\Server\RequestError;
|
2017-07-14 15:08:47 +03:00
|
|
|
use GraphQL\Server\ServerConfig;
|
|
|
|
use GraphQL\Validator\DocumentValidator;
|
2017-08-18 16:48:27 +03:00
|
|
|
use GraphQL\Validator\Rules\CustomValidationRule;
|
2017-07-14 15:08:47 +03:00
|
|
|
use GraphQL\Validator\ValidationContext;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class QueryExecutionTest extends ServerTestCase
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ServerConfig
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2017-08-20 21:16:55 +03:00
|
|
|
$schema = $this->buildSchema();
|
2017-08-15 14:05:09 +03:00
|
|
|
$this->config = ServerConfig::create()
|
|
|
|
->setSchema($schema);
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testSimpleQueryExecution() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$query = '{f1}';
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'f1' => 'f1'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertQueryResultEquals($expected, $query);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testReturnsSyntaxErrors() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-07-19 15:30:39 +03:00
|
|
|
$query = '{f1';
|
|
|
|
|
|
|
|
$result = $this->executeQuery($query);
|
|
|
|
$this->assertSame(null, $result->data);
|
|
|
|
$this->assertCount(1, $result->errors);
|
|
|
|
$this->assertContains(
|
2018-02-12 14:23:39 +03:00
|
|
|
'Syntax Error: Expected Name, found <EOF>',
|
2017-07-19 15:30:39 +03:00
|
|
|
$result->errors[0]->getMessage()
|
|
|
|
);
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testDebugExceptions() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-17 12:42:28 +03:00
|
|
|
$debug = Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE;
|
|
|
|
$this->config->setDebug($debug);
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
$query = '
|
|
|
|
{
|
2018-09-03 22:18:04 +03:00
|
|
|
fieldWithSafeException
|
2017-07-14 15:08:47 +03:00
|
|
|
f1
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
2018-09-03 22:18:04 +03:00
|
|
|
'fieldWithSafeException' => null,
|
2017-07-14 15:08:47 +03:00
|
|
|
'f1' => 'f1'
|
|
|
|
],
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' => 'This is the exception we want',
|
2018-09-03 22:18:04 +03:00
|
|
|
'path' => ['fieldWithSafeException'],
|
2017-07-14 15:08:47 +03:00
|
|
|
'trace' => []
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = $this->executeQuery($query)->toArray();
|
|
|
|
$this->assertArraySubset($expected, $result);
|
|
|
|
}
|
2018-09-03 22:18:04 +03:00
|
|
|
|
|
|
|
public function testRethrowUnsafeExceptions() : void
|
|
|
|
{
|
|
|
|
$this->config->setDebug(Debug::RETHROW_UNSAFE_EXCEPTIONS);
|
|
|
|
$this->expectException(UnsafeException::class);
|
|
|
|
|
|
|
|
$this->executeQuery('
|
|
|
|
{
|
|
|
|
fieldWithUnsafeException
|
|
|
|
}
|
|
|
|
')->toArray();
|
|
|
|
}
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testPassesRootValueAndContext() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$rootValue = 'myRootValue';
|
|
|
|
$context = new \stdClass();
|
|
|
|
|
|
|
|
$this->config
|
|
|
|
->setContext($context)
|
|
|
|
->setRootValue($rootValue);
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
{
|
|
|
|
testContextAndRootValue
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
$this->assertTrue(!isset($context->testedRootValue));
|
|
|
|
$this->executeQuery($query);
|
|
|
|
$this->assertSame($rootValue, $context->testedRootValue);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testPassesVariables() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$variables = ['a' => 'a', 'b' => 'b'];
|
|
|
|
$query = '
|
|
|
|
query ($a: String!, $b: String!) {
|
|
|
|
a: fieldWithArg(arg: $a)
|
|
|
|
b: fieldWithArg(arg: $b)
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'a' => 'a',
|
|
|
|
'b' => 'b'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertQueryResultEquals($expected, $query, $variables);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testPassesCustomValidationRules() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
{nonExistentField}
|
|
|
|
';
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
['message' => 'Cannot query field "nonExistentField" on type "Query".']
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertQueryResultEquals($expected, $query);
|
|
|
|
|
|
|
|
$called = false;
|
|
|
|
|
|
|
|
$rules = [
|
2017-08-18 16:48:27 +03:00
|
|
|
new CustomValidationRule('SomeRule', function() use (&$called) {
|
2017-07-14 15:08:47 +03:00
|
|
|
$called = true;
|
|
|
|
return [];
|
2017-08-18 16:48:27 +03:00
|
|
|
})
|
2017-07-14 15:08:47 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->config->setValidationRules($rules);
|
|
|
|
$expected = [
|
|
|
|
'data' => []
|
|
|
|
];
|
|
|
|
$this->assertQueryResultEquals($expected, $query);
|
|
|
|
$this->assertTrue($called);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsValidationRulesAsClosure() : void
|
2017-08-15 16:45:23 +03:00
|
|
|
{
|
|
|
|
$called = false;
|
|
|
|
$params = $doc = $operationType = null;
|
|
|
|
|
|
|
|
$this->config->setValidationRules(function($p, $d, $o) use (&$called, &$params, &$doc, &$operationType) {
|
|
|
|
$called = true;
|
|
|
|
$params = $p;
|
|
|
|
$doc = $d;
|
|
|
|
$operationType = $o;
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertFalse($called);
|
|
|
|
$this->executeQuery('{f1}');
|
|
|
|
$this->assertTrue($called);
|
|
|
|
$this->assertInstanceOf(OperationParams::class, $params);
|
|
|
|
$this->assertInstanceOf(DocumentNode::class, $doc);
|
|
|
|
$this->assertEquals('query', $operationType);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsDifferentValidationRulesDependingOnOperation() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$q1 = '{f1}';
|
|
|
|
$q2 = '{invalid}';
|
|
|
|
$called1 = false;
|
|
|
|
$called2 = false;
|
|
|
|
|
|
|
|
$this->config->setValidationRules(function(OperationParams $params) use ($q1, $q2, &$called1, &$called2) {
|
|
|
|
if ($params->query === $q1) {
|
|
|
|
$called1 = true;
|
|
|
|
return DocumentValidator::allRules();
|
|
|
|
} else {
|
|
|
|
$called2 = true;
|
|
|
|
return [
|
2017-08-18 16:48:27 +03:00
|
|
|
new CustomValidationRule('MyRule', function(ValidationContext $context) {
|
2017-07-14 15:08:47 +03:00
|
|
|
$context->reportError(new Error("This is the error we are looking for!"));
|
2017-08-18 16:48:27 +03:00
|
|
|
})
|
2017-07-14 15:08:47 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$expected = ['data' => ['f1' => 'f1']];
|
|
|
|
$this->assertQueryResultEquals($expected, $q1);
|
|
|
|
$this->assertTrue($called1);
|
|
|
|
$this->assertFalse($called2);
|
|
|
|
|
|
|
|
$called1 = false;
|
|
|
|
$called2 = false;
|
|
|
|
$expected = ['errors' => [['message' => 'This is the error we are looking for!']]];
|
|
|
|
$this->assertQueryResultEquals($expected, $q2);
|
|
|
|
$this->assertFalse($called1);
|
|
|
|
$this->assertTrue($called2);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSkippingValidation() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$this->config->setValidationRules([]);
|
|
|
|
$query = '{nonExistentField}';
|
|
|
|
$expected = ['data' => []];
|
|
|
|
$this->assertQueryResultEquals($expected, $query);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testPersistedQueriesAreDisabledByDefault() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-07-19 15:30:39 +03:00
|
|
|
$result = $this->executePersistedQuery('some-id');
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
2017-08-07 22:00:17 +03:00
|
|
|
'message' => 'Persisted queries are not supported by this server',
|
|
|
|
'category' => 'request'
|
2017-07-19 15:30:39 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testBatchedQueriesAreDisabledByDefault() : void
|
2017-08-15 14:05:09 +03:00
|
|
|
{
|
|
|
|
$batch = [
|
|
|
|
[
|
|
|
|
'query' => '{invalid}'
|
|
|
|
],
|
|
|
|
[
|
2018-09-03 22:18:04 +03:00
|
|
|
'query' => '{f1,fieldWithSafeException}'
|
2017-08-15 14:05:09 +03:00
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = $this->executeBatchedQuery($batch);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' => 'Batched queries are not supported by this server',
|
|
|
|
'category' => 'request'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' => 'Batched queries are not supported by this server',
|
|
|
|
'category' => 'request'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected[0], $result[0]->toArray());
|
|
|
|
$this->assertEquals($expected[1], $result[1]->toArray());
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMutationsAreNotAllowedInReadonlyMode() : void
|
2017-07-19 15:30:39 +03:00
|
|
|
{
|
|
|
|
$mutation = 'mutation { a }';
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
2017-08-07 22:00:17 +03:00
|
|
|
'message' => 'GET supports only query operation',
|
|
|
|
'category' => 'request'
|
2017-07-19 15:30:39 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = $this->executeQuery($mutation, null, true);
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsPersistentQueries() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$called = false;
|
|
|
|
$this->config->setPersistentQueryLoader(function($queryId, OperationParams $params) use (&$called) {
|
|
|
|
$called = true;
|
|
|
|
$this->assertEquals('some-id', $queryId);
|
|
|
|
return '{f1}';
|
|
|
|
});
|
|
|
|
|
|
|
|
$result = $this->executePersistedQuery('some-id');
|
|
|
|
$this->assertTrue($called);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'f1' => 'f1'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
|
|
|
|
|
|
|
// Make sure it allows returning document node:
|
|
|
|
$called = false;
|
|
|
|
$this->config->setPersistentQueryLoader(function($queryId, OperationParams $params) use (&$called) {
|
|
|
|
$called = true;
|
|
|
|
$this->assertEquals('some-id', $queryId);
|
|
|
|
return Parser::parse('{f1}');
|
|
|
|
});
|
|
|
|
$result = $this->executePersistedQuery('some-id');
|
|
|
|
$this->assertTrue($called);
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testProhibitsInvalidPersistedQueryLoader() : void
|
2017-07-19 15:30:39 +03:00
|
|
|
{
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectException(InvariantViolation::class);
|
|
|
|
$this->expectExceptionMessage(
|
|
|
|
'Persistent query loader must return query string or instance of GraphQL\Language\AST\DocumentNode ' .
|
2018-02-13 18:51:44 +03:00
|
|
|
'but got: {"err":"err"}'
|
2017-07-19 15:30:39 +03:00
|
|
|
);
|
|
|
|
$this->config->setPersistentQueryLoader(function($queryId, OperationParams $params) use (&$called) {
|
|
|
|
return ['err' => 'err'];
|
|
|
|
});
|
|
|
|
$this->executePersistedQuery('some-id');
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testPersistedQueriesAreStillValidatedByDefault() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$this->config->setPersistentQueryLoader(function() {
|
|
|
|
return '{invalid}';
|
|
|
|
});
|
|
|
|
$result = $this->executePersistedQuery('some-id');
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' => 'Cannot query field "invalid" on type "Query".',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [ ['line' => 1, 'column' => 2] ],
|
|
|
|
'category' => 'graphql'
|
2017-07-14 15:08:47 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowSkippingValidationForPersistedQueries() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$this->config
|
|
|
|
->setPersistentQueryLoader(function($queryId) {
|
|
|
|
if ($queryId === 'some-id') {
|
|
|
|
return '{invalid}';
|
|
|
|
} else {
|
|
|
|
return '{invalid2}';
|
|
|
|
}
|
|
|
|
})
|
|
|
|
->setValidationRules(function(OperationParams $params) {
|
|
|
|
if ($params->queryId === 'some-id') {
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
return DocumentValidator::allRules();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$result = $this->executePersistedQuery('some-id');
|
|
|
|
$expected = [
|
|
|
|
'data' => []
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
|
|
|
|
|
|
|
$result = $this->executePersistedQuery('some-other-id');
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' => 'Cannot query field "invalid2" on type "Query".',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [ ['line' => 1, 'column' => 2] ],
|
|
|
|
'category' => 'graphql'
|
2017-07-14 15:08:47 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testProhibitsUnexpectedValidationRules() : void
|
2017-07-19 15:30:39 +03:00
|
|
|
{
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectException(InvariantViolation::class);
|
|
|
|
$this->expectExceptionMessage('Expecting validation rules to be array or callable returning array, but got: instance of stdClass');
|
2017-07-19 15:30:39 +03:00
|
|
|
$this->config->setValidationRules(function(OperationParams $params) {
|
|
|
|
return new \stdClass();
|
|
|
|
});
|
|
|
|
$this->executeQuery('{f1}');
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testExecutesBatchedQueries() : void
|
2017-07-17 16:31:26 +03:00
|
|
|
{
|
2017-08-15 14:05:09 +03:00
|
|
|
$this->config->setQueryBatching(true);
|
|
|
|
|
2017-07-17 16:31:26 +03:00
|
|
|
$batch = [
|
|
|
|
[
|
|
|
|
'query' => '{invalid}'
|
|
|
|
],
|
|
|
|
[
|
2018-09-03 22:18:04 +03:00
|
|
|
'query' => '{f1,fieldWithSafeException}'
|
2017-07-17 16:31:26 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'query' => '
|
|
|
|
query ($a: String!, $b: String!) {
|
|
|
|
a: fieldWithArg(arg: $a)
|
|
|
|
b: fieldWithArg(arg: $b)
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'variables' => ['a' => 'a', 'b' => 'b'],
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = $this->executeBatchedQuery($batch);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'errors' => [['message' => 'Cannot query field "invalid" on type "Query".']]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'data' => [
|
|
|
|
'f1' => 'f1',
|
2018-09-03 22:18:04 +03:00
|
|
|
'fieldWithSafeException' => null
|
2017-07-17 16:31:26 +03:00
|
|
|
],
|
|
|
|
'errors' => [
|
|
|
|
['message' => 'This is the exception we want']
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'data' => [
|
|
|
|
'a' => 'a',
|
|
|
|
'b' => 'b'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertArraySubset($expected[0], $result[0]->toArray());
|
|
|
|
$this->assertArraySubset($expected[1], $result[1]->toArray());
|
|
|
|
$this->assertArraySubset($expected[2], $result[2]->toArray());
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testDeferredsAreSharedAmongAllBatchedQueries() : void
|
2017-07-17 16:31:26 +03:00
|
|
|
{
|
|
|
|
$batch = [
|
|
|
|
[
|
|
|
|
'query' => '{dfd(num: 1)}'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'query' => '{dfd(num: 2)}'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'query' => '{dfd(num: 3)}',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$calls = [];
|
|
|
|
|
|
|
|
$this->config
|
2017-08-15 14:05:09 +03:00
|
|
|
->setQueryBatching(true)
|
2017-07-17 16:31:26 +03:00
|
|
|
->setRootValue('1')
|
|
|
|
->setContext([
|
|
|
|
'buffer' => function($num) use (&$calls) {
|
|
|
|
$calls[] = "buffer: $num";
|
|
|
|
},
|
|
|
|
'load' => function($num) use (&$calls) {
|
|
|
|
$calls[] = "load: $num";
|
|
|
|
return "loaded: $num";
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
$result = $this->executeBatchedQuery($batch);
|
|
|
|
|
|
|
|
$expectedCalls = [
|
|
|
|
'buffer: 1',
|
|
|
|
'buffer: 2',
|
|
|
|
'buffer: 3',
|
|
|
|
'load: 1',
|
|
|
|
'load: 2',
|
|
|
|
'load: 3',
|
|
|
|
];
|
|
|
|
$this->assertEquals($expectedCalls, $calls);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'data' => [
|
|
|
|
'dfd' => 'loaded: 1'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'data' => [
|
|
|
|
'dfd' => 'loaded: 2'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'data' => [
|
|
|
|
'dfd' => 'loaded: 3'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected[0], $result[0]->toArray());
|
|
|
|
$this->assertEquals($expected[1], $result[1]->toArray());
|
|
|
|
$this->assertEquals($expected[2], $result[2]->toArray());
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testValidatesParamsBeforeExecution() : void
|
2017-08-15 14:05:09 +03:00
|
|
|
{
|
|
|
|
$op = OperationParams::create(['queryBad' => '{f1}']);
|
|
|
|
$helper = new Helper();
|
|
|
|
$result = $helper->executeOperation($this->config, $op);
|
|
|
|
$this->assertInstanceOf(ExecutionResult::class, $result);
|
|
|
|
|
|
|
|
$this->assertEquals(null, $result->data);
|
|
|
|
$this->assertCount(1, $result->errors);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'GraphQL Request must include at least one of those two parameters: "query" or "queryId"',
|
|
|
|
$result->errors[0]->getMessage()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
RequestError::class,
|
|
|
|
$result->errors[0]->getPrevious()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsContextAsClosure() : void
|
2017-08-15 16:45:23 +03:00
|
|
|
{
|
|
|
|
$called = false;
|
|
|
|
$params = $doc = $operationType = null;
|
|
|
|
|
|
|
|
$this->config->setContext(function($p, $d, $o) use (&$called, &$params, &$doc, &$operationType) {
|
|
|
|
$called = true;
|
|
|
|
$params = $p;
|
|
|
|
$doc = $d;
|
|
|
|
$operationType = $o;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertFalse($called);
|
|
|
|
$this->executeQuery('{f1}');
|
|
|
|
$this->assertTrue($called);
|
|
|
|
$this->assertInstanceOf(OperationParams::class, $params);
|
|
|
|
$this->assertInstanceOf(DocumentNode::class, $doc);
|
|
|
|
$this->assertEquals('query', $operationType);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsRootValueAsClosure() : void
|
2017-08-15 16:45:23 +03:00
|
|
|
{
|
|
|
|
$called = false;
|
|
|
|
$params = $doc = $operationType = null;
|
|
|
|
|
|
|
|
$this->config->setRootValue(function($p, $d, $o) use (&$called, &$params, &$doc, &$operationType) {
|
|
|
|
$called = true;
|
|
|
|
$params = $p;
|
|
|
|
$doc = $d;
|
|
|
|
$operationType = $o;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertFalse($called);
|
|
|
|
$this->executeQuery('{f1}');
|
|
|
|
$this->assertTrue($called);
|
|
|
|
$this->assertInstanceOf(OperationParams::class, $params);
|
|
|
|
$this->assertInstanceOf(DocumentNode::class, $doc);
|
|
|
|
$this->assertEquals('query', $operationType);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAppliesErrorFormatter() : void
|
2017-08-17 12:42:28 +03:00
|
|
|
{
|
|
|
|
$called = false;
|
|
|
|
$error = null;
|
|
|
|
$this->config->setErrorFormatter(function($e) use (&$called, &$error) {
|
|
|
|
$called = true;
|
|
|
|
$error = $e;
|
|
|
|
return ['test' => 'formatted'];
|
|
|
|
});
|
|
|
|
|
2018-09-03 22:18:04 +03:00
|
|
|
$result = $this->executeQuery('{fieldWithSafeException}');
|
2017-08-17 12:42:28 +03:00
|
|
|
$this->assertFalse($called);
|
|
|
|
$formatted = $result->toArray();
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
['test' => 'formatted']
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertTrue($called);
|
|
|
|
$this->assertArraySubset($expected, $formatted);
|
|
|
|
$this->assertInstanceOf(Error::class, $error);
|
|
|
|
|
|
|
|
// Assert debugging still works even with custom formatter
|
|
|
|
$formatted = $result->toArray(Debug::INCLUDE_TRACE);
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'test' => 'formatted',
|
|
|
|
'trace' => []
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertArraySubset($expected, $formatted);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAppliesErrorsHandler() : void
|
2017-08-17 12:42:28 +03:00
|
|
|
{
|
|
|
|
$called = false;
|
|
|
|
$errors = null;
|
|
|
|
$formatter = null;
|
|
|
|
$this->config->setErrorsHandler(function($e, $f) use (&$called, &$errors, &$formatter) {
|
|
|
|
$called = true;
|
|
|
|
$errors = $e;
|
|
|
|
$formatter = $f;
|
|
|
|
return [
|
|
|
|
['test' => 'handled']
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
2018-09-03 22:18:04 +03:00
|
|
|
$result = $this->executeQuery('{fieldWithSafeException,test: fieldWithSafeException}');
|
2017-08-17 12:42:28 +03:00
|
|
|
|
|
|
|
$this->assertFalse($called);
|
|
|
|
$formatted = $result->toArray();
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
['test' => 'handled']
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertTrue($called);
|
|
|
|
$this->assertArraySubset($expected, $formatted);
|
|
|
|
$this->assertInternalType('array', $errors);
|
|
|
|
$this->assertCount(2, $errors);
|
|
|
|
$this->assertInternalType('callable', $formatter);
|
|
|
|
$this->assertArraySubset($expected, $formatted);
|
|
|
|
}
|
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
private function executePersistedQuery($queryId, $variables = null)
|
|
|
|
{
|
|
|
|
$op = OperationParams::create(['queryId' => $queryId, 'variables' => $variables]);
|
2017-07-16 14:52:38 +03:00
|
|
|
$helper = new Helper();
|
|
|
|
$result = $helper->executeOperation($this->config, $op);
|
2017-07-14 15:08:47 +03:00
|
|
|
$this->assertInstanceOf(ExecutionResult::class, $result);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-07-19 15:30:39 +03:00
|
|
|
private function executeQuery($query, $variables = null, $readonly = false)
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-07-19 15:30:39 +03:00
|
|
|
$op = OperationParams::create(['query' => $query, 'variables' => $variables], $readonly);
|
2017-07-16 14:52:38 +03:00
|
|
|
$helper = new Helper();
|
|
|
|
$result = $helper->executeOperation($this->config, $op);
|
2017-07-14 15:08:47 +03:00
|
|
|
$this->assertInstanceOf(ExecutionResult::class, $result);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-07-17 16:31:26 +03:00
|
|
|
private function executeBatchedQuery(array $qs)
|
|
|
|
{
|
|
|
|
$batch = [];
|
|
|
|
foreach ($qs as $params) {
|
2017-07-21 18:11:20 +03:00
|
|
|
$batch[] = OperationParams::create($params);
|
2017-07-17 16:31:26 +03:00
|
|
|
}
|
|
|
|
$helper = new Helper();
|
|
|
|
$result = $helper->executeBatch($this->config, $batch);
|
|
|
|
$this->assertInternalType('array', $result);
|
|
|
|
$this->assertCount(count($qs), $result);
|
|
|
|
|
|
|
|
foreach ($result as $index => $entry) {
|
|
|
|
$this->assertInstanceOf(ExecutionResult::class, $entry, "Result at $index is not an instance of " . ExecutionResult::class);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
private function assertQueryResultEquals($expected, $query, $variables = null)
|
|
|
|
{
|
|
|
|
$result = $this->executeQuery($query, $variables);
|
2017-07-18 16:57:30 +03:00
|
|
|
$this->assertArraySubset($expected, $result->toArray(true));
|
2017-07-14 15:08:47 +03:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|