extend type Query {
ad(id: ID @eq): Ad @find(model: "App\\Models\\Ad")
ads: [Ad!]! @paginate(type: "paginator" model: "App\\Models\\Ad")
}
union AdDetail @union(resolveType: "App\\GraphQL\\Unions\\AdDetail@resolveType") =
RoomForRentAd | RoomWantedAd
type Ad{
id: ID!
author: User! @belongsTo
reference_id: String!
ad_type: AdType! @rename(attribute: "type")
title: String!
description: String!
detail: AdDetail
}
App\GraphQL\Unions\AdDetail.php
<?php
namespace App\GraphQL\Unions;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Support\Facades\Log;
use Nuwave\Lighthouse\Schema\TypeRegistry;
class AdDetail
{
protected $typeRegistry;
public function __construct(TypeRegistry $typeRegistry)
{
$this->typeRegistry = $typeRegistry;
}
public function resolveType($rootValue, GraphQLContext $context, ResolveInfo $resolveInfo): Type
{
Log::info("test", [$rootValue, $context, $resolveInfo]);
return $this->typeRegistry->get(class_basename($rootValue));
}
}
Method 'resolveType' doesn't log anything, leaving it empty doesn't raise any exceptions also exiting inside it doesn't cause any problems too... The 'constructor' is being called and logging data though
Query
query {
ad(id: "008c5402-fdf1-35c5-9a01-77bb6dbd9b41") {
author {
name
email
}
reference_id
ad_type
title
description
detail {
__typename
... on RoomForRentAd {
rent
}
... on RoomWantedAd {
people_male
}
}
}
}
Response
{
"data": {
"ad": {
"author": {
"name": "Mrs. Velva Howell II",
"email": null
},
"reference_id": "NV976",
"ad_type": "ROOM_WANTED_AD",
"title": "Quia laboriosam et et quam qui.",
"description": "Eos provident a nemo molestias rerum autem quos. Hic et quo vero modi. Sint earum quis ea earum corporis accusantium veritatis. ",
"detail": null
}
}
}
Custom resolver for Union directive not executing
Schema
App\GraphQL\Unions\AdDetail.php
Method 'resolveType' doesn't log anything, leaving it empty doesn't raise any exceptions also exiting inside it doesn't cause any problems too... The 'constructor' is being called and logging data though
Query
Response
Lighthouse Version: 3.7 Laravel Version: 5.8