Graphql-laravel: Resolving additional select field

Created on 4 Dec 2018  路  4Comments  路  Source: rebing/graphql-laravel

Hi,

I'm using an anonymous function to resolve a field called 'trashed'. This works directly with Laravel's soft deletes.

'trashed' => [
    'type' => Type::boolean(),
    'description' => 'Whether the item has been trashed',
    'selectable' => false,
    'resolve' => function (\App\Models\AssetType $type) {
        return $type->trashed();
    }
]

The problem is, within my query I actually use $fields->getSelect(). I don't want trashed to be selectable but I do want $type->trashed() to be able to retrieve the deleted_at field from the database.

Is there any way I can make it so that when requesting the trashed field, deleted_at is added to the getSelect() array and retrieved as such?

I guess the only other way I can do it is simply resolve deleted_at as my field and return it as a string which will either be null or the deleted timestamp - I'd just rather have a boolean that does this

Thanks..

Most helpful comment

@rebing - it certainly did thank you. I've just applied it. Selectable flag still has to be set as false and the resolve method defined. Here's an example of how I've just used it for anyone searching -

'created' => [
    'type' => Type::string(),
    'always' => ['created_at'],
    'selectable' => false,
    'resolve' => function (Booking $booking) {
        return $booking->created_at->format('c');
    }
]

So with the above code, created_at is included as one of the select fields when using $fields->getSelect() in the query resolver.

All 4 comments

Try adding 'always' => ['deleted_at'] to trashed array

@rebing - now that could be very helpful! Thanks very much.

@chrispage1 Did it work?

@rebing - it certainly did thank you. I've just applied it. Selectable flag still has to be set as false and the resolve method defined. Here's an example of how I've just used it for anyone searching -

'created' => [
    'type' => Type::string(),
    'always' => ['created_at'],
    'selectable' => false,
    'resolve' => function (Booking $booking) {
        return $booking->created_at->format('c');
    }
]

So with the above code, created_at is included as one of the select fields when using $fields->getSelect() in the query resolver.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timothyvictor picture timothyvictor  路  3Comments

kirgy picture kirgy  路  8Comments

nozols picture nozols  路  8Comments

AdrianCarreno picture AdrianCarreno  路  5Comments

benjamin-ndc picture benjamin-ndc  路  3Comments