File tree Expand file tree Collapse file tree 4 files changed +57
-3
lines changed Expand file tree Collapse file tree 4 files changed +57
-3
lines changed Original file line number Diff line number Diff line change @@ -5,13 +5,14 @@ between each release.
5
5
6
6
## Tech Debt
7
7
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
9
10
10
11
## 1.3: 2019-08-03
11
12
12
13
### Added
13
14
14
- - Support for inline fragments in the package
15
+ - Support for inline fragments
15
16
16
17
## 1.2: 2019-07-24
17
18
Original file line number Diff line number Diff line change @@ -178,6 +178,30 @@ false by default
178
178
variable. The default value will only be considered
179
179
if the isRequired argument is set to false.
180
180
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
+
181
205
# The Query Builder
182
206
183
207
The QueryBuilder class can be used to construct Query objects dynamically, which
Original file line number Diff line number Diff line change 3
3
namespace GraphQL \QueryBuilder ;
4
4
5
5
use GraphQL \Exception \EmptySelectionSetException ;
6
+ use GraphQL \InlineFragment ;
6
7
use GraphQL \Query ;
7
8
use GraphQL \RawObject ;
8
9
use GraphQL \Variable ;
@@ -77,7 +78,12 @@ public function getQuery(): Query
77
78
*/
78
79
protected function selectField ($ selectedField )
79
80
{
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
+ ) {
81
87
$ this ->selectionSet [] = $ selectedField ;
82
88
}
83
89
Original file line number Diff line number Diff line change 3
3
namespace GraphQL \Tests ;
4
4
5
5
use GraphQL \Exception \EmptySelectionSetException ;
6
+ use GraphQL \InlineFragment ;
6
7
use GraphQL \Query ;
7
8
use GraphQL \QueryBuilder \QueryBuilder ;
8
9
use GraphQL \RawObject ;
@@ -171,6 +172,28 @@ public function testSelectNestedQueryBuilder()
171
172
);
172
173
}
173
174
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
+
174
197
/**
175
198
* @covers \GraphQL\QueryBuilder\QueryBuilder::getQuery
176
199
* @covers \GraphQL\QueryBuilder\QueryBuilder::setArgument
You can’t perform that action at this time.
0 commit comments