Skip to content

Commit 497784e

Browse files
committed
Apply fix from mghoneimy#71
1 parent caadeba commit 497784e

File tree

3 files changed

+141
-14
lines changed

3 files changed

+141
-14
lines changed

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
![Note]
2+
This fork was only created because the original library is no longer maintained and to add support for responses that contain both data & errors. See https://github.com/mghoneimy/php-graphql-client/pull/70 https://github.com/mghoneimy/php-graphql-client/pull/71
3+
14
# PHP GraphQL Client
5+
26
![Build Status](https://github.com/mghoneimy/php-graphql-client/actions/workflows/php.yml/badge.svg)
37
[![Total
48
Downloads](https://poser.pugx.org/gmostafa/php-graphql-client/downloads)](https://packagist.org/packages/gmostafa/php-graphql-client)
@@ -29,14 +33,13 @@ There are 3 primary ways to use this package to generate your GraphQL queries:
2933
Run the following command to install the package using composer:
3034

3135
```
32-
$ composer require gmostafa/php-graphql-client
36+
$ composer require brandonbeeler/php-graphql-client
3337
```
3438

3539
# Object-to-Query-Mapper Extension
3640

3741
To avoid the hassle of having to write _any_ queries and just interact with PHP
38-
objects generated from your API schema visit [PHP GraphQL OQM repository](
39-
https://github.com/mghoneimy/php-graphql-oqm)
42+
objects generated from your API schema visit [PHP GraphQL OQM repository](https://github.com/mghoneimy/php-graphql-oqm)
4043

4144
# Query Examples
4245

@@ -84,8 +87,8 @@ The full form shouldn't be used unless the query can't be represented in the
8487
shorthand form, which has only one case, when we want to run multiple queries
8588
in the same object.
8689

87-
8890
## Multiple Queries
91+
8992
```php
9093
$gql = (new Query())
9194
->setSelectionSet(
@@ -116,6 +119,7 @@ Writing multiple queries requires writing the query object in the full form
116119
to represent each query as a subfield under the parent query object.
117120

118121
## Nested Queries
122+
119123
```php
120124
$gql = (new Query('companies'))
121125
->setSelectionSet(
@@ -190,7 +194,7 @@ $gql = (new Query('companies'))
190194
This query is another special case of the arguments query. In this example,
191195
we're setting a custom input object "filter" with some values to limit the
192196
companies being returned. We're setting the filter "name_starts_with" with
193-
value "Face". This query will retrieve only the companies whose names
197+
value "Face". This query will retrieve only the companies whose names
194198
start with the phrase "Face".
195199

196200
The RawObject class being constructed is used for injecting the string into the
@@ -228,12 +232,13 @@ standards. Its constructor receives 4 arguments:
228232
- name: Represents the variable name
229233
- type: Represents the variable type according to the GraphQL server schema
230234
- isRequired (Optional): Represents if the variable is required or not, it's
231-
false by default
235+
false by default
232236
- defaultValue (Optional): Represents the default value to be assigned to the
233-
variable. The default value will only be considered
234-
if the isRequired argument is set to false.
237+
variable. The default value will only be considered
238+
if the isRequired argument is set to false.
235239

236240
## Using an alias
241+
237242
```php
238243
$gql = (new Query())
239244
->setSelectionSet(
@@ -366,7 +371,7 @@ companies starting with a name prefix and returns the company with the
366371
# Constructing The Client
367372

368373
A Client object can easily be instantiated by providing the GraphQL endpoint
369-
URL.
374+
URL.
370375

371376
The Client constructor also receives an optional "authorizationHeaders"
372377
array, which can be used to add authorization headers to all requests being sent
@@ -381,7 +386,6 @@ $client = new Client(
381386
);
382387
```
383388

384-
385389
The Client constructor also receives an optional "httpOptions" array, which
386390
**overrides** the "authorizationHeaders" and can be used to add custom
387391
[Guzzle HTTP Client request options](https://guzzle.readthedocs.io/en/latest/request-options.html).
@@ -392,7 +396,7 @@ Example:
392396
$client = new Client(
393397
'http://api.graphql.com',
394398
[],
395-
[
399+
[
396400
'connect_timeout' => 5,
397401
'timeout' => 5,
398402
'headers' => [
@@ -410,7 +414,6 @@ $client = new Client(
410414
);
411415
```
412416

413-
414417
It is possible to use your own preconfigured HTTP client that implements the [PSR-18 interface](https://www.php-fig.org/psr/psr-18/).
415418

416419
Example:
@@ -435,6 +438,7 @@ structure:
435438
$results = $client->runQuery($gql);
436439
$results->getData()->companies[0]->branches;
437440
```
441+
438442
Or getting results in array structure:
439443

440444
```php

src/Exception/QueryError.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ class QueryError extends RuntimeException
1717
* @var array
1818
*/
1919
protected $errorDetails;
20+
/**
21+
* @var array
22+
*/
23+
protected $data;
24+
/**
25+
* @var array
26+
*/
27+
protected $errors;
2028

2129
/**
2230
* QueryError constructor.
@@ -26,6 +34,11 @@ class QueryError extends RuntimeException
2634
public function __construct($errorDetails)
2735
{
2836
$this->errorDetails = $errorDetails['errors'][0];
37+
$this->data = [];
38+
if (!empty($errorDetails['data'])) {
39+
$this->data = $errorDetails['data'];
40+
}
41+
$this->errors = $errorDetails['errors'];
2942
parent::__construct($this->errorDetails['message']);
3043
}
3144

@@ -36,4 +49,20 @@ public function getErrorDetails()
3649
{
3750
return $this->errorDetails;
3851
}
39-
}
52+
53+
/**
54+
* @return array
55+
*/
56+
public function getData()
57+
{
58+
return $this->data;
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public function getErrors()
65+
{
66+
return $this->errors;
67+
}
68+
}

tests/QueryErrorTest.php

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,99 @@ public function testConstructQueryError()
4747
],
4848
$queryError->getErrorDetails()
4949
);
50+
$this->assertEquals([], $queryError->getData());
5051
}
51-
}
52+
53+
/**
54+
* @covers \GraphQL\Exception\QueryError::__construct
55+
* @covers \GraphQL\Exception\QueryError::getErrorDetails
56+
*/
57+
public function testConstructQueryErrorWhenResponseHasData()
58+
{
59+
$errorData = [
60+
'errors' => [
61+
[
62+
'message' => 'first error message',
63+
'location' => [
64+
[
65+
'line' => 1,
66+
'column' => 3,
67+
]
68+
],
69+
],
70+
[
71+
'message' => 'second error message',
72+
'location' => [
73+
[
74+
'line' => 2,
75+
'column' => 4,
76+
]
77+
],
78+
],
79+
],
80+
'data' => [
81+
'someField' => [
82+
[
83+
'data' => 'value',
84+
],
85+
[
86+
'data' => 'value',
87+
]
88+
]
89+
]
90+
];
91+
92+
$queryError = new QueryError($errorData);
93+
$this->assertEquals('first error message', $queryError->getMessage());
94+
$this->assertEquals(
95+
[
96+
'message' => 'first error message',
97+
'location' => [
98+
[
99+
'line' => 1,
100+
'column' => 3,
101+
]
102+
]
103+
],
104+
$queryError->getErrorDetails()
105+
);
106+
107+
$this->assertEquals(
108+
[
109+
[
110+
'message' => 'first error message',
111+
'location' => [
112+
[
113+
'line' => 1,
114+
'column' => 3,
115+
]
116+
]
117+
],
118+
[
119+
'message' => 'second error message',
120+
'location' => [
121+
[
122+
'line' => 2,
123+
'column' => 4,
124+
]
125+
]
126+
]
127+
],
128+
$queryError->getErrors()
129+
);
130+
131+
$this->assertEquals(
132+
[
133+
'someField' => [
134+
[
135+
'data' => 'value',
136+
],
137+
[
138+
'data' => 'value',
139+
]
140+
]
141+
],
142+
$queryError->getData()
143+
);
144+
}
145+
}

0 commit comments

Comments
 (0)