-
-
Notifications
You must be signed in to change notification settings - Fork 922
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
psihius
wants to merge
9
commits into
api-platform:main
Choose a base branch
from
psihius:feature/graphql-subscription-for-all-operations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c0a74fe
feat(graphql): added support for graphql subscriptions to work for al…
psihius 8d892b2
feat(graphql): adjusted some of the formatting and method placement t…
psihius 2769f7e
feat(graphql): reworked how collection subscriptions work, added opt …
psihius 9f53f74
feat(graphql): fixing mercure subscription failing tests
psihius 06180ae
feat(graphql): fixed code style and a bug in collection cache handling
psihius 8db9702
feat(graphql): Fixed not putting all 3 items into the subscription cache
psihius 9536ec9
feat(graphql): Collection cache keys are now segmented by private fie…
psihius cea37ca
feat(graphql): Handle multiple subscriptions per resource and issue u…
psihius 1aaf997
feat(graphql): Update getResourceId to handle scalar values
psihius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ | |||||
use ApiPlatform\GraphQl\State\Provider\NoopProvider; | ||||||
use ApiPlatform\Metadata\ApiProperty; | ||||||
use ApiPlatform\Metadata\GraphQl\Query; | ||||||
use ApiPlatform\Metadata\GraphQl\QueryCollection; | ||||||
use ApiPlatform\Metadata\IdentifiersExtractorInterface; | ||||||
use ApiPlatform\Metadata\IriConverterInterface; | ||||||
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||||||
|
@@ -26,6 +27,7 @@ | |||||
use ApiPlatform\Metadata\Util\ClassInfoTrait; | ||||||
use ApiPlatform\Serializer\CacheKeyTrait; | ||||||
use ApiPlatform\Serializer\ItemNormalizer as BaseItemNormalizer; | ||||||
use Doctrine\Common\Collections\Collection; | ||||||
use Psr\Log\LoggerInterface; | ||||||
use Psr\Log\NullLogger; | ||||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||||||
|
@@ -106,6 +108,11 @@ public function normalize(mixed $object, ?string $format = null, array $context | |||||
$data[self::ITEM_IDENTIFIERS_KEY] = $this->identifiersExtractor->getIdentifiersFromItem($object, $context['operation'] ?? null); | ||||||
} | ||||||
|
||||||
if (isset($context['graphql_operation_name']) && 'mercure_subscription' === $context['graphql_operation_name'] && \is_object($object) && isset($data['id']) && !isset($data['_id'])) { | ||||||
$data['_id'] = $data['id']; | ||||||
$data['id'] = $this->iriConverter->getIriFromResource($object); | ||||||
} | ||||||
|
||||||
return $data; | ||||||
} | ||||||
|
||||||
|
@@ -120,10 +127,46 @@ protected function normalizeCollectionOfRelations(ApiProperty $propertyMetadata, | |||||
return [...$attributeValue]; | ||||||
} | ||||||
|
||||||
// Handle relationships for mercure subscriptions | ||||||
if ($operation instanceof QueryCollection && 'mercure_subscription' === $context['graphql_operation_name'] && $attributeValue instanceof Collection && !$attributeValue->isEmpty()) { | ||||||
$relationContext = $context; | ||||||
// Grab collection attributes | ||||||
$relationContext['attributes'] = $context['attributes']['collection']; | ||||||
// Iterate over the collection and normalize each item | ||||||
$data['collection'] = $attributeValue | ||||||
->map(fn ($item) => $this->normalize($item, $format, $relationContext)) | ||||||
// Convert the collection to an array | ||||||
->toArray(); | ||||||
|
||||||
// Handle pagination if it's enabled in the query | ||||||
return $this->addPagination($attributeValue, $data, $context); | ||||||
} | ||||||
|
||||||
// to-many are handled directly by the GraphQL resolver | ||||||
return []; | ||||||
} | ||||||
|
||||||
private function addPagination(Collection $collection, array $data, array $context): 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.
Suggested change
Use the value of |
||||||
{ | ||||||
if ($context['attributes']['paginationInfo'] ?? false) { | ||||||
$data['paginationInfo'] = []; | ||||||
if (\array_key_exists('hasNextPage', $context['attributes']['paginationInfo'])) { | ||||||
$data['paginationInfo']['hasNextPage'] = $collection->count() > ($context['pagination']['itemsPerPage'] ?? 10); | ||||||
} | ||||||
if (\array_key_exists('itemsPerPage', $context['attributes']['paginationInfo'])) { | ||||||
$data['paginationInfo']['itemsPerPage'] = $context['pagination']['itemsPerPage'] ?? 10; | ||||||
} | ||||||
if (\array_key_exists('lastPage', $context['attributes']['paginationInfo'])) { | ||||||
$data['paginationInfo']['lastPage'] = (int) ceil($collection->count() / ($context['pagination']['itemsPerPage'] ?? 10)); | ||||||
} | ||||||
if (\array_key_exists('totalCount', $context['attributes']['paginationInfo'])) { | ||||||
$data['paginationInfo']['totalCount'] = $collection->count(); | ||||||
} | ||||||
} | ||||||
|
||||||
return $data; | ||||||
} | ||||||
|
||||||
/** | ||||||
* {@inheritdoc} | ||||||
*/ | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We tend to avoid doing things like this in our code base, prefer a simple foreach, especially if you transform the iterable to an array just fill your
$data['collection']
it'll be one less complexity loop.