Skip to content

Commit 1b312e9

Browse files
author
Bertrand Dunogier
committed
Completed date formatting through the "formatted" leaf
Example: { content { ... on DateAndTimeFieldValue { formatted(format: "d/m/Y") } } }
1 parent b5562c1 commit 1b312e9

3 files changed

Lines changed: 18 additions & 16 deletions

File tree

GraphQL/Resolver/ContentResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class ContentResolver
5050
'ezrichtext' => 'RichTextFieldValue',
5151
'ezstring' => 'TextLineFieldValue',
5252
'ezobjectrelationlist' => 'RelationListFieldValue',
53+
'ezdate' => 'DateFieldValue',
54+
'ezdatetime' => 'DateAndTimeFieldValue',
5355
];
5456

5557
public function __construct(ContentService $contentService, SearchService $searchService, ContentTypeService $contentTypeService, TypeResolver $typeResolver)

GraphQL/Resolver/DateResolver.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@
66

77
class DateResolver
88
{
9-
public function resolveDateToFormat(DateTime $date, Argument $args)
9+
public function resolveDateToFormat(string $format, DateTime $date = null)
1010
{
11-
if (isset($args['constant'])) {
12-
$format = $args['constant'];
13-
} else if (isset($args['format'])) {
14-
$format = $args['format'];
15-
} else {
16-
$format = DateTime::RFC850;
11+
if ($date === null) {
12+
return $date;
1713
}
18-
1914
return $date->format($format);
2015
}
2116
}

Resources/config/graphql/Field.types.yml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,25 @@ DateFieldValue:
159159
config:
160160
interfaces: [FieldValue]
161161
fields:
162-
format:
162+
text:
163163
type: "String"
164-
description: "String representation of the value"
165-
resolve: "@=value"
164+
description: "Unix timestamp"
165+
resolve: "@=value.value ? value.value.getTimestamp() : null"
166+
formatted:
167+
type: "String"
168+
description: "Formatted date"
169+
# @todo: refactor with a datetime resolver
170+
resolve: "@=resolver('DateTimeFormat', [args['format'], value.value])"
171+
args:
172+
format:
173+
type: String
174+
description: "A format string compatible with PHP's date() function"
166175

167176
DateAndTimeFieldValue:
168177
type: object
178+
inherits: ["DateFieldValue"]
169179
config:
170180
interfaces: [FieldValue]
171-
fields:
172-
text:
173-
type: "String"
174-
description: "String representation of the value"
175-
resolve: "@=value"
176181

177182
EmailAddressFieldValue:
178183
type: object

0 commit comments

Comments
 (0)