Skip to content

Commit d31e1a1

Browse files
authored
Beta 6 compatibility (#13)
* Beta 6 compatibility * Deprecation fix. * Real deprecation fix. * Use graphql phpunit configuration. * Cache metadata fix. * .travis.yml fix * Views order fix.
1 parent 8ad4680 commit d31e1a1

16 files changed

Lines changed: 310 additions & 192 deletions

.travis.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@ env:
1616
matrix:
1717
- DRUPAL_CORE=8.3.x
1818
- DRUPAL_CORE=8.4.x
19+
- DRUPAL_CORE=8.5.x
1920

2021
matrix:
2122
# Don't wait for the allowed failures to build.
2223
fast_finish: true
2324
include:
2425
- php: 7.1
2526
env:
26-
- DRUPAL_CORE=8.4.x
27+
- DRUPAL_CORE=8.5.x
2728
# Only run code coverage on the latest php and drupal versions.
2829
- WITH_PHPDBG_COVERAGE=true
2930
allow_failures:
3031
# Allow the code coverage report to fail.
3132
- php: 7.1
3233
env:
33-
- DRUPAL_CORE=8.4.x
34+
- DRUPAL_CORE=8.5.x
3435
# Only run code coverage on the latest php and drupal versions.
3536
- WITH_PHPDBG_COVERAGE=true
3637

@@ -79,6 +80,10 @@ install:
7980
# Reference the module in the build site.
8081
- ln -s $TRAVIS_BUILD_DIR $DRUPAL_BUILD_DIR/modules/graphql_views
8182

83+
# Copy the customized phpunit configuration file to the core directory so
84+
# the relative paths are correct.
85+
- cp $DRUPAL_BUILD_DIR/modules/graphql/phpunit.xml.dist $DRUPAL_BUILD_DIR/core/phpunit.xml
86+
8287
# When running with phpdbg we need to replace all code occurrences that check
8388
# for 'cli' with 'phpdbg'. Some files might be write protected, hence the
8489
# fallback.
@@ -88,17 +93,17 @@ install:
8893

8994
# Bring in the module dependencies without requiring a merge plugin. The
9095
# require also triggers a full 'composer install'.
91-
- composer --working-dir=$DRUPAL_BUILD_DIR require youshido/graphql:~1.5
96+
- composer --working-dir=$DRUPAL_BUILD_DIR require webonyx/graphql-php:^0.11.5
9297

9398
script:
9499
# Run the unit tests using phpdbg if the environment variable is 'true'.
95100
- if [[ "$WITH_PHPDBG_COVERAGE" == "true" ]];
96-
then phpdbg -qrr $DRUPAL_BUILD_DIR/vendor/bin/phpunit --configuration $DRUPAL_BUILD_DIR/core/phpunit.xml.dist --coverage-clover $TRAVIS_BUILD_DIR/coverage.xml $TRAVIS_BUILD_DIR;
101+
then phpdbg -qrr $DRUPAL_BUILD_DIR/vendor/bin/phpunit --configuration $DRUPAL_BUILD_DIR/core/phpunit.xml --coverage-clover $TRAVIS_BUILD_DIR/coverage.xml $TRAVIS_BUILD_DIR;
97102
fi
98103

99104
# Run the unit tests with standard php otherwise.
100105
- if [[ "$WITH_PHPDBG_COVERAGE" != "true" ]];
101-
then $DRUPAL_BUILD_DIR/vendor/bin/phpunit --configuration $DRUPAL_BUILD_DIR/core/phpunit.xml.dist $TRAVIS_BUILD_DIR;
106+
then $DRUPAL_BUILD_DIR/vendor/bin/phpunit --configuration $DRUPAL_BUILD_DIR/core/phpunit.xml $TRAVIS_BUILD_DIR;
102107
fi
103108

104109
after_success:

src/Plugin/Deriver/Enums/ViewSortByDeriver.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ public function getDerivativeDefinitions($basePluginDefinition) {
2424

2525
/** @var \Drupal\graphql_views\Plugin\views\display\GraphQL $display */
2626
$display = $this->getViewDisplay($view, $displayId);
27-
$sorts = array_map(function ($sort) {
28-
return [
29-
'name' => $sort['id'],
27+
$sorts = array_filter($display->getOption('sorts') ?: [], function ($sort) {
28+
return $sort['exposed'];
29+
});
30+
$sorts = array_reduce($sorts, function ($carry, $sort) {
31+
$carry[strtoupper($sort['id'])] = [
3032
'value' => $sort['id'],
3133
'description' => $sort['expose']['label'],
3234
];
33-
}, array_filter($display->getOption('sorts') ?: [], function($sort) {
34-
return $sort['exposed'];
35-
}));
35+
return $carry;
36+
}, []);
3637

3738
if (!empty($sorts)) {
3839
$id = implode('-', [$viewId, $displayId, 'view']);

src/Plugin/Deriver/Fields/ViewDeriver.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,13 @@ protected function getContextualArguments(array $arguments, $id) {
9191
* The sort arguments if any exposed sorts are available.
9292
*/
9393
protected function getSortArguments(DisplayPluginInterface $display, $id) {
94-
return $display->getOption('sorts') ? [
94+
$sorts = array_filter($display->getOption('sorts') ?: [], function ($sort) {
95+
return $sort['exposed'];
96+
});
97+
return $sorts ? [
9598
'sortDirection' => [
9699
'type' => 'ViewSortDirection',
97-
'default' => 'ASC',
100+
'default' => 'asc',
98101
],
99102
'sortBy' => [
100103
'type' => StringHelper::camelCase($id, 'sort', 'by'),
@@ -171,7 +174,7 @@ protected function getTypes(array $arguments) {
171174
$types = array_merge($types, [StringHelper::camelCase($argument['entity_type'])]);
172175
}
173176
else {
174-
$types = array_merge($types, array_map(function($bundle) use ($argument) {
177+
$types = array_merge($types, array_map(function ($bundle) use ($argument) {
175178
return StringHelper::camelCase($argument['entity_type'], $bundle);
176179
}, array_keys($argument['bundles'])));
177180
}

src/Plugin/Deriver/ViewDeriverBase.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,11 @@ protected function getViewStyle(ViewEntityInterface $view, $displayId) {
251251
protected function getCacheMetadataDefinition(ViewEntityInterface $view) {
252252
return [
253253
'schema_cache_tags' => $view->getCacheTags(),
254-
'schema_cache_contexts' => $view->getCacheContexts(),
255254
'schema_cache_max_age' => $view->getCacheMaxAge(),
255+
'response_cache_contexts' => array_merge($view->getCacheContexts(), [
256+
// TODO: check if they are really always there.
257+
'languages:language_content',
258+
]),
256259
];
257260
}
258261

src/Plugin/GraphQL/Enums/ViewSortDirection.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@
99
* id = "view_sort_direction",
1010
* name = "ViewSortDirection",
1111
* provider = "views",
12-
* values = {
13-
* "ASC" = "asc",
14-
* "DESC" = "desc"
15-
* }
1612
* )
1713
*/
1814
class ViewSortDirection extends EnumPluginBase {
1915

16+
/**
17+
* {@inheritdoc}
18+
*/
19+
protected function buildEnumValues($definition) {
20+
return [
21+
'ASC' => [
22+
'value' => 'ASC',
23+
'description' => 'Sort in ascending order.',
24+
],
25+
'DESC' => [
26+
'value' => 'DESC',
27+
'description' => 'Sort in descending order.',
28+
],
29+
];
30+
}
31+
2032
}

src/Plugin/GraphQL/Fields/View.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
use Drupal\Core\Entity\EntityInterface;
77
use Drupal\Core\Entity\EntityTypeManagerInterface;
88
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9+
use Drupal\graphql\GraphQL\Execution\ResolveContext;
910
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
11+
use GraphQL\Type\Definition\ResolveInfo;
1012
use Symfony\Component\DependencyInjection\ContainerInterface;
11-
use Youshido\GraphQL\Execution\ResolveInfo;
1213

1314
/**
1415
* Expose views as root fields.
@@ -59,7 +60,7 @@ public static function create(ContainerInterface $container, array $configuratio
5960
/**
6061
* {@inheritdoc}
6162
*/
62-
public function resolveValues($value, array $args, ResolveInfo $info) {
63+
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
6364
$storage = $this->entityTypeManager->getStorage('view');
6465
$definition = $this->getPluginDefinition();
6566

@@ -91,14 +92,24 @@ public function resolveValues($value, array $args, ResolveInfo $info) {
9192
$executable->setCurrentPage($args['page']);
9293
}
9394

94-
yield $executable->render($definition['display']);
95+
$result = $executable->render($definition['display']);
96+
/** @var \Drupal\Core\Cache\CacheableMetadata $cache */
97+
if ($cache = $result['cache']) {
98+
$cache->setCacheContexts(
99+
array_filter($cache->getCacheContexts(), function ($context) {
100+
// Don't emit the url cache contexts.
101+
return $context !== 'url' && strpos($context, 'url.') !== 0;
102+
})
103+
);
104+
}
105+
yield $result;
95106
}
96107
}
97108

98109
/**
99110
* {@inheritdoc}
100111
*/
101-
protected function getCacheDependencies(array $result, $value, array $args, ResolveInfo $info) {
112+
protected function getCacheDependencies(array $result, $value, array $args, ResolveContext $context, ResolveInfo $info) {
102113
return array_map(function ($item) {
103114
return $item['cache'];
104115
}, $result);

src/Plugin/GraphQL/Fields/ViewResultCount.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Drupal\graphql_views\Plugin\GraphQL\Fields;
44

5+
use Drupal\graphql\GraphQL\Execution\ResolveContext;
56
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
67
use Drupal\views\ViewExecutable;
7-
use Youshido\GraphQL\Execution\ResolveInfo;
8+
use GraphQL\Type\Definition\ResolveInfo;
89

910
/**
1011
* Expose result count of a view.
@@ -23,7 +24,7 @@ class ViewResultCount extends FieldPluginBase {
2324
/**
2425
* {@inheritdoc}
2526
*/
26-
public function resolveValues($value, array $args, ResolveInfo $info) {
27+
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
2728
if (isset($value['view']) && $value['view'] instanceof ViewExecutable) {
2829
yield intval($value['view']->total_rows);
2930
}

src/Plugin/GraphQL/Fields/ViewResultList.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Drupal\graphql_views\Plugin\GraphQL\Fields;
44

5+
use Drupal\graphql\GraphQL\Execution\ResolveContext;
56
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
6-
use Youshido\GraphQL\Execution\ResolveInfo;
7+
use GraphQL\Type\Definition\ResolveInfo;
78

89
/**
910
* Expose results of a view.
@@ -21,7 +22,7 @@ class ViewResultList extends FieldPluginBase {
2122
/**
2223
* {@inheritdoc}
2324
*/
24-
protected function resolveValues($value, array $args, ResolveInfo $info) {
25+
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
2526
if (isset($value['rows'])) {
2627
foreach ($value['rows'] as $row) {
2728
yield $row;

src/Plugin/GraphQL/Fields/ViewRowField.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Drupal\graphql_views\Plugin\GraphQL\Fields;
44

5+
use Drupal\graphql\GraphQL\Execution\ResolveContext;
56
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
6-
use Youshido\GraphQL\Execution\ResolveInfo;
7+
use GraphQL\Type\Definition\ResolveInfo;
78

89
/**
910
* Expose view row fields for configured fieldable views.
@@ -20,10 +21,11 @@ class ViewRowField extends FieldPluginBase {
2021
/**
2122
* {@inheritdoc}
2223
*/
23-
public function resolveValues($value, array $args, ResolveInfo $info) {
24+
public function resolveValues($value, array $args, ResolveContext $contex, ResolveInfo $info) {
2425
$definition = $this->getPluginDefinition();
2526
if (isset($value[$definition['field']])) {
2627
yield $value[$definition['field']];
2728
}
2829
}
30+
2931
}

tests/queries/contextual.gql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ query ($test2NodeId: String!) {
2626
}
2727
}
2828

29-
node2:nodeById (id: "1", language: en) {
29+
node2:nodeById (id: "1", language: EN) {
3030
... on Node {
3131
graphqlTestContextualNodeView {
3232
results {
@@ -35,7 +35,7 @@ query ($test2NodeId: String!) {
3535
}
3636
}
3737
}
38-
node3:nodeById (id: "1", language: en) {
38+
node3:nodeById (id: "1", language: EN) {
3939
... on Node {
4040
graphqlTestContextualNodeView (contextualFilter: {nid: "X"}) {
4141
results {
@@ -44,7 +44,7 @@ query ($test2NodeId: String!) {
4444
}
4545
}
4646
}
47-
node4:nodeById (id: "1", language: en) {
47+
node4:nodeById (id: "1", language: EN) {
4848
... on NodeTest {
4949
graphqlTestContextualNodeView {
5050
results {
@@ -53,7 +53,7 @@ query ($test2NodeId: String!) {
5353
}
5454
}
5555
}
56-
node5:nodeById (id: "1", language: en) {
56+
node5:nodeById (id: "1", language: EN) {
5757
... on NodeTest {
5858
graphqlTestContextualNodeView (contextualFilter: {nid: "X"}) {
5959
results {
@@ -76,7 +76,7 @@ query ($test2NodeId: String!) {
7676
}
7777
}
7878

79-
nodetest2:nodeById (id: "1", language: en) {
79+
nodetest2:nodeById (id: "1", language: EN) {
8080
... on NodeTest {
8181
graphqlTestContextualNodetestView {
8282
results {
@@ -86,7 +86,7 @@ query ($test2NodeId: String!) {
8686
}
8787
}
8888

89-
nodetest3:nodeById (id: "1", language: en) {
89+
nodetest3:nodeById (id: "1", language: EN) {
9090
... on NodeTest {
9191
graphqlTestContextualNodetestView (contextualFilter: {nid: "X"}) {
9292
results {
@@ -109,7 +109,7 @@ query ($test2NodeId: String!) {
109109
}
110110
}
111111

112-
node_and_nodetest2:nodeById (id: $test2NodeId, language: en) {
112+
node_and_nodetest2:nodeById (id: $test2NodeId, language: EN) {
113113
... on Node {
114114
graphqlTestContextualNodeAndNodetestView {
115115
results {
@@ -118,7 +118,7 @@ query ($test2NodeId: String!) {
118118
}
119119
}
120120
}
121-
node_and_nodetest3:nodeById (id: $test2NodeId, language: en) {
121+
node_and_nodetest3:nodeById (id: $test2NodeId, language: EN) {
122122
... on Node {
123123
graphqlTestContextualNodeAndNodetestView (contextualFilter: {nid: "X", nid_1: "X"}) {
124124
results {
@@ -128,7 +128,7 @@ query ($test2NodeId: String!) {
128128
}
129129
}
130130

131-
node_and_nodetest4:nodeById (id: "1", language: en) {
131+
node_and_nodetest4:nodeById (id: "1", language: EN) {
132132
... on NodeTest {
133133
graphqlTestContextualNodeAndNodetestView {
134134
results {
@@ -138,7 +138,7 @@ query ($test2NodeId: String!) {
138138
}
139139
}
140140

141-
node_and_nodetest5:nodeById (id: "1", language: en) {
141+
node_and_nodetest5:nodeById (id: "1", language: EN) {
142142
... on NodeTest {
143143
graphqlTestContextualNodeAndNodetestView (contextualFilter: {nid: "X", nid_1: "X"}) {
144144
results {

0 commit comments

Comments
 (0)