Skip to content

Commit 53059f2

Browse files
committed
Added generic url interface.
1 parent b2d87d5 commit 53059f2

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

src/Plugin/GraphQL/Fields/ExampleRoute.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @GraphQLField(
1414
* id = "example_url_route",
1515
* name = "route",
16-
* type = "Url",
16+
* type = "GenericUrl",
1717
* nullable = true,
1818
* arguments = {
1919
* "path" = "String"

src/Plugin/GraphQL/Fields/PageTitle.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* name = "pageTitle",
1919
* nullable = true,
2020
* multi = false,
21-
* types = {"Url"}
21+
* types = {"GenericUrl"}
2222
* )
2323
*/
2424
class PageTitle extends FieldPluginBase {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Drupal\graphql_example\Plugin\GraphQL\Interfaces;
4+
5+
use Drupal\Core\Url;
6+
use Drupal\graphql_core\Annotation\GraphQLInterface;
7+
use Drupal\graphql_core\GraphQL\InterfacePluginBase;
8+
9+
/**
10+
* Common interface for internal and external Urls.
11+
*
12+
* For simplicity reasons, this example does not utilize dependency injection.
13+
*
14+
* @GraphQLInterface(
15+
* id = "generic_url",
16+
* name = "GenericUrl"
17+
* )
18+
*/
19+
class GenericUrl extends InterfacePluginBase {
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function resolveType($object) {
25+
if ($object instanceof Url) {
26+
/** @var \Drupal\graphql_core\GraphQLSchemaManagerInterface $schemaManager */
27+
$schemaManager = \Drupal::service('graphql_core.schema_manager');
28+
if ($object->isExternal()) {
29+
return $schemaManager->findByName('ExternalUrl', [
30+
GRAPHQL_CORE_TYPE_PLUGIN,
31+
]);
32+
}
33+
else {
34+
return $schemaManager->findByName('Url', [
35+
GRAPHQL_CORE_TYPE_PLUGIN,
36+
]);
37+
}
38+
}
39+
}
40+
41+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Drupal\graphql_example\Plugin\GraphQL\Types;
4+
5+
6+
use Drupal\graphql_core\Annotation\GraphQLType;
7+
use Drupal\graphql_core\GraphQL\TypePluginBase;
8+
9+
/**
10+
* GraphQL Type for external urls.
11+
*
12+
* @GraphQLType(
13+
* id = "external_url",
14+
* name = "ExternalUrl",
15+
* interfaces = {"GenericUrl"}
16+
* )
17+
*/
18+
class ExternalUrl extends TypePluginBase {
19+
20+
}

0 commit comments

Comments
 (0)