21
21
22
22
class SelectFields
23
23
{
24
- /** @var array */
25
- private static $ args = [];
26
24
/** @var array */
27
25
private $ select = [];
28
26
/** @var array */
@@ -43,14 +41,20 @@ public function __construct(ResolveInfo $info, GraphqlType $parentType, array $a
43
41
$ parentType = $ parentType ->getWrappedType (true );
44
42
}
45
43
46
- if (! is_null ($ info ->fieldNodes [0 ]->selectionSet )) {
47
- self ::$ args = $ args ;
44
+ $ requestedFields = $ this ->getFieldSelection ($ info , $ args , 5 );
45
+ $ fields = self ::getSelectableFieldsAndRelations ($ requestedFields , $ parentType );
46
+ $ this ->select = $ fields [0 ];
47
+ $ this ->relations = $ fields [1 ];
48
+ }
48
49
49
- $ fields = self ::getSelectableFieldsAndRelations ($ info ->getFieldSelection (5 ), $ parentType );
50
+ private function getFieldSelection (ResolveInfo $ resolveInfo , array $ args , int $ depth ): array
51
+ {
52
+ $ resolveInfoFieldsAndArguments = new ResolveInfoFieldsAndArguments ($ resolveInfo );
50
53
51
- $ this ->select = $ fields [0 ];
52
- $ this ->relations = $ fields [1 ];
53
- }
54
+ return [
55
+ 'args ' => $ args ,
56
+ 'fields ' => $ resolveInfoFieldsAndArguments ->getFieldsAndArgumentsSelection ($ depth ),
57
+ ];
54
58
}
55
59
56
60
/**
@@ -95,9 +99,9 @@ public static function getSelectableFieldsAndRelations(array $requestedFields, G
95
99
if ($ topLevel ) {
96
100
return [$ select , $ with ];
97
101
} else {
98
- return function ($ query ) use ($ with , $ select , $ customQuery ) {
102
+ return function ($ query ) use ($ with , $ select , $ customQuery, $ requestedFields ) {
99
103
if ($ customQuery ) {
100
- $ query = $ customQuery (self :: $ args , $ query );
104
+ $ query = $ customQuery ($ requestedFields [ ' args ' ] , $ query );
101
105
}
102
106
103
107
$ query ->select ($ select );
@@ -118,7 +122,7 @@ protected static function handleFields(array $requestedFields, GraphqlType $pare
118
122
{
119
123
$ parentTable = self ::isMongodbInstance ($ parentType ) ? null : self ::getTableNameFromParentType ($ parentType );
120
124
121
- foreach ($ requestedFields as $ key => $ field ) {
125
+ foreach ($ requestedFields[ ' fields ' ] as $ key => $ field ) {
122
126
// Ignore __typename, as it's a special case
123
127
if ($ key === '__typename ' ) {
124
128
continue ;
@@ -142,7 +146,7 @@ protected static function handleFields(array $requestedFields, GraphqlType $pare
142
146
}
143
147
144
148
// First check if the field is even accessible
145
- $ canSelect = self ::validateField ($ fieldObject );
149
+ $ canSelect = self ::validateField ($ fieldObject, $ field [ ' args ' ] );
146
150
if ($ canSelect === true ) {
147
151
// Add a query, if it exists
148
152
$ customQuery = Arr::get ($ fieldObject ->config , 'query ' );
@@ -155,7 +159,7 @@ protected static function handleFields(array $requestedFields, GraphqlType $pare
155
159
self ::handleFields ($ field , $ fieldObject ->config ['type ' ]->getWrappedType (), $ select , $ with );
156
160
}
157
161
// With
158
- elseif (is_array ($ field ) && $ queryable ) {
162
+ elseif (is_array ($ field[ ' fields ' ] ) && $ queryable ) {
159
163
if (isset ($ parentType ->config ['model ' ])) {
160
164
// Get the next parent type, so that 'with' queries could be made
161
165
// Both keys for the relation are required (e.g 'id' <-> 'user_id')
@@ -193,7 +197,7 @@ protected static function handleFields(array $requestedFields, GraphqlType $pare
193
197
$ segments = explode ('. ' , $ foreignKey );
194
198
$ foreignKey = end ($ segments );
195
199
if (! array_key_exists ($ foreignKey , $ field )) {
196
- $ field [$ foreignKey ] = self ::FOREIGN_KEY ;
200
+ $ field [' fields ' ][ $ foreignKey ] = self ::FOREIGN_KEY ;
197
201
}
198
202
}
199
203
@@ -240,10 +244,11 @@ protected static function handleFields(array $requestedFields, GraphqlType $pare
240
244
* Check the privacy status, if it's given.
241
245
*
242
246
* @param FieldDefinition $fieldObject
247
+ * @param array $fieldArgs Arguments given with the field
243
248
* @return bool|null - true, if selectable; false, if not selectable, but allowed;
244
249
* null, if not allowed
245
250
*/
246
- protected static function validateField (FieldDefinition $ fieldObject ): ?bool
251
+ protected static function validateField (FieldDefinition $ fieldObject, array $ fieldArgs ): ?bool
247
252
{
248
253
$ selectable = true ;
249
254
@@ -256,15 +261,15 @@ protected static function validateField(FieldDefinition $fieldObject): ?bool
256
261
$ privacyClass = $ fieldObject ->config ['privacy ' ];
257
262
258
263
// If privacy given as a closure
259
- if (is_callable ($ privacyClass ) && call_user_func ($ privacyClass , self :: $ args ) === false ) {
264
+ if (is_callable ($ privacyClass ) && call_user_func ($ privacyClass , $ fieldArgs ) === false ) {
260
265
$ selectable = null ;
261
266
}
262
267
// If Privacy class given
263
268
elseif (is_string ($ privacyClass )) {
264
269
if (Arr::has (self ::$ privacyValidations , $ privacyClass )) {
265
270
$ validated = self ::$ privacyValidations [$ privacyClass ];
266
271
} else {
267
- $ validated = call_user_func ([app ($ privacyClass ), 'fire ' ], self :: $ args );
272
+ $ validated = call_user_func ([app ($ privacyClass ), 'fire ' ], $ fieldArgs );
268
273
self ::$ privacyValidations [$ privacyClass ] = $ validated ;
269
274
}
270
275
0 commit comments