1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Test; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\BrowserKit\Client; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Ynlo\GraphQLBundle\Model\ID; |
16
|
|
|
use Ynlo\GraphQLBundle\Model\NodeInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @method Client getClient() |
20
|
|
|
*/ |
21
|
|
|
trait GraphQLHelperTrait |
22
|
|
|
{ |
23
|
|
|
private static $endpoint; |
24
|
|
|
|
25
|
|
|
private static $query; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $endpoint |
29
|
|
|
*/ |
30
|
|
|
protected static function endpoint($endpoint) |
31
|
|
|
{ |
32
|
|
|
self::$endpoint = $endpoint; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $query |
37
|
|
|
* @param array $variables |
38
|
|
|
*/ |
39
|
21 |
|
protected static function send($query, array $variables = []) |
40
|
|
|
{ |
41
|
21 |
|
self::$query = ['query' => $query, 'variables' => $variables]; |
42
|
21 |
|
self::getClient()->request(Request::METHOD_POST, self::$endpoint, [], [], [], json_encode(self::$query)); |
|
|
|
|
43
|
21 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $nodeType |
47
|
|
|
* @param string|NodeInterface $databaseId |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
10 |
|
protected static function encodeID($nodeType, $databaseId) |
52
|
|
|
{ |
53
|
10 |
|
if ($databaseId instanceof NodeInterface) { |
54
|
7 |
|
$databaseId = $databaseId->getId(); |
55
|
|
|
} |
56
|
|
|
|
57
|
10 |
|
return ID::encode($nodeType, $databaseId); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $globalID |
62
|
|
|
* |
63
|
|
|
* @return ID |
64
|
|
|
*/ |
65
|
|
|
protected static function decodeID($globalID): ID |
66
|
|
|
{ |
67
|
|
|
return ID::createFromString($globalID); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Print helpful debug information |
72
|
|
|
*/ |
73
|
|
|
protected static function debugInfo() |
74
|
|
|
{ |
75
|
|
|
if (self::$query) { |
76
|
|
|
$query = self::$query['query'] ?? null; |
77
|
|
|
|
78
|
|
|
$type = 'QUERY'; |
79
|
|
|
if (preg_match('/^\s*mutation/', $query)) { |
80
|
|
|
$type = 'MUTATION'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$variables = self::$query['variables'] ?? null; |
84
|
|
|
|
85
|
|
|
print_r("\n\n-------------- GraphQL $type ----------------\n\n"); |
86
|
|
|
print_r($query ?? null); |
87
|
|
|
print_r("\n\n"); |
88
|
|
|
print_r("------------------- VARIABLES-----------------------\n\n"); |
89
|
|
|
print_r(json_encode($variables, JSON_PRETTY_PRINT)); |
90
|
|
|
print_r("\n\n"); |
91
|
|
|
print_r("-------------------- RESPONSE ----------------------\n\n"); |
92
|
|
|
$content = self::getClient()->getResponse()->getContent(); |
|
|
|
|
93
|
|
|
$json = @json_decode($content, true); |
94
|
|
|
if ($json) { |
95
|
|
|
print_r(json_encode($json, JSON_PRETTY_PRINT)); |
96
|
|
|
} else { |
97
|
|
|
print_r($content); |
98
|
|
|
} |
99
|
|
|
print_r("\n\n"); |
100
|
|
|
print_r("-----------------------------------------------------\n\n"); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|