Skip to content

Commit 94e1227

Browse files
committed
Fixing mutations after last graphql module version update.
1 parent 3005bee commit 94e1227

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

graphql_mutation.info.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ type: module
33
description: 'Core module with common fields and types for enabling mutations.'
44
package: GraphQL
55
core: 8.x
6-
hidden: true
76
dependencies:
87
- graphql_core

src/Plugin/GraphQL/Mutations/EntityMutationInputTrait.php

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,71 @@
22

33
namespace Drupal\graphql_mutation\Plugin\GraphQL\Mutations;
44

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;
79
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;
810

911
trait EntityMutationInputTrait {
1012

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+
1136
/**
1237
* Extract entity values from the resolver args.
1338
*
1439
* Loops over all input values and assigns them to their original field names.
1540
*
1641
* @param array $inputArgs
1742
* 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
1944
* The input type.
45+
* @param \Youshido\GraphQL\Execution\ResolveInfo $info
46+
* The resolve info object.
2047
*
2148
* @return array
2249
* The extracted entity values with their proper, internal field names.
2350
*/
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) {
2755
$isMulti = $fields[$current]['multi'];
2856
$fieldName = $fields[$current]['field_name'];
2957
$fieldValue = $inputArgs[$current];
58+
59+
/** @var \Drupal\graphql\GraphQL\Type\InputObjectType $fieldType */
3060
$fieldType = $inputType->getField($current)->getType()->getNamedType();
3161

3262
if ($fieldType instanceof AbstractScalarType) {
3363
return $carry + [$fieldName => $fieldValue];
3464
}
3565

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);
4070

4171
return $carry + [$fieldName => $fieldValue];
4272
}
@@ -53,14 +83,17 @@ protected function extractEntityInput(array $inputArgs, InputTypePluginBase $inp
5383
*
5484
* @param array $fieldValue
5585
* The field values keyed by property name.
56-
* @param \Drupal\graphql\Plugin\GraphQL\InputTypes\InputTypePluginBase $fieldType
86+
* @param \Drupal\graphql\GraphQL\Type\InputObjectType $fieldType
5787
* The field type.
88+
* @param \Youshido\GraphQL\Execution\ResolveInfo $info
89+
* The resolve info object.
5890
*
5991
* @return array
6092
* The extracted field values with their proper, internal property names.
6193
*/
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'];
6497
return array_reduce(array_keys($fieldValue), function($carry, $current) use ($properties, $fieldValue) {
6598
$key = $properties[$current]['property_name'];
6699
$value = $fieldValue[$current];

0 commit comments

Comments
 (0)