2
2
3
3
namespace Drupal \graphql_mutation \Plugin \GraphQL \Mutations ;
4
4
5
- use Drupal \graphql \Plugin \GraphQL \InputTypes \InputTypePluginBase ;
6
- use Drupal \graphql_mutation \Plugin \GraphQL \InputTypes \EntityInputField ;
5
+ use Drupal \graphql \GraphQL \Schema \Schema ;
6
+ use Drupal \graphql \GraphQL \Type \InputObjectType ;
7
+ use Drupal \graphql \Plugin \GraphQL \PluggableSchemaPluginInterface ;
8
+ use Youshido \GraphQL \Execution \ResolveInfo ;
7
9
use Youshido \GraphQL \Type \Scalar \AbstractScalarType ;
8
10
9
11
trait EntityMutationInputTrait {
10
12
13
+ /**
14
+ * Loads the schema builder from the resolve info.
15
+ *
16
+ * @param \Youshido\GraphQL\Execution\ResolveInfo $info
17
+ * The resolve info object.
18
+ *
19
+ * @return \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface
20
+ * The schema builder.
21
+ */
22
+ protected function getSchemaBuilderFromResolveInfo (ResolveInfo $ info ) {
23
+ $ schema = isset ($ info ) ? $ info ->getExecutionContext ()->getSchema () : NULL ;
24
+ if (!$ schema instanceof Schema) {
25
+ throw new \LogicException ('Could not load schema from execution context. ' );
26
+ }
27
+
28
+ $ schemaPlugin = $ schema ->getSchemaPlugin ();
29
+ if (!$ schemaPlugin instanceof PluggableSchemaPluginInterface) {
30
+ throw new \LogicException ('Could not load schema plugin from schema. ' );
31
+ }
32
+
33
+ return $ schemaPlugin ->getSchemaBuilder ();
34
+ }
35
+
11
36
/**
12
37
* Extract entity values from the resolver args.
13
38
*
14
39
* Loops over all input values and assigns them to their original field names.
15
40
*
16
41
* @param array $inputArgs
17
42
* The entity values provided through the resolver args.
18
- * @param \Drupal\graphql\Plugin\ GraphQL\InputTypes\InputTypePluginBase $inputType
43
+ * @param \Drupal\graphql\GraphQL\Type\InputObjectType $inputType
19
44
* The input type.
45
+ * @param \Youshido\GraphQL\Execution\ResolveInfo $info
46
+ * The resolve info object.
20
47
*
21
48
* @return array
22
49
* The extracted entity values with their proper, internal field names.
23
50
*/
24
- protected function extractEntityInput (array $ inputArgs , InputTypePluginBase $ inputType ) {
25
- $ fields = $ inputType ->getPluginDefinition ()['fields ' ];
26
- return array_reduce (array_keys ($ inputArgs ), function ($ carry , $ current ) use ($ fields , $ inputArgs , $ inputType ) {
51
+ protected function extractEntityInput (array $ inputArgs , InputObjectType $ inputType , ResolveInfo $ info ) {
52
+ $ builder = $ this ->getSchemaBuilderFromResolveInfo ($ info );
53
+ $ fields = $ inputType ->getPlugin ($ builder )->getPluginDefinition ()['fields ' ];
54
+ return array_reduce (array_keys ($ inputArgs ), function ($ carry , $ current ) use ($ fields , $ inputArgs , $ inputType , $ info ) {
27
55
$ isMulti = $ fields [$ current ]['multi ' ];
28
56
$ fieldName = $ fields [$ current ]['field_name ' ];
29
57
$ fieldValue = $ inputArgs [$ current ];
58
+
59
+ /** @var \Drupal\graphql\GraphQL\Type\InputObjectType $fieldType */
30
60
$ fieldType = $ inputType ->getField ($ current )->getType ()->getNamedType ();
31
61
32
62
if ($ fieldType instanceof AbstractScalarType) {
33
63
return $ carry + [$ fieldName => $ fieldValue ];
34
64
}
35
65
36
- if ($ fieldType instanceof EntityInputField ) {
37
- $ fieldValue = $ isMulti ? array_map (function ($ value ) use ($ fieldType ) {
38
- return $ this ->extractEntityFieldInput ($ value , $ fieldType );
39
- }, $ fieldValue ) : $ this ->extractEntityFieldInput ($ fieldValue , $ fieldType );
66
+ if ($ fieldType instanceof InputObjectType ) {
67
+ $ fieldValue = $ isMulti ? array_map (function ($ value ) use ($ fieldType, $ info ) {
68
+ return $ this ->extractEntityFieldInput ($ value , $ fieldType, $ info );
69
+ }, $ fieldValue ) : $ this ->extractEntityFieldInput ($ fieldValue , $ fieldType, $ info );
40
70
41
71
return $ carry + [$ fieldName => $ fieldValue ];
42
72
}
@@ -53,14 +83,17 @@ protected function extractEntityInput(array $inputArgs, InputTypePluginBase $inp
53
83
*
54
84
* @param array $fieldValue
55
85
* The field values keyed by property name.
56
- * @param \Drupal\graphql\Plugin\ GraphQL\InputTypes\InputTypePluginBase $fieldType
86
+ * @param \Drupal\graphql\GraphQL\Type\InputObjectType $fieldType
57
87
* The field type.
88
+ * @param \Youshido\GraphQL\Execution\ResolveInfo $info
89
+ * The resolve info object.
58
90
*
59
91
* @return array
60
92
* The extracted field values with their proper, internal property names.
61
93
*/
62
- protected function extractEntityFieldInput (array $ fieldValue , InputTypePluginBase $ fieldType ) {
63
- $ properties = $ fieldType ->getPluginDefinition ()['fields ' ];
94
+ protected function extractEntityFieldInput (array $ fieldValue , InputObjectType $ fieldType , ResolveInfo $ info ) {
95
+ $ builder = $ this ->getSchemaBuilderFromResolveInfo ($ info );
96
+ $ properties = $ fieldType ->getPlugin ($ builder )->getPluginDefinition ()['fields ' ];
64
97
return array_reduce (array_keys ($ fieldValue ), function ($ carry , $ current ) use ($ properties , $ fieldValue ) {
65
98
$ key = $ properties [$ current ]['property_name ' ];
66
99
$ value = $ fieldValue [$ current ];
0 commit comments