-
-
Notifications
You must be signed in to change notification settings - Fork 901
feat(graphql): added support for graphql subscriptions to work for actions #6904
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
base: main
Are you sure you want to change the base?
Changes from all commits
46a0291
c9cefd8
e764f2f
e57e105
9e61e92
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 |
---|---|---|
|
@@ -47,43 +47,124 @@ | |
$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 ? ($operation->getMercure() ?? false) : 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); | ||
} | ||
} | ||
if ($operation instanceof Subscription && $operation->isCollection()) { | ||
$subscriptionId = $this->updateSubscriptionCollectionCacheData( | ||
$iri, | ||
$fields, | ||
); | ||
} else { | ||
$subscriptionId = $this->updateSubscriptionItemCacheData( | ||
$iri, | ||
$fields, | ||
$result, | ||
$private, | ||
$privateFields, | ||
$previousObject | ||
); | ||
} | ||
|
||
return $subscriptionId; | ||
} | ||
|
||
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)); | ||
$subscriptions = []; | ||
|
||
if ($subscriptionsCacheItem->isHit()) { | ||
$subscriptions = $subscriptionsCacheItem->get(); | ||
foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) { | ||
if ($subscriptionFields === $fields) { | ||
return $subscriptionId; | ||
} | ||
} | ||
return $subscriptionsCacheItem->get(); | ||
} | ||
|
||
$subscriptionId = $this->subscriptionIdentifierGenerator->generateSubscriptionIdentifier($fields); | ||
unset($result['clientSubscriptionId']); | ||
$subscriptions[] = [$subscriptionId, $fields, $result]; | ||
$subscriptionsCacheItem->set($subscriptions); | ||
$this->subscriptionsCache->save($subscriptionsCacheItem); | ||
return []; | ||
} | ||
|
||
return $subscriptionId; | ||
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); | ||
} | ||
|
||
public function getPushPayloads(object $object): array | ||
private function getResourceId(mixed $privateField, object $previousObject): string | ||
{ | ||
$id = $previousObject->{'get'.ucfirst($privateField)}()->getId(); | ||
if ($id instanceof \Stringable) { | ||
return (string) $id; | ||
} | ||
|
||
return $id; | ||
} | ||
|
||
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); | ||
// Add collection subscriptions | ||
$subscriptions = array_merge( | ||
$this->getSubscriptionsFromIri($this->getCollectionIri($iri)), | ||
$this->getSubscriptionsFromIri($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); | ||
$operation = (new Subscription())->withName('mercure_subscription')->withShortName($shortName); | ||
$data = $this->normalizeProcessor->process($object, $operation, [], $resolverContext); | ||
|
||
unset($data['clientSubscriptionId']); | ||
|
@@ -96,22 +177,80 @@ | |
return $payloads; | ||
} | ||
|
||
/** | ||
* @return array<array> | ||
*/ | ||
private function getSubscriptionsFromIri(string $iri): array | ||
private function getDeletePushPayloads(object $object): array | ||
{ | ||
$subscriptionsCacheItem = $this->subscriptionsCache->getItem($this->encodeIriToCacheKey($iri)); | ||
$iri = $object->id; | ||
$subscriptions = array_merge( | ||
$this->getSubscriptionsFromIri($iri), | ||
$this->getSubscriptionsFromIri($this->getCollectionIri($iri)) | ||
); | ||
|
||
$payloads = []; | ||
foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) { | ||
$payloads[] = [$subscriptionId, ['type' => 'delete', 'payload' => $object]]; | ||
} | ||
$this->removeItemFromSubscriptionCache($iri); | ||
|
||
return $payloads; | ||
} | ||
|
||
private function updateSubscriptionItemCacheData( | ||
string $iri, | ||
array $fields, | ||
?array $result, | ||
bool $private, | ||
array $privateFields, | ||
?object $previousObject, | ||
): string { | ||
$subscriptionsCacheItem = $this->subscriptionsCache->getItem($this->encodeIriToCacheKey($iri)); | ||
$subscriptions = []; | ||
if ($subscriptionsCacheItem->isHit()) { | ||
return $subscriptionsCacheItem->get(); | ||
/* | ||
* @var array<array{string, array<string, string|array>, array<string, string|array>}> | ||
*/ | ||
$subscriptions = $subscriptionsCacheItem->get(); | ||
foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) { | ||
if ($subscriptionFields === $fields) { | ||
return $subscriptionId; | ||
} | ||
} | ||
} | ||
|
||
return []; | ||
$subscriptionId = $this->subscriptionIdentifierGenerator->generateSubscriptionIdentifier($fields); | ||
unset($result['clientSubscriptionId']); | ||
if ($private && $privateFields && $previousObject) { | ||
foreach ($privateFields as $privateField) { | ||
unset($result['__private_field_'.$privateField]); | ||
} | ||
} | ||
$subscriptions[] = [$subscriptionId, $fields, $result]; | ||
$subscriptionsCacheItem->set($subscriptions); | ||
$this->subscriptionsCache->save($subscriptionsCacheItem); | ||
|
||
return $subscriptionId; | ||
} | ||
|
||
private function encodeIriToCacheKey(string $iri): string | ||
{ | ||
return str_replace('/', '_', $iri); | ||
private function updateSubscriptionCollectionCacheData( | ||
string $iri, | ||
array $fields, | ||
): string { | ||
$subscriptionCollectionCacheItem = $this->subscriptionsCache->getItem( | ||
$this->encodeIriToCacheKey($this->getCollectionIri($iri)), | ||
); | ||
$collectionSubscriptions = []; | ||
if ($subscriptionCollectionCacheItem->isHit()) { | ||
$collectionSubscriptions = $subscriptionCollectionCacheItem->get(); | ||
foreach ($collectionSubscriptions as [$subscriptionId, $subscriptionFields]) { | ||
if ($subscriptionFields === $fields) { | ||
return $subscriptionId; | ||
} | ||
} | ||
} | ||
$subscriptionId = $this->subscriptionIdentifierGenerator->generateSubscriptionIdentifier($fields + ['__collection' => true]); | ||
$collectionSubscriptions[] = [$subscriptionId, $fields]; | ||
$subscriptionCollectionCacheItem->set($collectionSubscriptions); | ||
$this->subscriptionsCache->save($subscriptionCollectionCacheItem); | ||
|
||
return $subscriptionId; | ||
} | ||
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 |
||
} |
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.