1111use Doctrine \ORM \EntityManager ;
1212use Exception ;
1313use GraphQL \Error \Error ;
14+ use GraphQL \Type \Definition \InputObjectField ;
1415use GraphQL \Type \Definition \InputObjectType ;
1516use GraphQL \Type \Definition \Type ;
1617
@@ -36,58 +37,63 @@ public function __construct(
3637 */
3738 public function get (string $ id , array $ requiredFields = [], array $ optionalFields = []): InputObjectType
3839 {
40+ $ fields = [];
3941 $ targetEntity = $ this ->metadata ->get ($ id );
4042
41- /** @psalm-suppress InvalidArgument */
42- return new InputObjectType ([
43- 'name ' => $ targetEntity ->getTypeName () . '_Input ' ,
44- 'description ' => $ targetEntity ->getDescription (),
45- 'fields ' => function () use ($ id , $ targetEntity , $ requiredFields , $ optionalFields ): array {
46- $ fields = [];
47-
48- foreach ($ this ->entityManager ->getClassMetadata ($ id )->getFieldNames () as $ fieldName ) {
49- /**
50- * Do not include identifiers as input. In the majority of cases there will be
51- * no reason to set or update an identifier. For the case where an identifier
52- * should be set or updated, this facotry is not the correct solution.
53- */
54- if (! empty ($ optionalFields ) || ! empty ($ requiredFields )) {
55- // Include field as optional
56- if (in_array ($ fieldName , $ optionalFields ) || $ optionalFields === ['* ' ]) {
57- if ($ optionalFields === ['* ' ] && $ this ->entityManager ->getClassMetadata ($ id )->isIdentifier ($ fieldName )) {
58- continue ;
59- }
60-
61- $ fields [$ fieldName ]['description ' ] = $ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['description ' ];
62- $ fields [$ fieldName ]['type ' ] = $ this ->typeManager ->get ($ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['type ' ]);
63- }
64-
65- // Include field as required
66- if (in_array ($ fieldName , $ requiredFields ) || $ requiredFields === ['* ' ]) {
67- if ($ requiredFields === ['* ' ] && $ this ->entityManager ->getClassMetadata ($ id )->isIdentifier ($ fieldName )) {
68- continue ;
69- }
43+ foreach ($ this ->entityManager ->getClassMetadata ($ id )->getFieldNames () as $ fieldName ) {
44+ /**
45+ * Do not include identifiers as input. In the majority of cases there will be
46+ * no reason to set or update an identifier. For the case where an identifier
47+ * should be set or updated, this facotry is not the correct solution.
48+ */
49+ if (! empty ($ optionalFields ) || ! empty ($ requiredFields )) {
50+ // Include field as optional
51+ if (in_array ($ fieldName , $ optionalFields ) || $ optionalFields === ['* ' ]) {
52+ if ($ optionalFields === ['* ' ] && $ this ->entityManager ->getClassMetadata ($ id )->isIdentifier ($ fieldName )) {
53+ continue ;
54+ }
7055
71- if ($ this ->entityManager ->getClassMetadata ($ id )->isIdentifier ($ fieldName )) {
72- throw new Exception ('Identifier ' . $ fieldName . ' is an invalid input. ' );
73- }
56+ $ fields [$ fieldName ] = new InputObjectField ([
57+ 'name ' => $ fieldName ,
58+ 'description ' => (string ) $ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['description ' ],
59+ 'type ' => $ this ->typeManager ->get ($ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['type ' ]),
60+ ]);
61+ }
7462
75- $ fields [$ fieldName ]['description ' ] = $ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['description ' ];
76- $ fields [$ fieldName ]['type ' ] = Type::nonNull ($ this ->typeManager ->get ($ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['type ' ]));
77- }
78- } else {
79- // All fields are required
80- if ($ this ->entityManager ->getClassMetadata ($ id )->isIdentifier ($ fieldName )) {
81- continue ;
82- }
63+ // Include field as required
64+ if (in_array ($ fieldName , $ requiredFields ) || $ requiredFields === ['* ' ]) {
65+ if ($ requiredFields === ['* ' ] && $ this ->entityManager ->getClassMetadata ($ id )->isIdentifier ($ fieldName )) {
66+ continue ;
67+ }
8368
84- $ fields [ $ fieldName ][ ' description ' ] = $ targetEntity -> getMetadataConfig ()[ ' fields ' ][ $ fieldName][ ' description ' ];
85- $ fields [ $ fieldName ][ ' type ' ] = Type:: nonNull ( $ this -> typeManager -> get ( $ targetEntity -> getMetadataConfig ()[ ' fields ' ][ $ fieldName ][ ' type ' ]) );
69+ if ( $ this -> entityManager -> getClassMetadata ( $ id )-> isIdentifier ( $ fieldName)) {
70+ throw new Exception ( ' Identifier ' . $ fieldName . ' is an invalid input. ' );
8671 }
72+
73+ $ fields [$ fieldName ] = new InputObjectField ([
74+ 'name ' => $ fieldName ,
75+ 'description ' => (string ) $ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['description ' ],
76+ 'type ' => Type::nonNull ($ this ->typeManager ->get ($ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['type ' ])),
77+ ]);
8778 }
79+ } else {
80+ // All fields are required
81+ if ($ this ->entityManager ->getClassMetadata ($ id )->isIdentifier ($ fieldName )) {
82+ continue ;
83+ }
84+
85+ $ fields [$ fieldName ] = new InputObjectField ([
86+ 'name ' => $ fieldName ,
87+ 'description ' => (string ) $ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['description ' ],
88+ 'type ' => Type::nonNull ($ this ->typeManager ->get ($ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['type ' ])),
89+ ]);
90+ }
91+ }
8892
89- return $ fields ;
90- },
93+ return new InputObjectType ([
94+ 'name ' => $ targetEntity ->getTypeName () . '_Input ' ,
95+ 'description ' => $ targetEntity ->getDescription (),
96+ 'fields ' => static fn () => $ fields ,
9197 ]);
9298 }
9399}
0 commit comments