-
-
Notifications
You must be signed in to change notification settings - Fork 912
refactor(metadata): no default graphql operation name #5348
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
Changes from all commits
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
use ApiPlatform\Metadata\GraphQl\Mutation; | ||
use ApiPlatform\Metadata\GraphQl\Operation; | ||
use ApiPlatform\Metadata\GraphQl\Query; | ||
use ApiPlatform\Metadata\GraphQl\QueryCollection; | ||
use ApiPlatform\Metadata\GraphQl\Subscription; | ||
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; | ||
use ApiPlatform\State\Pagination\Pagination; | ||
|
@@ -73,10 +74,12 @@ public function getResourceObjectType(?string $resourceClass, ResourceMetadataCo | |
$shortName .= 'Payload'; | ||
} | ||
|
||
if ('item_query' === $operationName || 'collection_query' === $operationName) { | ||
if (!$operation->getResolver() && ($operation instanceof Query || $operation instanceof QueryCollection)) { | ||
// Test if the collection/item has different groups | ||
if ($resourceMetadataCollection->getOperation($operation instanceof CollectionOperationInterface ? 'item_query' : 'collection_query')->getNormalizationContext() !== $operation->getNormalizationContext()) { | ||
$shortName .= $operation instanceof CollectionOperationInterface ? 'Collection' : 'Item'; | ||
$isCollection = $operation instanceof CollectionOperationInterface; | ||
$otherOperation = $resourceMetadataCollection->getGraphQlOperation(null, !$isCollection); | ||
if ($otherOperation->getNormalizationContext() !== $operation->getNormalizationContext()) { | ||
$shortName .= $isCollection ? 'Collection' : 'Item'; | ||
} | ||
} | ||
|
||
|
@@ -104,27 +107,25 @@ public function getResourceObjectType(?string $resourceClass, ResourceMetadataCo | |
'name' => $shortName, | ||
'description' => $operation->getDescription(), | ||
'resolveField' => $this->defaultFieldResolver, | ||
'fields' => function () use ($resourceClass, $operation, $operationName, $resourceMetadataCollection, $input, $wrapData, $depth, $ioMetadata) { | ||
'fields' => function () use ($resourceClass, $operation, $resourceMetadataCollection, $input, $wrapData, $depth, $ioMetadata) { | ||
if ($wrapData) { | ||
$queryNormalizationContext = $this->getQueryOperation($resourceMetadataCollection)?->getNormalizationContext() ?? []; | ||
|
||
try { | ||
$mutationNormalizationContext = $operation instanceof Mutation || $operation instanceof Subscription ? ($resourceMetadataCollection->getOperation($operationName)->getNormalizationContext() ?? []) : []; | ||
$mutationNormalizationContext = $operation instanceof Mutation || $operation instanceof Subscription ? ($operation->getNormalizationContext() ?? []) : []; | ||
} catch (OperationNotFoundException) { | ||
$mutationNormalizationContext = []; | ||
} | ||
// Use a new type for the wrapped object only if there is a specific normalization context for the mutation or the subscription. | ||
// If not, use the query type in order to ensure the client cache could be used. | ||
$useWrappedType = $queryNormalizationContext !== $mutationNormalizationContext; | ||
|
||
$wrappedOperationName = $operationName; | ||
$wrappedOperation = $operation; | ||
|
||
if (!$useWrappedType) { | ||
$wrappedOperationName = $operation instanceof Query ? $operationName : 'item_query'; | ||
$wrappedOperation = $resourceMetadataCollection->getGraphQlOperation(null, forceCollection: $operation instanceof Query); | ||
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. Why using a named argument here and not elsewhere? 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 want to start using them I can revert for the codestyle 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. Personally I think it's better like this indeed. But you should uniformize the call. |
||
} | ||
|
||
$wrappedOperation = $resourceMetadataCollection->getOperation($wrappedOperationName); | ||
|
||
$fields = [ | ||
lcfirst($wrappedOperation->getShortName()) => $this->getResourceObjectType($resourceClass, $resourceMetadataCollection, $wrappedOperation instanceof Operation ? $wrappedOperation : null, $input, true, $depth), | ||
]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,10 +82,7 @@ public function convertType(Type $type, bool $input, Operation $rootOperation, s | |
} catch (ResourceClassNotFoundException|OperationNotFoundException) { | ||
} | ||
/** @var Query $enumOperation */ | ||
$enumOperation = (new Query()) | ||
->withClass($type->getClassName()) | ||
->withShortName($operation?->getShortName() ?? (new \ReflectionClass($type->getClassName()))->getShortName()) | ||
->withDescription($operation?->getDescription()); | ||
$enumOperation = new Query(name: 'item_query', class: $type->getClassName(), shortName: $operation?->getShortName() ?? (new \ReflectionClass($type->getClassName()))->getShortName(), description: $operation?->getDescription()); | ||
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.
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. we do need a name |
||
|
||
return $this->typeBuilder->getEnumType($enumOperation); | ||
} | ||
|
@@ -170,16 +167,14 @@ private function getResourceType(Type $type, bool $input, Operation $rootOperati | |
|
||
// We're retrieving the type of a property which is a relation to the root resource. | ||
if ($resourceClass !== $rootResource && $rootOperation instanceof Query) { | ||
$operationName = $isCollection ? 'collection_query' : 'item_query'; | ||
} | ||
|
||
try { | ||
$operation = $resourceMetadataCollection->getOperation($operationName); | ||
} catch (OperationNotFoundException) { | ||
$operation = $resourceMetadataCollection->getOperation($isCollection ? 'collection_query' : 'item_query'); | ||
} | ||
if (!$operation instanceof Operation) { | ||
throw new OperationNotFoundException(); | ||
$operation = $resourceMetadataCollection->getGraphQlOperation(null, $isCollection); | ||
} else { | ||
try { | ||
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName); | ||
} catch (OperationNotFoundException) { | ||
// try to fetch the default operation in case we didn't found the named one | ||
$operation = $resourceMetadataCollection->getGraphQlOperation(null, $isCollection); | ||
} | ||
} | ||
|
||
return $this->typeBuilder->getResourceObjectType($resourceClass, $resourceMetadataCollection, $operation, $input, false, $depth); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,7 +115,7 @@ class: $class, | |
fetchPartial: $fetchPartial, | ||
forceEager: $forceEager, | ||
priority: $priority, | ||
name: $name ?: 'item_query', | ||
name: $name, | ||
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. Why? 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. because we want to remove logic from our operations 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, why not. |
||
provider: $provider, | ||
processor: $processor, | ||
stateOptions: $stateOptions, | ||
|
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 not using
instanceof
here?