Comments (6)
Hey @morpheus7CS, so this message is saying that the created_at
, updated_at
or deleted_at
fields are returning a Illuminate\Support\Carbon
instance rather than a string
. There are 3 ways to handle this (directives, scalars, field resolver) but the easiest would be to create a custom directive like so:
// app/Http/GraphQL/Directives/DateTimeDirective.php
namespace App\Http\GraphQL\Directives;
use Carbon\Carbon;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\Contracts\FieldResolver;
use Nuwave\Lighthouse\Support\Traits\HandlesDirectives;
class DateTimeDirective implements FieldResolver
{
use HandlesDirectives;
/**
* Name of the directive.
*
* @return string
*/
public function name()
{
return 'dateTime';
}
/**
* Resolve the field directive.
*
* @param FieldValue $value
*
* @return FieldValue
*/
public function resolveField(FieldValue $value)
{
$field = $value->getFieldName();
return $value->setResolver(function ($root) use ($field) {
$carbon = data_get($root, $field);
// Format this however you want, but make sure it's a string
return $carbon instanceof Carbon ? $carbon->toIso8601String() : $carbon;
});
}
}
And use it in your schema:
type Project {
id: ID!
parent_id: Parent @belongsTo
name: String!
created_at: String @dateTime
updated_at: String @dateTime
deleted_at: String @dateTime
}
Just a side note, I'll be creating a Carbon
scalar and a @carbon(format: "YYYY-mm-dd")
directive that will come with Lighthouse for you to use soon.
from lighthouse.
I like the idea. The native @carbon
directive would be very useful. Will it be considered on next version?
"Just a side note, I'll be creating a Carbon scalar and a @carbon(format: "YYYY-mm-dd") directive that will come with Lighthouse for you to use soon."
from lighthouse.
I think your problem is created_at, etc... because those fields are Carbon instances.
I always create a Scalar Date to convert the carbon instace to a String representation.
from lighthouse.
This is my solution,
<?php
namespace App\GraphQL\Scalars;
use Nuwave\Lighthouse\Schema\Types\Scalars\DateTime as LightDateTime;
class DateTime extends LightDateTime
{
}
and for your definitions:
type User @model {
created_at: DateTime
updated_at: DateTime
}
from lighthouse.
Thanks @kikoseijo!! When I push the update with the scalar and directive I'll check a config option for non-null that way it won't introduce any breaking changes for anyone who's using that scalar.
from lighthouse.
Ooops!
I meant to point it as an example, its a file with no use otherwise,
I guess best resolution for scalars and directives its to use users defined directories first for overwrite purposes, then, try to locate in lighthouse defined folders, I think this way saves us time to have it copy pasted.
Most people won't look for things to use inside libraries.
BTW if you want to get rid of those and let people have them written... happy with that also.
from lighthouse.
Related Issues (20)
- Segmentation fault when handling large payloads HOT 8
- Requesting local scopes can pass parameters HOT 1
- New directive that mirrors functionality of `whereHas` for relations (not `@whereHasConditions`) HOT 1
- `make setup` failed on macOS
- Access context in `FieldMiddleware` HOT 3
- CanArgs is defined twice in schema-directives.graphql
- `extend scalar X` directives are lost
- @canFind is missing "model" argument in graphql definition HOT 1
- Allow to customize the unique key for `PaginatedModelsLoader`
- artisan lighthouse:union stub appears to be incorrect HOT 1
- Problem with subscription middleware HOT 2
- An error occurs in the SubscriptionRegistry when sending a subscription event via Subscription::broadcast with Laravel Octane (Swoole) HOT 5
- Laravel v11 support dependancy missing
- Enum Support for Morph Types HOT 1
- Mutation transaction seem not work HOT 4
- Failed to find class App\GraphQl\Queries\TodoQuery in namespaces [] for directive @paginate
- @whereNull and @whereNotNull No directive found!
- GraphQL subscription with Laravel Reverb HOT 3
- input.id in @can(find: ) HOT 1
- Throttle directive sets wrong decay time HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lighthouse.