How to pass a model to the broadcast, to the fields of which field resolvers have already been applied? #975
-
class ShotType extends GraphQLType
{
protected $attributes = [
'name' => 'Shot',
'model' => Shot::class,
];
public function fields(): array
{
return [
'id' => [
'type' => GraphQL::type('Int!'),
],
'user_id' => [
'type' => GraphQL::type('Int!'),
],
'disk' => [
'type' => GraphQL::type('String!'),
],
'path' => [
'type' => GraphQL::type('String!'),
],
'image' => [
'type' => GraphQL::type('String!'),
'selectable' => false,
'always' => ['disk', 'path'],
'args' => [
'sizes' => [
'type' => GraphQL::type('String'),
'description' => 'Example: resize:fit:80:80',
],
],
'resolve' => function ($root, $args) {
if (isset($args['sizes'])) {
return Resize::resize($root->image, $args['sizes']);
}
return $root->image;
}
],
];
}
} mutation ($image: Upload!) {
createShot(image: $image) {
id
image(sizes: "resize:fit:80:160")
}
} Inside the mutation in the resolve method, I call |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
How to you "get" |
Beta Was this translation helpful? Give feedback.
-
Found a temporary solution, but maybe not temporary. :) Created my MyGraphQLController. $data = Helpers::applyEach(
function (BaseOperationParams $baseOperationParams) use ($schemaName, $graphql): array {
$operationParams = new OperationParams($baseOperationParams);
return $graphql->execute($schemaName, $operationParams);
},
$operations
); added $mutationName = array_keys($data['data'])[0];
$subscriptions = array_keys($config->get('graphql.subscriptions'));
if (in_array($mutationName, $subscriptions)) {
$class = $config->get('graphql.subscriptions')[$mutationName];
broadcast(new $class($data['data'][$mutationName]))->toOthers();
} In graphql config added 'route' => [
'controller' => Trevio\Common\GraphQL\MyGraphQLController::class . '@query',
],
'subscriptions' => [
'createShot' => Trevio\Modules\Shots\GraphQL\Subscriptions\OnShotCreated::class
], OnShotCreated <?php
namespace Trevio\Modules\Shots\GraphQL\Subscriptions;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
class OnShotCreated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets;
public $model;
public function __construct($model)
{
$this->model = $model;
}
public function broadcastAs(): string
{
return 'shots.created';
}
public function broadcastOn(): Channel
{
return new Channel('public');
}
} Now the correct data is sent to the frontend in real time. But perhaps there is a more elegant solution. |
Beta Was this translation helpful? Give feedback.
-
Because you are fetching an Eloquent model: that particular code is unrelated to GraphQL and therefore your broadcaster receives Eloquent not GraphQL. It's not a good separation to have your GraphQL shapes bleed into other parts, there should be a clear boundary. I can take a guess it's about the As for your workaround: if you want to stick to that approach, it's essentially a "post"-step into the whole "graphql execution middleware" used by this package. The use of I think what would be more idiomatic if you have to stick to the GraphQL data structure, is to just override |
Beta Was this translation helpful? Give feedback.
Found a temporary solution, but maybe not temporary. :)
Created my MyGraphQLController.
In the query method after these lines
added