Skip to content

Commit 7d4e4e1

Browse files
committed
Allows resolve event listeners to override or manipulate the resolved result
1 parent 36c001d commit 7d4e4e1

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

Event/ResolveEvent.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@ class ResolveEvent extends GenericEvent
1414

1515
/** @var array */
1616
private $astFields;
17+
18+
/** @var mixed|null */
19+
private $resolvedValue;
1720

1821
/**
1922
* Constructor.
2023
*
2124
* @param FieldInterface $field
2225
* @param array $astFields
26+
* @param mixed|null $resolvedValue
2327
*/
24-
public function __construct(FieldInterface $field, array $astFields)
28+
public function __construct(FieldInterface $field, array $astFields, $resolvedValue = null)
2529
{
2630
$this->field = $field;
2731
$this->astFields = $astFields;
28-
parent::__construct('ResolveEvent', [$field, $astFields]);
32+
$this->resolvedValue = $resolvedValue;
33+
parent::__construct('ResolveEvent', [$field, $astFields, $resolvedValue]);
2934
}
3035

3136
/**
@@ -47,4 +52,25 @@ public function getAstFields()
4752
{
4853
return $this->astFields;
4954
}
55+
56+
/**
57+
* Returns the resolved value.
58+
*
59+
* @return mixed|null
60+
*/
61+
public function getResolvedValue()
62+
{
63+
return $this->resolvedValue;
64+
}
65+
66+
/**
67+
* Allows the event listener to manipulate the resolved value.
68+
*
69+
* @param $resolvedValue
70+
*/
71+
public function setResolvedValue($resolvedValue)
72+
{
73+
$this->resolvedValue = $resolvedValue;
74+
}
5075
}
76+

Execution/Processor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $par
116116
$result = $field->resolve($parentValue, $arguments, $resolveInfo);
117117
}
118118

119-
$event = new ResolveEvent($field, $astFields);
119+
$event = new ResolveEvent($field, $astFields, $result);
120120
$this->eventDispatcher->dispatch('graphql.post_resolve', $event);
121-
return $result;
121+
return $event->getResolvedValue();
122122
}
123123

124124
private function assertClientHasOperationAccess(Query $query)

0 commit comments

Comments
 (0)