Skip to content

Commit 1be16d4

Browse files
committed
GQL-48: Finalized feature
- Added examples in README.md - Added support for embedding inline fragments in query builder selection set
1 parent f4fd7c8 commit 1be16d4

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

Changelog.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ between each release.
55

66
## Tech Debt
77

8-
- Refactor the query conversion to string to separate the process of constructing a query and adding a field
8+
- Refactor the query conversion to string to separate the process of
9+
constructing a new query and adding a nested subfield
910

1011
## 1.3: 2019-08-03
1112

1213
### Added
1314

14-
- Support for inline fragments in the package
15+
- Support for inline fragments
1516

1617
## 1.2: 2019-07-24
1718

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,30 @@ false by default
178178
variable. The default value will only be considered
179179
if the isRequired argument is set to false.
180180

181+
## Using Interfaces: Query With Inline Fragments
182+
183+
When querying a field that returns an interface type, you might need to use
184+
inline fragments to access data on the underlying concrete type.
185+
186+
This example show how to generate inline fragments using this package:
187+
188+
```
189+
$gql = new Query('companies');
190+
$gql->setSelectionSet(
191+
[
192+
'serialNumber',
193+
'name',
194+
(new InlineFragment('PrivateCompany'))
195+
->setSelectionSet(
196+
[
197+
'boardMembers',
198+
'shareholders',
199+
]
200+
),
201+
]
202+
);
203+
```
204+
181205
# The Query Builder
182206

183207
The QueryBuilder class can be used to construct Query objects dynamically, which

src/QueryBuilder/AbstractQueryBuilder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace GraphQL\QueryBuilder;
44

55
use GraphQL\Exception\EmptySelectionSetException;
6+
use GraphQL\InlineFragment;
67
use GraphQL\Query;
78
use GraphQL\RawObject;
89
use GraphQL\Variable;
@@ -77,7 +78,12 @@ public function getQuery(): Query
7778
*/
7879
protected function selectField($selectedField)
7980
{
80-
if (is_string($selectedField) || $selectedField instanceof AbstractQueryBuilder || $selectedField instanceof Query) {
81+
if (
82+
is_string($selectedField)
83+
|| $selectedField instanceof AbstractQueryBuilder
84+
|| $selectedField instanceof Query
85+
|| $selectedField instanceof InlineFragment
86+
) {
8187
$this->selectionSet[] = $selectedField;
8288
}
8389

tests/QueryBuilderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace GraphQL\Tests;
44

55
use GraphQL\Exception\EmptySelectionSetException;
6+
use GraphQL\InlineFragment;
67
use GraphQL\Query;
78
use GraphQL\QueryBuilder\QueryBuilder;
89
use GraphQL\RawObject;
@@ -171,6 +172,28 @@ public function testSelectNestedQueryBuilder()
171172
);
172173
}
173174

175+
/**
176+
* @covers \GraphQL\QueryBuilder\QueryBuilder::getQuery
177+
* @covers \GraphQL\QueryBuilder\QueryBuilder::selectField
178+
*/
179+
public function testSelectInlineFragment()
180+
{
181+
$this->queryBuilder->selectField(
182+
(new InlineFragment('Type'))
183+
->setSelectionSet(['field'])
184+
);
185+
$this->assertEquals(
186+
'query {
187+
Object {
188+
... on Type {
189+
field
190+
}
191+
}
192+
}',
193+
(string) $this->queryBuilder->getQuery()
194+
);
195+
}
196+
174197
/**
175198
* @covers \GraphQL\QueryBuilder\QueryBuilder::getQuery
176199
* @covers \GraphQL\QueryBuilder\QueryBuilder::setArgument

0 commit comments

Comments
 (0)