Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/GraphQl/Subscription/SubscriptionIdentifierGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@
public function generateSubscriptionIdentifier(array $fields): string
{
unset($fields['mercureUrl'], $fields['clientSubscriptionId']);
$fields = $this->removeTypename($fields);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed?

Copy link
Contributor Author

@psihius psihius Jan 17, 2025

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.


return hash('sha256', print_r($fields, true));
}

private function removeTypename(array $data): array
{
foreach ($data as $key => $value) {
if ($key === '__typename') {
unset($data[$key]);

Check warning on line 35 in src/GraphQl/Subscription/SubscriptionIdentifierGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionIdentifierGenerator.php#L35

Added line #L35 was not covered by tests
} elseif (is_array($value)) {
$data[$key] = $this->removeTypename($value);
}
}

return $data;
}
}
135 changes: 122 additions & 13 deletions src/GraphQl/Subscription/SubscriptionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,18 @@
$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)) {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 null === $iri || '' === $iri || [] === $iri.
Those 2 methods called above are part of a IdentifierTrait trait, means all call sides will need modification.

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

Check warning on line 59 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L58-L59

Added lines #L58 - L59 were not covered by tests
}
}
$subscriptionsCacheItem = $this->subscriptionsCache->getItem($this->encodeIriToCacheKey($iri));
$subscriptions = [];
if ($subscriptionsCacheItem->isHit()) {
Expand All @@ -63,26 +72,124 @@

$subscriptionId = $this->subscriptionIdentifierGenerator->generateSubscriptionIdentifier($fields);
unset($result['clientSubscriptionId']);
if ($private && $privateFields && $previousObject) {
foreach ($options['private_fields'] as $privateField) {
unset($result['__private_field_'.$privateField]);

Check warning on line 77 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L76-L77

Added lines #L76 - L77 were not covered by tests
}
}
$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);

Check warning on line 96 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L96

Added line #L96 was not covered by tests
} 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

Check warning on line 118 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L118

Added line #L118 was not covered by tests
{
$cacheKey = $this->encodeIriToCacheKey($iri);
if ($this->subscriptionsCache->hasItem($cacheKey)) {
$this->subscriptionsCache->deleteItem($cacheKey);

Check warning on line 122 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L120-L122

Added lines #L120 - L122 were not covered by tests
}
}

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;

Check warning on line 139 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L139

Added line #L139 was not covered by tests
}
}
}
$subscriptionCollectionCacheItem->set($subscriptions);
$this->subscriptionsCache->save($subscriptionCollectionCacheItem);
}

private function getResourceId(mixed $privateField, object $previousObject): string

Check warning on line 147 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L147

Added line #L147 was not covered by tests
{
$id = $previousObject->{'get' . ucfirst($privateField)}()->getId();
if ($id instanceof \Stringable) {
return (string)$id;

Check warning on line 151 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L149-L151

Added lines #L149 - L151 were not covered by tests
}
return $id;

Check warning on line 153 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L153

Added line #L153 was not covered by tests
}
Comment on lines +147 to +154
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is usually using identifiersExtractor could you explain why you need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
I am all for doing it in a better way :)


private function getCollectionIri(string $iri): string
{
return substr($iri, 0, strrpos($iri, '/'));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too hackish we need to find a better way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check warning on line 180 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L179-L180

Added lines #L179 - L180 were not covered by tests
}
}
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;

Check warning on line 189 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L187-L189

Added lines #L187 - L189 were not covered by tests
}
}
$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);

Expand All @@ -92,22 +199,24 @@
$payloads[] = [$subscriptionId, $data];
}
}

return $payloads;
}

/**
* @return array<array>
*/
private function getSubscriptionsFromIri(string $iri): array
private function getDeletePushPayloads(object $object): array

Check warning on line 205 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L205

Added line #L205 was not covered by tests
{
$subscriptionsCacheItem = $this->subscriptionsCache->getItem($this->encodeIriToCacheKey($iri));

if ($subscriptionsCacheItem->isHit()) {
return $subscriptionsCacheItem->get();
$iri = $object->id;
$subscriptions = $this->getSubscriptionsFromIri($iri);
if ($subscriptions === []) {

Check warning on line 209 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L207-L209

Added lines #L207 - L209 were not covered by tests
// Get subscriptions from collection Iri
$subscriptions = $this->getSubscriptionsFromIri($this->getCollectionIri($iri));

Check warning on line 211 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L211

Added line #L211 was not covered by tests
}

return [];
$payloads = [];
foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) {
$payloads[] = [$subscriptionId, ['type' => 'delete', 'payload' => $object]];

Check warning on line 216 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L214-L216

Added lines #L214 - L216 were not covered by tests
}
$this->removeItemFromSubscriptionCache($iri);
return $payloads;

Check warning on line 219 in src/GraphQl/Subscription/SubscriptionManager.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Subscription/SubscriptionManager.php#L218-L219

Added lines #L218 - L219 were not covered by tests
}

private function encodeIriToCacheKey(string $iri): string
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Subscription/SubscriptionManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ final class PublishMercureUpdatesListener
'topics' => true,
'data' => true,
'private' => true,
'private_fields' => true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are private_fields? couldn't find that in the specification.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
There you just generate a topic prefix, but since topics are not part of GraphQL subscription, we are incorporating value of the field so it generates unique subscription id that works only for that specific combo of private fields.

'id' => true,
'type' => true,
'retry' => true,
Expand Down Expand Up @@ -293,11 +294,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]) {
Expand Down
Loading