@@ -35,15 +35,17 @@ class SelectFields
3535 * @param GraphqlType $parentType
3636 * @param array $queryArgs Arguments given with the query/mutation
3737 * @param int $depth The depth to walk the AST and introspect for nested relations
38+ * @param mixed $ctx The GraphQL context; can be anything and is only passed through
39+ * Can be created/overridden by \Rebing\GraphQL\GraphQLController::queryContext
3840 */
39- public function __construct (ResolveInfo $ info , GraphqlType $ parentType , array $ queryArgs , int $ depth )
41+ public function __construct (ResolveInfo $ info , GraphqlType $ parentType , array $ queryArgs , int $ depth, $ ctx )
4042 {
4143 if ($ parentType instanceof WrappingType) {
4244 $ parentType = $ parentType ->getWrappedType (true );
4345 }
4446
4547 $ requestedFields = $ this ->getFieldSelection ($ info , $ queryArgs , $ depth );
46- $ fields = self ::getSelectableFieldsAndRelations ($ queryArgs , $ requestedFields , $ parentType );
48+ $ fields = self ::getSelectableFieldsAndRelations ($ queryArgs , $ requestedFields , $ parentType, null , true , $ ctx );
4749 $ this ->select = $ fields [0 ];
4850 $ this ->relations = $ fields [1 ];
4951 }
@@ -62,17 +64,24 @@ private function getFieldSelection(ResolveInfo $resolveInfo, array $args, int $d
6264 * Retrieve the fields (top level) and relations that
6365 * will be selected with the query.
6466 *
65- * @param array $queryArgs Arguments given with the query/mutation
67+ * @param array $queryArgs Arguments given with the query/mutation
6668 * @param array $requestedFields
6769 * @param GraphqlType $parentType
6870 * @param Closure|null $customQuery
6971 * @param bool $topLevel
72+ * @param mixed $ctx The GraphQL context; can be anything and is only passed through
7073 * @return array|Closure - if first recursion, return an array,
7174 * where the first key is 'select' array and second is 'with' array.
7275 * On other recursions return a closure that will be used in with
7376 */
74- public static function getSelectableFieldsAndRelations (array $ queryArgs , array $ requestedFields , GraphqlType $ parentType , ?Closure $ customQuery = null , bool $ topLevel = true )
75- {
77+ public static function getSelectableFieldsAndRelations (
78+ array $ queryArgs ,
79+ array $ requestedFields ,
80+ GraphqlType $ parentType ,
81+ ?Closure $ customQuery = null ,
82+ bool $ topLevel = true ,
83+ $ ctx = null
84+ ) {
7685 $ select = [];
7786 $ with = [];
7887
@@ -82,7 +91,7 @@ public static function getSelectableFieldsAndRelations(array $queryArgs, array $
8291 $ parentTable = self ::getTableNameFromParentType ($ parentType );
8392 $ primaryKey = self ::getPrimaryKeyFromParentType ($ parentType );
8493
85- self ::handleFields ($ queryArgs , $ requestedFields , $ parentType , $ select , $ with );
94+ self ::handleFields ($ queryArgs , $ requestedFields , $ parentType , $ select , $ with, $ ctx );
8695
8796 // If a primary key is given, but not in the selects, add it
8897 if (null !== $ primaryKey ) {
@@ -95,9 +104,9 @@ public static function getSelectableFieldsAndRelations(array $queryArgs, array $
95104 if ($ topLevel ) {
96105 return [$ select , $ with ];
97106 } else {
98- return function ($ query ) use ($ with , $ select , $ customQuery , $ requestedFields ) {
107+ return function ($ query ) use ($ with , $ select , $ customQuery , $ requestedFields, $ ctx ) {
99108 if ($ customQuery ) {
100- $ query = $ customQuery ($ requestedFields ['args ' ], $ query );
109+ $ query = $ customQuery ($ requestedFields ['args ' ], $ query, $ ctx );
101110 }
102111
103112 $ query ->select ($ select );
@@ -115,9 +124,16 @@ public static function getSelectableFieldsAndRelations(array $queryArgs, array $
115124 * @param GraphqlType $parentType
116125 * @param array $select Passed by reference, adds further fields to select
117126 * @param array $with Passed by reference, adds further relations
127+ * @param mixed $ctx The GraphQL context; can be anything and is only passed through
118128 */
119- protected static function handleFields (array $ queryArgs , array $ requestedFields , GraphqlType $ parentType , array &$ select , array &$ with ): void
120- {
129+ protected static function handleFields (
130+ array $ queryArgs ,
131+ array $ requestedFields ,
132+ GraphqlType $ parentType ,
133+ array &$ select ,
134+ array &$ with ,
135+ $ ctx
136+ ): void {
121137 $ parentTable = self ::isMongodbInstance ($ parentType ) ? null : self ::getTableNameFromParentType ($ parentType );
122138
123139 foreach ($ requestedFields ['fields ' ] as $ key => $ field ) {
@@ -154,7 +170,7 @@ protected static function handleFields(array $queryArgs, array $requestedFields,
154170
155171 // Pagination
156172 if (is_a ($ parentType , config ('graphql.pagination_type ' , PaginationType::class))) {
157- self ::handleFields ($ queryArgs , $ field , $ fieldObject ->config ['type ' ]->getWrappedType (), $ select , $ with );
173+ self ::handleFields ($ queryArgs , $ field , $ fieldObject ->config ['type ' ]->getWrappedType (), $ select , $ with, $ ctx );
158174 }
159175 // With
160176 elseif (is_array ($ field ['fields ' ]) && $ queryable ) {
@@ -206,9 +222,9 @@ protected static function handleFields(array $queryArgs, array $requestedFields,
206222
207223 self ::addAlwaysFields ($ fieldObject , $ field , $ parentTable , true );
208224
209- $ with [$ relationsKey ] = self ::getSelectableFieldsAndRelations ($ queryArgs , $ field , $ newParentType , $ customQuery , false );
225+ $ with [$ relationsKey ] = self ::getSelectableFieldsAndRelations ($ queryArgs , $ field , $ newParentType , $ customQuery , false , $ ctx );
210226 } else {
211- self ::handleFields ($ queryArgs , $ field , $ fieldObject ->config ['type ' ], $ select , $ with );
227+ self ::handleFields ($ queryArgs , $ field , $ fieldObject ->config ['type ' ], $ select , $ with, $ ctx );
212228 }
213229 }
214230 // Select
@@ -310,8 +326,12 @@ private static function isQueryable(array $fieldObject): bool
310326 * @param string|null $parentTable
311327 * @param bool $forRelation
312328 */
313- protected static function addAlwaysFields (FieldDefinition $ fieldObject , array &$ select , ?string $ parentTable , bool $ forRelation = false ): void
314- {
329+ protected static function addAlwaysFields (
330+ FieldDefinition $ fieldObject ,
331+ array &$ select ,
332+ ?string $ parentTable ,
333+ bool $ forRelation = false
334+ ): void {
315335 if (isset ($ fieldObject ->config ['always ' ])) {
316336 $ always = $ fieldObject ->config ['always ' ];
317337
0 commit comments