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.
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
Original file line number Diff line number Diff line change @@ -178,6 +178,30 @@ false by default
178178variable. The default value will only be considered
179179if 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
183207The QueryBuilder class can be used to construct Query objects dynamically, which
Original file line number Diff line number Diff line change 33namespace GraphQL \QueryBuilder ;
44
55use GraphQL \Exception \EmptySelectionSetException ;
6+ use GraphQL \InlineFragment ;
67use GraphQL \Query ;
78use GraphQL \RawObject ;
89use 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
Original file line number Diff line number Diff line change 33namespace GraphQL \Tests ;
44
55use GraphQL \Exception \EmptySelectionSetException ;
6+ use GraphQL \InlineFragment ;
67use GraphQL \Query ;
78use GraphQL \QueryBuilder \QueryBuilder ;
89use 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
You can’t perform that action at this time.
0 commit comments