@@ -29,21 +29,51 @@ public function __construct(
2929 }
3030
3131 /**
32- * @param string[] $optionalFields
33- * @param array<array-key, InputObjectField> $fields
32+ * @param string[] $requiredFields An optional list of just the required fields you want for the mutation.
33+ * This allows specific fields per mutation.
34+ * @param string[] $optionalFields An optional list of optional fields you want for the mutation.
35+ * This allows specific fields per mutation.
3436 *
35- * @return array<array-key, InputObjectField>
37+ * @throws Error
38+ */
39+ public function get (string $ id , array $ requiredFields = [], array $ optionalFields = []): InputObjectType
40+ {
41+ $ fields = [];
42+ $ targetEntity = $ this ->metadata ->get ($ id );
43+
44+ if (! count ($ requiredFields ) && ! count ($ optionalFields )) {
45+ $ this ->addAllFieldsAsRequired ($ targetEntity , $ fields );
46+ } else {
47+ $ this ->addRequiredFields ($ targetEntity , $ requiredFields , $ fields );
48+ $ this ->addOptionalFields ($ targetEntity , $ optionalFields , $ fields );
49+ }
50+
51+ return new InputObjectType ([
52+ 'name ' => $ targetEntity ->getTypeName () . '_Input ' ,
53+ 'description ' => $ targetEntity ->getDescription (),
54+ 'fields ' => static fn () => $ fields ,
55+ ]);
56+ }
57+
58+ /**
59+ * @param string[] $optionalFields
60+ * @param array<int, InputObjectField> $fields
3661 */
3762 protected function addOptionalFields (
3863 mixed $ targetEntity ,
3964 array $ optionalFields ,
40- array $ fields ,
41- ): array {
65+ array & $ fields ,
66+ ): void {
4267 foreach ($ this ->entityManager ->getClassMetadata ($ targetEntity ->getEntityClass ())->getFieldNames () as $ fieldName ) {
4368 if (! in_array ($ fieldName , $ optionalFields ) && $ optionalFields !== ['* ' ]) {
4469 continue ;
4570 }
4671
72+ /**
73+ * Do not include identifiers as input. In the majority of cases there will be
74+ * no reason to set or update an identifier. For the case where an identifier
75+ * should be set or updated, this factory is not the correct solution.
76+ */
4777 if ($ optionalFields === ['* ' ] && $ this ->entityManager ->getClassMetadata ($ targetEntity ->getEntityClass ())->isIdentifier ($ fieldName )) {
4878 continue ;
4979 }
@@ -54,27 +84,31 @@ protected function addOptionalFields(
5484 'type ' => $ this ->typeManager ->get ($ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['type ' ]),
5585 ]);
5686 }
57-
58- return $ fields ;
5987 }
6088
6189 /**
62- * @param string[] $requiredFields
63- * @param array<array-key, InputObjectField> $fields
64- *
65- * @return array<array-key, InputObjectField>
90+ * @param string[] $requiredFields
91+ * @param array<int, InputObjectField> $fields
6692 */
6793 protected function addRequiredFields (
6894 mixed $ targetEntity ,
6995 array $ requiredFields ,
70- array $ fields ,
71- ): array {
96+ array & $ fields ,
97+ ): void {
7298 foreach ($ this ->entityManager ->getClassMetadata ($ targetEntity ->getEntityClass ())->getFieldNames () as $ fieldName ) {
7399 if (! in_array ($ fieldName , $ requiredFields ) && $ requiredFields !== ['* ' ]) {
74100 continue ;
75101 }
76102
77- if ($ requiredFields === ['* ' ] && $ this ->entityManager ->getClassMetadata ($ targetEntity ->getEntityClass ())->isIdentifier ($ fieldName )) {
103+ /**
104+ * Do not include identifiers as input. In the majority of cases there will be
105+ * no reason to set or update an identifier. For the case where an identifier
106+ * should be set or updated, this factory is not the correct solution.
107+ */
108+ if (
109+ $ requiredFields === ['* ' ]
110+ && $ this ->entityManager ->getClassMetadata ($ targetEntity ->getEntityClass ())->isIdentifier ($ fieldName )
111+ ) {
78112 continue ;
79113 }
80114
@@ -85,21 +119,22 @@ protected function addRequiredFields(
85119 $ fields [$ fieldName ] = new InputObjectField ([
86120 'name ' => $ fieldName ,
87121 'description ' => (string ) $ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['description ' ],
88- 'type ' => Type::nonNull ($ this ->typeManager ->get ($ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['type ' ])),
122+ 'type ' => Type::nonNull ($ this ->typeManager ->get (
123+ $ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['type ' ],
124+ )),
89125 ]);
90126 }
91-
92- return $ fields ;
93127 }
94128
95- /**
96- * @param array<array-key, InputObjectField> $fields
97- *
98- * @return array<array-key, InputObjectField>
99- */
100- protected function addAllFieldsAsRequired (mixed $ targetEntity , array $ fields ): array
129+ /** @param array<int, InputObjectField> $fields */
130+ protected function addAllFieldsAsRequired (mixed $ targetEntity , array &$ fields ): void
101131 {
102132 foreach ($ this ->entityManager ->getClassMetadata ($ targetEntity ->getEntityClass ())->getFieldNames () as $ fieldName ) {
133+ /**
134+ * Do not include identifiers as input. In the majority of cases there will be
135+ * no reason to set or update an identifier. For the case where an identifier
136+ * should be set or updated, this factory is not the correct solution.
137+ */
103138 if ($ this ->entityManager ->getClassMetadata ($ targetEntity ->getEntityClass ())->isIdentifier ($ fieldName )) {
104139 continue ;
105140 }
@@ -110,40 +145,5 @@ protected function addAllFieldsAsRequired(mixed $targetEntity, array $fields): a
110145 'type ' => Type::nonNull ($ this ->typeManager ->get ($ targetEntity ->getMetadataConfig ()['fields ' ][$ fieldName ]['type ' ])),
111146 ]);
112147 }
113-
114- return $ fields ;
115- }
116-
117- /**
118- * @param string[] $requiredFields An optional list of just the required fields you want for the mutation.
119- * This allows specific fields per mutation.
120- * @param string[] $optionalFields An optional list of optional fields you want for the mutation.
121- * This allows specific fields per mutation.
122- *
123- * @throws Error
124- */
125- public function get (string $ id , array $ requiredFields = [], array $ optionalFields = []): InputObjectType
126- {
127- $ fields = [];
128- $ targetEntity = $ this ->metadata ->get ($ id );
129-
130- /**
131- * Do not include identifiers as input. In the majority of cases there will be
132- * no reason to set or update an identifier. For the case where an identifier
133- * should be set or updated, this facotry is not the correct solution.
134- */
135-
136- if (! count ($ requiredFields ) && ! count ($ optionalFields )) {
137- $ fields = $ this ->addAllFieldsAsRequired ($ targetEntity , $ fields );
138- } else {
139- $ fields = $ this ->addRequiredFields ($ targetEntity , $ requiredFields , $ fields );
140- $ fields = $ this ->addOptionalFields ($ targetEntity , $ optionalFields , $ fields );
141- }
142-
143- return new InputObjectType ([
144- 'name ' => $ targetEntity ->getTypeName () . '_Input ' ,
145- 'description ' => $ targetEntity ->getDescription (),
146- 'fields ' => static fn () => $ fields ,
147- ]);
148148 }
149149}
0 commit comments