Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Factory/ItemResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __invoke(?string $resourceClass = null, ?string $rootClass = nul
{
return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operationName) {
// Data already fetched and normalized (field or nested resource)
if (isset($source[$info->fieldName])) {
if (isset($source[$info->fieldName]) || (\is_array($source) && \array_key_exists($info->fieldName, $source) && null === $source[$info->fieldName] && null === $resourceClass)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wouldn't it better like this:

null === $source[$info->fieldName] ?? false

Why checking if $resourceClass is null?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi Alan,
If I'm not missing something, when resolver is used to fetch a property inside an entity, which is not a collection or sub entity, $resourceClass is null at this time.
And everything in $source[$info->fieldName] at this point is the current value of the property requested.
So, if $resourceClass is null, and so is $source[$info->fieldName], then we should not go further and return what's inside $source[$info->fieldName] right away.
If we go further, the resolver try to get an entity, but the property requested is not one, and we have an error telling Resource class cannot be determined.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK I think I understand the issue now, thanks for the explanation.
We should probably replace the isset($source[$info->fieldName]) with a call to array_key_exists then (without checking if the $resourceClass is null since it would handle all the cases), don't you think?
But to be sure, we need to have a correct Behat test.

@moduleon moduleon Sep 17, 2020

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Actually I'm not sure that we could erase the condition on $resourceClass.
Because $source[$info->fieldName] can be null for a reason, and notably because we must fetch a sub entity or a collection in a new query (with specific filters, pagination or order). In that case $source[$info->fieldName] is null and $resourceClass is not.
I will write the Behat feature for reproducing the error.

return $source[$info->fieldName];
}

Expand Down