Skip to content

Commit 59bf864

Browse files
committed
Fix issue field file, image alway return NULL
1 parent 283090c commit 59bf864

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

graphql_api.graphql.inc

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,22 @@ function graphql_api_graphql_api_info() {
3434
'name' => 'field_item_file',
3535
'fields' => function () {
3636
$schema = graphql_api();
37-
return $schema->getInterfaceType('file')->getFields() + [
37+
$file_type = $schema->getObjectType('file_file');
38+
if (module_exists('file_entity')) {
39+
$file_type = $schema->getInterfaceType('file');
40+
}
41+
return [
3842
'description' => [
3943
'type' => Type::string(),
4044
'description' => t('Alt')
4145
],
4246
'display' => [
4347
'type' => Type::string(),
4448
'description' => t('Display')
49+
],
50+
'file' => [
51+
'type' => $file_type,
52+
'description' => t('File')
4553
]
4654
];
4755
}
@@ -50,7 +58,11 @@ function graphql_api_graphql_api_info() {
5058
'name' => 'field_item_image',
5159
'fields' => function () {
5260
$schema = graphql_api();
53-
return $schema->getInterfaceType('file')->getFields() + [
61+
$file_type = $schema->getObjectType('file_file');
62+
if (module_exists('file_entity')) {
63+
$file_type = $schema->getInterfaceType('file');
64+
}
65+
return [
5466
'description' => [
5567
'type' => Type::string(),
5668
'description' => t('Alt')
@@ -66,6 +78,18 @@ function graphql_api_graphql_api_info() {
6678
'title' => [
6779
'type' => Type::string(),
6880
'description' => t('Title')
81+
],
82+
'width' => [
83+
'type' => Type::float(),
84+
'description' => t('Width')
85+
],
86+
'height' => [
87+
'type' => Type::float(),
88+
'description' => t('Height')
89+
],
90+
'file' => [
91+
'type' => $file_type,
92+
'description' => t('File')
6993
]
7094
];
7195
}

src/Schema.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,10 +614,18 @@ public function getFields($entity_type, $bundle = '') {
614614
$fields[$field] = [
615615
'type' => $fieldType,
616616
'description' => isset($field_info['description']) ? $field_info['description'] : '',
617-
'resolve' => function ($value, $args, $context, ResolveInfo $info) use ($entity_type, $bundle, $field) {
617+
'resolve' => function ($value, $args, $context, ResolveInfo $info) use ($entity_type, $bundle, $field, $fieldType) {
618618
$wrap = entity_metadata_wrapper($entity_type, $value);
619619
if ($wrap->__isset($field)) {
620620
$items = $wrap->{$field}->value();
621+
if (in_array($fieldType->name, ['field_item_image', 'field_item_file']) && !empty($items['fid'])) {
622+
$items['file'] = file_load($items['fid']);
623+
}
624+
if ($fieldType instanceof ListOfType) {
625+
foreach ($items as $index => $item) {
626+
$items[$index]['file'] = file_load($item['fid']);
627+
}
628+
}
621629
return $items;
622630
}
623631
return NULL;

0 commit comments

Comments
 (0)