- πΊπΈUnited States mglaman WI, USA
I don't like adding non-standard parameters for
include
.\Drupal\jsonapi\Context\FieldResolver::resolveInternalIncludePath
is what throws this error.$public_field_name = $path_parts[0]; $internal_field_name = $resource_type->getInternalName($public_field_name); $relatable_resource_types = $resource_type->getRelatableResourceTypesByField($public_field_name); if (empty($relatable_resource_types)) { $message = "`$public_field_name` is not a valid relationship field name."; if (!empty(($possible = implode(', ', array_keys($resource_type->getRelatableResourceTypes()))))) { $message .= " Possible values: $possible."; } throw new CacheableBadRequestHttpException($cacheability, $message); }
The exploded paths is the include parameter
// $include_parameter: 'one.two.three, one.two.four'. $include_paths = array_map('trim', explode(',', $include_parameter)); // $exploded_paths: [['one', 'two', 'three'], ['one', 'two', 'four']]. $exploded_paths = array_map(function ($include_path) { return array_map('trim', explode('.', $include_path)); }, $include_paths);
So we could follow the patch's approach of iterating over each data, getting the resource type and checking if the include applies to the resource object. Then run
\Drupal\jsonapi\JsonApiResource\Data::deduplicate
at the end. The main difference if performing the check ourselves versus ignoring the exception.Maybe we should provide our own include resolver which wraps
\Drupal\jsonapi\IncludeResolver
and supports missing relationships, only for jsonapi_resources resources. But I feel like there should still be some kind of error if a relationship is requested that doesn't exist, at all. Maybe.Instead of checking the resource types in the data, we could just check the resource types defined by the route? However,
\Drupal\jsonapi_resources\Unstable\ResourceResponseFactory::create
is not aware of the resource types used. π‘ but\Drupal\jsonapi_resources\Unstable\DocumentExtractor::extractResourceObject
does this, which it also could!/** @var \Drupal\jsonapi\ResourceType\ResourceType[] $route_resource_types */ $route_resource_types = $request->attributes->get('resource_types');
- Merge request !11Support include parameter with mixed resource object types β (Merged) created by mglaman
- πΊπΈUnited States mglaman WI, USA
Okay, I came back to this later than I wanted to, but I have an MR opened with a failing test.
- Status changed to Needs review
about 2 months ago 4:09pm 2 May 2024 - πΊπΈUnited States mglaman WI, USA
Okay, I'm happy with the MR and approach.
- Status changed to RTBC
about 2 months ago 4:39pm 2 May 2024 - πΊπΈUnited States japerry KVUO
mglaman β credited japerry β .
- πΊπΈUnited States mglaman WI, USA
Saving issue credits. japerry helped review
-
mglaman β
committed e6ba9a4c on 8.x-1.x
Issue #3172884 by mglaman, lisotton, zenphp, japerry: Include parameter...
-
mglaman β
committed e6ba9a4c on 8.x-1.x
- Status changed to Fixed
about 2 months ago 4:41pm 2 May 2024 Automatically closed - issue fixed for 2 weeks with no activity.