-
-
Notifications
You must be signed in to change notification settings - Fork 897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(graphql): added support for graphql subscriptions to work for actions #6904
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,14 +42,24 @@ public function __construct(private readonly CacheItemPoolInterface $subscriptio | |
|
||
public function retrieveSubscriptionId(array $context, ?array $result, ?Operation $operation = null): ?string | ||
{ | ||
|
||
psihius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** @var ResolveInfo $info */ | ||
$info = $context['info']; | ||
$fields = $info->getFieldSelection(\PHP_INT_MAX); | ||
$this->arrayRecursiveSort($fields, 'ksort'); | ||
$iri = $operation ? $this->getIdentifierFromOperation($operation, $context['args'] ?? []) : $this->getIdentifierFromContext($context); | ||
if (null === $iri) { | ||
if (empty($iri)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's avoid a function call here if this can return an empty array we should fix the return type of the above functions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It also returns an empty string if you provide a value, but it's not a valid URI. That's why I made empty call instead of doing |
||
return null; | ||
} | ||
$options = $operation->getMercure() ?? false; | ||
$private = $options['private'] ?? false; | ||
$privateFields = $options['private_fields'] ?? []; | ||
$previousObject = $context['graphql_context']['previous_object'] ?? null; | ||
if ($private && $privateFields && $previousObject) { | ||
foreach ($options['private_fields'] as $privateField) { | ||
$fields['__private_field_'.$privateField] = $this->getResourceId($privateField, $previousObject); | ||
} | ||
} | ||
$subscriptionsCacheItem = $this->subscriptionsCache->getItem($this->encodeIriToCacheKey($iri)); | ||
$subscriptions = []; | ||
if ($subscriptionsCacheItem->isHit()) { | ||
|
@@ -63,26 +73,129 @@ public function retrieveSubscriptionId(array $context, ?array $result, ?Operatio | |
|
||
$subscriptionId = $this->subscriptionIdentifierGenerator->generateSubscriptionIdentifier($fields); | ||
unset($result['clientSubscriptionId']); | ||
if ($private && $privateFields && $previousObject) { | ||
foreach ($options['private_fields'] as $privateField) { | ||
unset($result['__private_field_'.$privateField]); | ||
} | ||
} | ||
$subscriptions[] = [$subscriptionId, $fields, $result]; | ||
$subscriptionsCacheItem->set($subscriptions); | ||
$this->subscriptionsCache->save($subscriptionsCacheItem); | ||
|
||
$this->updateSubscriptionCollectionCacheData( | ||
$iri, | ||
$fields, | ||
$subscriptions, | ||
); | ||
|
||
return $subscriptionId; | ||
} | ||
|
||
public function getPushPayloads(object $object): array | ||
public function getPushPayloads(object $object, string $type): array | ||
{ | ||
if ('delete' === $type) { | ||
$payloads = $this->getDeletePushPayloads($object); | ||
} else { | ||
$payloads = $this->getCreatedOrUpdatedPayloads($object); | ||
} | ||
|
||
return $payloads; | ||
} | ||
|
||
/** | ||
* @return array<array> | ||
*/ | ||
private function getSubscriptionsFromIri(string $iri): array | ||
{ | ||
$subscriptionsCacheItem = $this->subscriptionsCache->getItem($this->encodeIriToCacheKey($iri)); | ||
|
||
if ($subscriptionsCacheItem->isHit()) { | ||
return $subscriptionsCacheItem->get(); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
private function removeItemFromSubscriptionCache(string $iri): void | ||
{ | ||
$cacheKey = $this->encodeIriToCacheKey($iri); | ||
if ($this->subscriptionsCache->hasItem($cacheKey)) { | ||
$this->subscriptionsCache->deleteItem($cacheKey); | ||
} | ||
} | ||
|
||
private function encodeIriToCacheKey(string $iri): string | ||
{ | ||
return str_replace('/', '_', $iri); | ||
} | ||
|
||
private function updateSubscriptionCollectionCacheData( | ||
?string $iri, | ||
array $fields, | ||
array $subscriptions, | ||
): void | ||
{ | ||
$subscriptionCollectionCacheItem = $this->subscriptionsCache->getItem( | ||
$this->encodeIriToCacheKey($this->getCollectionIri($iri)), | ||
); | ||
if ($subscriptionCollectionCacheItem->isHit()) { | ||
$collectionSubscriptions = $subscriptionCollectionCacheItem->get(); | ||
foreach ($collectionSubscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) { | ||
if ($subscriptionFields === $fields) { | ||
return; | ||
} | ||
} | ||
} | ||
$subscriptionCollectionCacheItem->set($subscriptions); | ||
$this->subscriptionsCache->save($subscriptionCollectionCacheItem); | ||
} | ||
|
||
private function getResourceId(mixed $privateField, object $previousObject): string | ||
{ | ||
$id = $previousObject->{'get' . ucfirst($privateField)}()->getId(); | ||
if ($id instanceof \Stringable) { | ||
return (string)$id; | ||
} | ||
return $id; | ||
} | ||
Comment on lines
+147
to
+154
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is usually using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the part that extract the value from an object filed that is used for restrictive updates to be limited to specific context (in my case to a specific owner_id). It also is extracting a defined field, not an ID, so the name for the method should be changed. |
||
|
||
private function getCollectionIri(string $iri): string | ||
{ | ||
return substr($iri, 0, strrpos($iri, '/')); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is too hackish we need to find a better way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm all ears, I haven't found one :) |
||
|
||
private function getCreatedOrUpdatedPayloads(object $object): array | ||
{ | ||
$iri = $this->iriConverter->getIriFromResource($object); | ||
$subscriptions = $this->getSubscriptionsFromIri($iri); | ||
if ($subscriptions === []) { | ||
// Get subscriptions from collection Iri | ||
$subscriptions = $this->getSubscriptionsFromIri($this->getCollectionIri($iri)); | ||
} | ||
|
||
$resourceClass = $this->getObjectClass($object); | ||
$resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass); | ||
$shortName = $resourceMetadata->getOperation()->getShortName(); | ||
|
||
$mercure = $resourceMetadata->getOperation()->getMercure() ?? false; | ||
$private = $mercure['private'] ?? false; | ||
$privateFieldsConfig = $mercure['private_fields'] ?? []; | ||
$privateFieldData = []; | ||
if ($private && $privateFieldsConfig) { | ||
foreach ($privateFieldsConfig as $privateField) { | ||
$privateFieldData['__private_field_'.$privateField] = $this->getResourceId($privateField, $object); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've seen this logic twice, could you use a function to remove a few lines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A Trait then? This logic is used in 2 different files. |
||
|
||
$payloads = []; | ||
foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) { | ||
if ($privateFieldData) { | ||
$fieldDiff = array_intersect_assoc($subscriptionFields, $privateFieldData); | ||
if ($fieldDiff !== $privateFieldData) { | ||
continue; | ||
} | ||
} | ||
$resolverContext = ['fields' => $subscriptionFields, 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => true]; | ||
/** @var Operation */ | ||
$operation = (new Subscription())->withName('update_subscription')->withShortName($shortName); | ||
$data = $this->normalizeProcessor->process($object, $operation, [], $resolverContext); | ||
|
||
|
@@ -92,26 +205,24 @@ public function getPushPayloads(object $object): array | |
$payloads[] = [$subscriptionId, $data]; | ||
} | ||
} | ||
|
||
return $payloads; | ||
} | ||
|
||
/** | ||
* @return array<array> | ||
*/ | ||
private function getSubscriptionsFromIri(string $iri): array | ||
private function getDeletePushPayloads(object $object): array | ||
{ | ||
$subscriptionsCacheItem = $this->subscriptionsCache->getItem($this->encodeIriToCacheKey($iri)); | ||
|
||
if ($subscriptionsCacheItem->isHit()) { | ||
return $subscriptionsCacheItem->get(); | ||
$iri = $object->id; | ||
$subscriptions = $this->getSubscriptionsFromIri($iri); | ||
if ($subscriptions === []) { | ||
// Get subscriptions from collection Iri | ||
$subscriptions = $this->getSubscriptionsFromIri($this->getCollectionIri($iri)); | ||
} | ||
|
||
return []; | ||
$payloads = []; | ||
foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) { | ||
$payloads[] = [$subscriptionId, ['type' => 'delete', 'payload' => $object]]; | ||
} | ||
$this->removeItemFromSubscriptionCache($iri); | ||
return $payloads; | ||
} | ||
|
||
private function encodeIriToCacheKey(string $iri): string | ||
{ | ||
return str_replace('/', '_', $iri); | ||
} | ||
psihius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,5 +22,5 @@ interface SubscriptionManagerInterface | |
{ | ||
public function retrieveSubscriptionId(array $context, ?array $result): ?string; | ||
|
||
public function getPushPayloads(object $object): array; | ||
public function getPushPayloads(object $object, string $type): array; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a public interface, the added argument should be optional to cover the BC layer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ final class PublishMercureUpdatesListener | |
'topics' => true, | ||
'data' => true, | ||
'private' => true, | ||
'private_fields' => true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are fields that are also read into the fields array based on witch the sha256 hash for the subscription id is generated. In the REST side of things this is called "Issuing restrictive updates": https://api-platform.com/docs/core/mercure/#dispatching-restrictive-updates-security-mode |
||
'id' => true, | ||
'type' => true, | ||
'retry' => true, | ||
|
@@ -281,11 +282,11 @@ private function evaluateTopics(array &$options, object $object): void | |
*/ | ||
private function getGraphQlSubscriptionUpdates(object $object, array $options, string $type): array | ||
{ | ||
if ('update' !== $type || !$this->graphQlSubscriptionManager || !$this->graphQlMercureSubscriptionIriGenerator) { | ||
if (!$this->graphQlSubscriptionManager || !$this->graphQlMercureSubscriptionIriGenerator) { | ||
return []; | ||
} | ||
|
||
$payloads = $this->graphQlSubscriptionManager->getPushPayloads($object); | ||
$payloads = $this->graphQlSubscriptionManager->getPushPayloads($object, $type); | ||
|
||
$updates = []; | ||
foreach ($payloads as [$subscriptionId, $data]) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When executing a subscription GraphQL query, the __typename key is not present in fields in the SubscriptionProcessor, but when you get fields in doctrine subscriber to publish updates, the __typename is present in the fields. That results in different sha256 hashes as subscription id, which breaks publishing.