Skip to content

Commit 976cc38

Browse files
LeStatVfubhy
authored andcommitted
Provide possibility to set Views field GraphQL response type (#23)
1 parent 7df966f commit 976cc38

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

src/Plugin/Deriver/Fields/ViewRowFieldDeriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ public function getDerivativeDefinitions($basePluginDefinition) {
3333
foreach ($display->getHandlers('field') as $name => $field) {
3434
$id = implode('-', [$viewId, $displayId, 'field', $name]);
3535
$alias = $rowPlugin->getFieldKeyAlias($name);
36+
$type = $rowPlugin->getFieldType($name);
3637

3738
$this->derivatives[$id] = [
3839
'id' => $id,
3940
'name' => $alias,
40-
'type' => 'String',
41+
'type' => $type,
4142
'parents' => [$display->getGraphQLRowName()],
4243
'view' => $viewId,
4344
'display' => $displayId,

src/Plugin/views/row/GraphQLFieldRow.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ class GraphQLFieldRow extends RowPluginBase {
3838
*/
3939
protected $rawOutputOptions = [];
4040

41+
/**
42+
* Stores an array of field GrpahQL type.
43+
*
44+
* @var array
45+
*/
46+
protected $typeOptions = [];
47+
4148
/**
4249
* {@inheritdoc}
4350
*/
@@ -51,6 +58,7 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o
5158
$this->replacementAliases = array_filter(array_map('trim', $aliases));
5259
// Prepare an array of raw output field options.
5360
$this->rawOutputOptions = static::extractFromOptionsArray('raw_output', $options);
61+
$this->typeOptions = static::extractFromOptionsArray('type', $options);
5462
}
5563
}
5664

@@ -72,7 +80,12 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
7280

7381
$form['field_options'] = [
7482
'#type' => 'table',
75-
'#header' => [$this->t('Field'), $this->t('Alias'), $this->t('Raw output')],
83+
'#header' => [
84+
$this->t('Field'),
85+
$this->t('Alias'),
86+
$this->t('Raw output'),
87+
$this->t('Type'),
88+
],
7689
'#empty' => $this->t('You have no fields. Add some to your view.'),
7790
'#tree' => TRUE,
7891
];
@@ -104,6 +117,17 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
104117
'#type' => 'checkbox',
105118
'#default_value' => isset($options[$id]['raw_output']) ? $options[$id]['raw_output'] : '',
106119
];
120+
121+
$form['field_options'][$id]['type'] = [
122+
'#type' => 'select',
123+
'#options' => [
124+
'String' => $this->t('String'),
125+
'Int' => $this->t('Int'),
126+
'Float' => $this->t('Float'),
127+
'Boolean' => $this->t('Boolean'),
128+
],
129+
'#default_value' => isset($options[$id]['type']) ? $options[$id]['type'] : 'String',
130+
];
107131
}
108132
}
109133
}
@@ -173,6 +197,23 @@ public function getFieldKeyAlias($id) {
173197
return $id;
174198
}
175199

200+
/**
201+
* Return a GraphQL field type, as set in the options form.
202+
*
203+
* @param string $id
204+
* The field id to lookup a type for.
205+
*
206+
* @return string
207+
* The matches user entered type, or String.
208+
*/
209+
public function getFieldType($id) {
210+
if (isset($this->typeOptions[$id])) {
211+
return $this->typeOptions[$id];
212+
}
213+
214+
return 'String';
215+
}
216+
176217
/**
177218
* Extracts a set of option values from a nested options array.
178219
*

tests/src/Kernel/ViewsTest.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ protected function defaultCacheContexts() {
2020
], parent::defaultCacheContexts());
2121
}
2222

23-
/**
24-
* {@inheritdoc}
25-
*/
26-
protected function defaultCacheTags() {
27-
return array_merge([
28-
'config:field.storage.node.field_tags',
29-
], parent::defaultCacheTags());
30-
}
31-
3223
/**
3324
* Test that the view returns both nodes.
3425
*/
@@ -47,7 +38,6 @@ public function testSimpleView() {
4738
],
4839
],
4940
], $this->defaultCacheMetaData()->addCacheTags([
50-
'config:views.view.graphql_bundle_test',
5141
'config:views.view.graphql_test',
5242
'node:1',
5343
'node:2',
@@ -91,7 +81,6 @@ public function testPagedView() {
9181
],
9282
],
9383
], $this->defaultCacheMetaData()->addCacheTags([
94-
'config:views.view.graphql_bundle_test',
9584
'config:views.view.graphql_test',
9685
'node:1',
9786
'node:2',
@@ -146,7 +135,6 @@ public function testSortedView() {
146135
],
147136
],
148137
], $this->defaultCacheMetaData()->addCacheTags([
149-
'config:views.view.graphql_bundle_test',
150138
'config:views.view.graphql_test',
151139
'node:1',
152140
'node:2',
@@ -184,7 +172,6 @@ public function testFilteredView() {
184172
],
185173
],
186174
], $this->defaultCacheMetaData()->addCacheTags([
187-
'config:views.view.graphql_bundle_test',
188175
'config:views.view.graphql_test',
189176
'node:1',
190177
'node:4',
@@ -218,7 +205,6 @@ public function testMultiValueFilteredView() {
218205
],
219206
],
220207
], $this->defaultCacheMetaData()->addCacheTags([
221-
'config:views.view.graphql_bundle_test',
222208
'config:views.view.graphql_test',
223209
'node:1',
224210
'node:2',
@@ -259,7 +245,6 @@ public function testComplexFilteredView() {
259245
],
260246
],
261247
], $this->defaultCacheMetaData()->addCacheTags([
262-
'config:views.view.graphql_bundle_test',
263248
'config:views.view.graphql_test',
264249
'node:1',
265250
'node:2',
@@ -289,7 +274,6 @@ public function testSingleValueBundleFilterView() {
289274
],
290275
], $this->defaultCacheMetaData()->addCacheTags([
291276
'config:views.view.graphql_bundle_test',
292-
'config:views.view.graphql_test',
293277
'node:1',
294278
'node_list',
295279
]));

0 commit comments

Comments
 (0)