Skip to content

Commit 707f2f4

Browse files
mghoneimy#17 Adds the ability to use an alias on object queries. (mghoneimy#41)
Adds the ability to use an alias on object queries. * Adds alias to constructor and et method * Adds alias to QueryBuilder * Adds query builder setter method * Update README.md * Update Changelog.md * Updates query builder test
1 parent 1fd4df8 commit 707f2f4

File tree

6 files changed

+201
-4
lines changed

6 files changed

+201
-4
lines changed

Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ between each release.
88
- Refactor the query conversion to string to separate the process of
99
constructing a new query and adding a nested subfield
1010

11+
## Unreleased
12+
13+
### Changed
14+
15+
- Updated Query class to allow for an alias
16+
- Updated QueryBuilder class to allow for an alias
17+
1118
## 1.7:
1219

1320
### Added

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ The full form shouldn't be used unless the query can't be represented in the
8989
shorthand form, which has only one case, when we want to run multiple queries
9090
in the same object.
9191

92+
9293
## Multiple Queries
9394
```php
9495
$gql = (new Query())
@@ -237,6 +238,46 @@ false by default
237238
variable. The default value will only be considered
238239
if the isRequired argument is set to false.
239240

241+
## Using an alias
242+
```php
243+
$gql = (new Query())
244+
->setSelectionSet(
245+
[
246+
(new Query('companies', 'TechCo'))
247+
->setArguments(['name' => 'Tech Co.'])
248+
->setSelectionSet(
249+
[
250+
'name',
251+
'serialNumber'
252+
]
253+
),
254+
(new Query('companies', 'AnotherTechCo'))
255+
->setArguments(['name' => 'A.N. Other Tech Co.'])
256+
->setSelectionSet(
257+
[
258+
'name',
259+
'serialNumber'
260+
]
261+
)
262+
]
263+
);
264+
```
265+
266+
An alias can be set in the second argument of the Query constructor for occasions when the same object needs to be retrieved multiple times with different arguments.
267+
268+
```php
269+
$gql = (new Query('companies'))
270+
->setAlias('CompanyAlias')
271+
->setSelectionSet(
272+
[
273+
'name',
274+
'serialNumber'
275+
]
276+
);
277+
```
278+
279+
The alias can also be set via the setter method.
280+
240281
## Using Interfaces: Query With Inline Fragments
241282

242283
When querying a field that returns an interface type, you might need to use
@@ -279,6 +320,27 @@ $builder = (new QueryBuilder('companies'))
279320
$gql = $builder->getQuery();
280321
```
281322

323+
As with the Query class, an alias can be set using the second constructor argument.
324+
325+
```php
326+
$builder = (new QueryBuilder('companies', 'CompanyAlias'))
327+
->selectField('name')
328+
->selectField('serialNumber');
329+
330+
$gql = $builder->getQuery();
331+
```
332+
333+
Or via the setter method
334+
335+
```php
336+
$builder = (new QueryBuilder('companies'))
337+
->setAlias('CompanyAlias')
338+
->selectField('name')
339+
->selectField('serialNumber');
340+
341+
$gql = $builder->getQuery();
342+
```
343+
282344
### The Full Form
283345

284346
Just like the Query class, the QueryBuilder class can be written in full form to

src/Query.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class Query extends NestableObject
4747
*/
4848
protected $fieldName;
4949

50+
/**
51+
* Stores the object alias
52+
*
53+
* @var string
54+
*/
55+
protected $alias;
56+
5057
/**
5158
* Stores the list of variables to be used in the query
5259
*
@@ -72,17 +79,31 @@ class Query extends NestableObject
7279
* GQLQueryBuilder constructor.
7380
*
7481
* @param string $fieldName if no value is provided for the field name an empty query object is assumed
82+
* @param string $alias the alias to use for the query if required
7583
*/
76-
public function __construct(string $fieldName = '')
84+
public function __construct(string $fieldName = '', string $alias = '')
7785
{
7886
$this->fieldName = $fieldName;
87+
$this->alias = $alias;
7988
$this->operationName = '';
8089
$this->variables = [];
8190
$this->arguments = [];
8291
$this->selectionSet = [];
8392
$this->isNested = false;
8493
}
8594

95+
/**
96+
* @param string $alias
97+
*
98+
* @return Query
99+
*/
100+
public function setAlias(string $alias)
101+
{
102+
$this->alias = $alias;
103+
104+
return $this;
105+
}
106+
86107
/**
87108
* @param string $operationName
88109
*
@@ -227,7 +248,15 @@ public function __toString()
227248
}
228249
$argumentsString = $this->constructArguments();
229250

230-
return sprintf($queryFormat, $this->fieldName, $argumentsString, $selectionSetString);
251+
return sprintf($queryFormat, $this->generateFieldName(), $argumentsString, $selectionSetString);
252+
}
253+
254+
/**
255+
* @return string
256+
*/
257+
protected function generateFieldName(): string
258+
{
259+
return empty($this->alias) ? $this->fieldName : sprintf('%s: %s', $this->alias, $this->fieldName);
231260
}
232261

233262
/**

src/QueryBuilder/AbstractQueryBuilder.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,28 @@ abstract class AbstractQueryBuilder implements QueryBuilderInterface
3939
* QueryBuilder constructor.
4040
*
4141
* @param string $queryObject
42+
* @param string $alias
4243
*/
43-
public function __construct(string $queryObject = '')
44+
public function __construct(string $queryObject = '', string $alias = '')
4445
{
45-
$this->query = new Query($queryObject);
46+
$this->query = new Query($queryObject, $alias);
4647
$this->variables = [];
4748
$this->selectionSet = [];
4849
$this->argumentsList = [];
4950
}
5051

52+
/**
53+
* @param string $alias
54+
*
55+
* @return $this
56+
*/
57+
public function setAlias(string $alias)
58+
{
59+
$this->query->setAlias($alias);
60+
61+
return $this;
62+
}
63+
5164
/**
5265
* @return Query
5366
*/

tests/QueryBuilderTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,44 @@ public function testConstruct()
5050
);
5151
}
5252

53+
/**
54+
* @covers \GraphQL\QueryBuilder\QueryBuilder::__construct
55+
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::__construct
56+
*/
57+
public function testConstructWithAlias()
58+
{
59+
$builder = new QueryBuilder('Object', 'ObjectAlias');
60+
$builder->selectField('field_one');
61+
$this->assertEquals(
62+
'query {
63+
ObjectAlias: Object {
64+
field_one
65+
}
66+
}',
67+
(string) $builder->getQuery()
68+
);
69+
}
70+
71+
/**
72+
* @covers \GraphQL\QueryBuilder\QueryBuilder::__construct
73+
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::__construct
74+
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::setAlias
75+
*/
76+
public function testSetAlias()
77+
{
78+
$builder = (new QueryBuilder('Object'))
79+
->setAlias('ObjectAlias');;
80+
$builder->selectField('field_one');
81+
$this->assertEquals(
82+
'query {
83+
ObjectAlias: Object {
84+
field_one
85+
}
86+
}',
87+
(string) $builder->getQuery()
88+
);
89+
}
90+
5391
/**
5492
* @covers \GraphQL\QueryBuilder\QueryBuilder::getQuery
5593
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::getQuery

tests/QueryTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,54 @@ public function testQueryWithoutFieldName()
8080
);
8181
}
8282

83+
/**
84+
* @depends testConvertsToString
85+
*
86+
* @covers \GraphQL\Query::generateSignature
87+
* @covers \GraphQL\Query::__toString
88+
*/
89+
public function testQueryWithAlias()
90+
{
91+
$query = (new Query('Object', 'ObjectAlias'))
92+
->setSelectionSet([
93+
'one'
94+
]);
95+
96+
$this->assertEquals(
97+
"query {
98+
ObjectAlias: Object {
99+
one
100+
}
101+
}",
102+
(string) $query
103+
);
104+
}
105+
106+
/**
107+
* @depends testConvertsToString
108+
*
109+
* @covers \GraphQL\Query::setAlias
110+
* @covers \GraphQL\Query::generateSignature
111+
* @covers \GraphQL\Query::__toString
112+
*/
113+
public function testQueryWithSetAlias()
114+
{
115+
$query = (new Query('Object'))
116+
->setAlias('ObjectAlias')
117+
->setSelectionSet([
118+
'one'
119+
]);
120+
121+
$this->assertEquals(
122+
"query {
123+
ObjectAlias: Object {
124+
one
125+
}
126+
}",
127+
(string) $query
128+
);
129+
}
130+
83131
/**
84132
* @depends testConvertsToString
85133
*

0 commit comments

Comments
 (0)