38
38
use TheCodingMachine \GraphQLite \Middlewares \FieldMiddlewareInterface ;
39
39
use TheCodingMachine \GraphQLite \Middlewares \InputFieldHandlerInterface ;
40
40
use TheCodingMachine \GraphQLite \Middlewares \InputFieldMiddlewareInterface ;
41
+ use TheCodingMachine \GraphQLite \Middlewares \MagicPropertyResolver ;
41
42
use TheCodingMachine \GraphQLite \Middlewares \MissingMagicGetException ;
43
+ use TheCodingMachine \GraphQLite \Middlewares \ServiceResolver ;
44
+ use TheCodingMachine \GraphQLite \Middlewares \SourceConstructorParameterResolver ;
45
+ use TheCodingMachine \GraphQLite \Middlewares \SourceInputPropertyResolver ;
46
+ use TheCodingMachine \GraphQLite \Middlewares \SourceMethodResolver ;
47
+ use TheCodingMachine \GraphQLite \Middlewares \SourcePropertyResolver ;
42
48
use TheCodingMachine \GraphQLite \Parameters \InputTypeParameterInterface ;
43
49
use TheCodingMachine \GraphQLite \Parameters \ParameterInterface ;
44
50
use TheCodingMachine \GraphQLite \Parameters \PrefetchDataParameter ;
@@ -370,14 +376,6 @@ private function getFieldsByMethodAnnotations(string|object $controller, Reflect
370
376
$ type = $ this ->typeMapper ->mapReturnType ($ refMethod , $ docBlockObj );
371
377
}
372
378
373
- $ fieldDescriptor = new QueryFieldDescriptor (
374
- name: $ name ,
375
- type: $ type ,
376
- comment: trim ($ description ),
377
- deprecationReason: $ this ->getDeprecationReason ($ docBlockObj ),
378
- refMethod: $ refMethod ,
379
- );
380
-
381
379
$ parameters = $ refMethod ->getParameters ();
382
380
if ($ injectSource === true ) {
383
381
$ firstParameter = array_shift ($ parameters );
@@ -398,19 +396,19 @@ private function getFieldsByMethodAnnotations(string|object $controller, Reflect
398
396
$ args = ['__graphqlite_prefectData ' => $ prefetchDataParameter , ...$ args ];
399
397
}
400
398
401
- $ fieldDescriptor = $ fieldDescriptor -> withParameters ( $ args );
399
+ $ resolver = is_string ( $ controller ) ? new SourceMethodResolver ( $ refMethod ) : new ServiceResolver ([ $ controller , $ methodName ] );
402
400
403
- if ( is_string ( $ controller )) {
404
- $ fieldDescriptor = $ fieldDescriptor -> withTargetMethodOnSource ( $ refMethod -> getDeclaringClass ()-> getName (), $ methodName );
405
- } else {
406
- $ callable = [ $ controller , $ methodName ];
407
- assert ( is_callable ( $ callable ));
408
- $ fieldDescriptor = $ fieldDescriptor -> withCallable ( $ callable );
409
- }
410
-
411
- $ fieldDescriptor = $ fieldDescriptor
412
- -> withInjectSource ( $ injectSource )
413
- -> withMiddlewareAnnotations ( $ this -> annotationReader -> getMiddlewareAnnotations ( $ refMethod ) );
401
+ $ fieldDescriptor = new QueryFieldDescriptor (
402
+ name: $ name ,
403
+ type: $ type ,
404
+ resolver: $ resolver ,
405
+ originalResolver: $ resolver ,
406
+ parameters: $ args ,
407
+ injectSource: $ injectSource ,
408
+ comment: trim ( $ description ),
409
+ deprecationReason: $ this -> getDeprecationReason ( $ docBlockObj ),
410
+ middlewareAnnotations: $ this -> annotationReader -> getMiddlewareAnnotations ( $ refMethod ),
411
+ );
414
412
415
413
$ field = $ this ->fieldMiddleware ->process ($ fieldDescriptor , new class implements FieldHandlerInterface {
416
414
public function handle (QueryFieldDescriptor $ fieldDescriptor ): FieldDefinition |null
@@ -480,26 +478,21 @@ private function getFieldsByPropertyAnnotations(string|object $controller, Refle
480
478
assert ($ type instanceof OutputType);
481
479
}
482
480
481
+ $ resolver = is_string ($ controller ) ?
482
+ new SourcePropertyResolver ($ refProperty ) :
483
+ new ServiceResolver (fn () => PropertyAccessor::getValue ($ controller , $ refProperty ->getName ()));
484
+
483
485
$ fieldDescriptor = new QueryFieldDescriptor (
484
486
name: $ name ,
485
487
type: $ type ,
488
+ resolver: $ resolver ,
489
+ originalResolver: $ resolver ,
490
+ injectSource: false ,
486
491
comment: trim ($ description ),
487
492
deprecationReason: $ this ->getDeprecationReason ($ docBlock ),
488
- refProperty : $ refProperty ,
493
+ middlewareAnnotations : $ this -> annotationReader -> getMiddlewareAnnotations ( $ refProperty) ,
489
494
);
490
495
491
- if (is_string ($ controller )) {
492
- $ fieldDescriptor = $ fieldDescriptor ->withTargetPropertyOnSource ($ refProperty ->getDeclaringClass ()->getName (), $ refProperty ->getName ());
493
- } else {
494
- $ fieldDescriptor = $ fieldDescriptor ->withCallable (static function () use ($ controller , $ refProperty ) {
495
- return PropertyAccessor::getValue ($ controller , $ refProperty ->getName ());
496
- });
497
- }
498
-
499
- $ fieldDescriptor = $ fieldDescriptor
500
- ->withInjectSource (false )
501
- ->withMiddlewareAnnotations ($ this ->annotationReader ->getMiddlewareAnnotations ($ refProperty ));
502
-
503
496
$ field = $ this ->fieldMiddleware ->process ($ fieldDescriptor , new class implements FieldHandlerInterface {
504
497
public function handle (QueryFieldDescriptor $ fieldDescriptor ): FieldDefinition |null
505
498
{
@@ -597,15 +590,16 @@ private function getQueryFieldsFromSourceFields(array $sourceFields, ReflectionC
597
590
$ type = $ this ->typeMapper ->mapReturnType ($ refMethod , $ docBlockObj );
598
591
}
599
592
593
+ $ resolver = new SourceMethodResolver ($ refMethod );
594
+
600
595
$ fieldDescriptor = new QueryFieldDescriptor (
601
596
name: $ sourceField ->getName (),
602
597
type: $ type ,
598
+ resolver: $ resolver ,
599
+ originalResolver: $ resolver ,
603
600
parameters: $ args ,
604
- targetClass: $ refMethod ->getDeclaringClass ()->getName (),
605
- targetMethodOnSource: $ methodName ,
606
601
comment: $ description ,
607
602
deprecationReason: $ deprecationReason ?? null ,
608
- refMethod: $ refMethod ,
609
603
);
610
604
} else {
611
605
$ outputType = $ sourceField ->getOutputType ();
@@ -619,11 +613,13 @@ private function getQueryFieldsFromSourceFields(array $sourceFields, ReflectionC
619
613
$ type = $ this ->resolvePhpType ($ phpTypeStr , $ refClass , $ magicGefRefMethod );
620
614
}
621
615
616
+ $ resolver = new MagicPropertyResolver ($ refClass ->getName (), $ sourceField ->getSourceName () ?? $ sourceField ->getName ());
617
+
622
618
$ fieldDescriptor = new QueryFieldDescriptor (
623
619
name: $ sourceField ->getName (),
624
620
type: $ type ,
625
- targetClass : $ refClass -> getName () ,
626
- magicProperty : $ sourceField -> getSourceName () ?? $ sourceField -> getName () ,
621
+ resolver : $ resolver ,
622
+ originalResolver : $ resolver ,
627
623
comment: $ sourceField ->getDescription (),
628
624
);
629
625
}
@@ -889,27 +885,22 @@ private function getInputFieldsByMethodAnnotations(string|object $controller, Re
889
885
890
886
assert ($ type instanceof InputType);
891
887
888
+ $ resolver = new SourceMethodResolver ($ refMethod );
889
+
892
890
$ inputFieldDescriptor = new InputFieldDescriptor (
893
891
name: $ name ,
894
892
type: $ type ,
893
+ resolver: $ resolver ,
894
+ originalResolver: $ resolver ,
895
895
parameters: $ args ,
896
+ injectSource: $ injectSource ,
896
897
comment: trim ($ description ),
897
- refMethod : $ refMethod ,
898
+ middlewareAnnotations : $ this -> annotationReader -> getMiddlewareAnnotations ( $ refMethod) ,
898
899
isUpdate: $ isUpdate ,
900
+ hasDefaultValue: $ isUpdate ,
901
+ defaultValue: $ args [$ name ]->getDefaultValue ()
899
902
);
900
903
901
- $ inputFieldDescriptor = $ inputFieldDescriptor
902
- ->withHasDefaultValue ($ isUpdate )
903
- ->withDefaultValue ($ args [$ name ]->getDefaultValue ());
904
- $ constructerParameters = $ this ->getClassConstructParameterNames ($ refClass );
905
- if (!in_array ($ name , $ constructerParameters )) {
906
- $ inputFieldDescriptor = $ inputFieldDescriptor ->withTargetMethodOnSource ($ refMethod ->getDeclaringClass ()->getName (), $ methodName );
907
- }
908
-
909
- $ inputFieldDescriptor = $ inputFieldDescriptor
910
- ->withInjectSource ($ injectSource )
911
- ->withMiddlewareAnnotations ($ this ->annotationReader ->getMiddlewareAnnotations ($ refMethod ));
912
-
913
904
$ field = $ this ->inputFieldMiddleware ->process ($ inputFieldDescriptor , new class implements InputFieldHandlerInterface {
914
905
public function handle (InputFieldDescriptor $ inputFieldDescriptor ): InputField |null
915
906
{
@@ -965,53 +956,38 @@ private function getInputFieldsByPropertyAnnotations(string|object $controller,
965
956
$ description = $ inputProperty ->getDescription ();
966
957
}
967
958
968
- if (in_array ($ name , $ constructerParameters )) {
969
- $ middlewareAnnotations = $ this ->annotationReader ->getPropertyAnnotations ($ refProperty , MiddlewareAnnotationInterface::class);
970
- if ($ middlewareAnnotations !== []) {
971
- throw IncompatibleAnnotationsException::middlewareAnnotationsUnsupported ();
972
- }
973
- // constructor hydrated
974
- $ field = new InputField (
975
- $ name ,
976
- $ inputProperty ->getType (),
977
- [$ inputProperty ->getName () => $ inputProperty ],
978
- null ,
979
- null ,
980
- trim ($ description ),
981
- $ isUpdate ,
982
- $ inputProperty ->hasDefaultValue (),
983
- $ inputProperty ->getDefaultValue (),
984
- );
985
- } else {
986
- $ type = $ inputProperty ->getType ();
987
- if (!$ inputType && $ isUpdate && $ type instanceof NonNull) {
988
- $ type = $ type ->getWrappedType ();
989
- }
990
- assert ($ type instanceof InputType);
959
+ $ type = $ inputProperty ->getType ();
960
+ if (!$ inputType && $ isUpdate && $ type instanceof NonNull) {
961
+ $ type = $ type ->getWrappedType ();
962
+ }
963
+ assert ($ type instanceof InputType);
964
+ $ forConstructorHydration = in_array ($ name , $ constructerParameters );
965
+ $ resolver = $ forConstructorHydration ?
966
+ new SourceConstructorParameterResolver ($ refProperty ->getDeclaringClass ()->getName (), $ refProperty ->getName ()) :
967
+ new SourceInputPropertyResolver ($ refProperty );
991
968
992
- // setters and properties
993
- $ inputFieldDescriptor = new InputFieldDescriptor (
994
- name: $ inputProperty ->getName (),
995
- type: $ type ,
996
- parameters: [ $ inputProperty -> getName () => $ inputProperty ] ,
997
- targetClass : $ refProperty -> getDeclaringClass ()-> getName () ,
998
- targetPropertyOnSource: $ refProperty ->getName (),
999
- injectSource: false ,
1000
- comment: trim ( $ description ) ,
1001
- middlewareAnnotations: $ this -> annotationReader -> getMiddlewareAnnotations ( $ refProperty ),
1002
- refProperty : $ refProperty ,
1003
- isUpdate: $ isUpdate ,
1004
- hasDefaultValue: $ inputProperty ->hasDefaultValue (),
1005
- defaultValue: $ inputProperty ->getDefaultValue (),
1006
- );
969
+ // setters and properties
970
+ $ inputFieldDescriptor = new InputFieldDescriptor (
971
+ name: $ inputProperty ->getName (),
972
+ type: $ type ,
973
+ resolver: $ resolver ,
974
+ originalResolver : $ resolver ,
975
+ parameters: [ $ inputProperty ->getName () => $ inputProperty ] ,
976
+ injectSource: false ,
977
+ forConstructorHydration: $ forConstructorHydration ,
978
+ comment: trim ( $ description ),
979
+ middlewareAnnotations : $ this -> annotationReader -> getMiddlewareAnnotations ( $ refProperty) ,
980
+ isUpdate: $ isUpdate ,
981
+ hasDefaultValue: $ inputProperty ->hasDefaultValue (),
982
+ defaultValue: $ inputProperty ->getDefaultValue (),
983
+ );
1007
984
1008
- $ field = $ this ->inputFieldMiddleware ->process ($ inputFieldDescriptor , new class implements InputFieldHandlerInterface {
1009
- public function handle (InputFieldDescriptor $ inputFieldDescriptor ): InputField |null
1010
- {
1011
- return InputField::fromFieldDescriptor ($ inputFieldDescriptor );
1012
- }
1013
- });
1014
- }
985
+ $ field = $ this ->inputFieldMiddleware ->process ($ inputFieldDescriptor , new class implements InputFieldHandlerInterface {
986
+ public function handle (InputFieldDescriptor $ inputFieldDescriptor ): InputField |null
987
+ {
988
+ return InputField::fromFieldDescriptor ($ inputFieldDescriptor );
989
+ }
990
+ });
1015
991
1016
992
if ($ field === null ) {
1017
993
continue ;
0 commit comments