Skip to content

Instantly share code, notes, and snippets.

@Kingdutch
Created June 17, 2021 15:46
Show Gist options
  • Select an option

  • Save Kingdutch/5990c24a2734c5fb01e26cf444f44c18 to your computer and use it in GitHub Desktop.

Select an option

Save Kingdutch/5990c24a2734c5fb01e26cf444f44c18 to your computer and use it in GitHub Desktop.
Patch to add interface to GraphQL Type decorator
From 27a47d83645fccc52fefe98c2e246e07ac78e84b Mon Sep 17 00:00:00 2001
From: Alexander Varwijk <alexander@getopensocial.com>
Date: Thu, 17 Jun 2021 17:40:10 +0200
Subject: [PATCH] Provide DecoratableTypeResolverInterface
The interface allows alternative but compatible decoratable type
resolvers to be created. It also provides an upgrade path for projects
that were already using the pattern.
---
src/GraphQL/DecoratableTypeResolver.php | 34 ++--------
.../DecoratableTypeResolverInterface.php | 62 +++++++++++++++++++
2 files changed, 67 insertions(+), 29 deletions(-)
create mode 100644 src/GraphQL/DecoratableTypeResolverInterface.php
diff --git a/src/GraphQL/DecoratableTypeResolver.php b/src/GraphQL/DecoratableTypeResolver.php
index 5fd2c89..80b8da4 100644
--- a/src/GraphQL/DecoratableTypeResolver.php
+++ b/src/GraphQL/DecoratableTypeResolver.php
@@ -3,36 +3,12 @@
namespace Drupal\graphql\GraphQL;
/**
- * A decoratable type resolver to resolve GraphQL interfaces to concrete types.
- *
- * Type resolvers should extend this class so that they can be chained in
- * schema extensions plugins.
- *
- * For example with the following class defined.
- * ```php
- * class ConcreteTypeResolver extends DecoratableTypeResolver {
- *
- * protected function resolve($object) : ?string {
- * return $object instanceof MyType ? 'MyType' : NULL;
- * }
- * }
- * ```
- *
- * A schema extension would call:
- * ```php
- * $registry->addTypeResolver(
- * 'InterfaceType',
- * new ConcreteTypeResolver($registry->getTypeResolver('InterfaceType'))
- * );
- * ```
- *
- * TypeResolvers should not extend other type resolvers but always extend this
- * class directly. Classes will be called in the reverse order of being added
- * (classes added last will be called first).
+ * A base class for decoratable type resolvers.
*
* @package Drupal\graphql\GraphQL
+ * @see \Drupal\graphql\GraphQL\DecoratableTypeResolverInterface
*/
-abstract class DecoratableTypeResolver {
+abstract class DecoratableTypeResolver implements DecoratableTypeResolverInterface {
/**
* The previous type resolver that was set in the chain.
@@ -44,10 +20,10 @@ abstract class DecoratableTypeResolver {
/**
* Create a new decoratable type resolver.
*
- * @param \Drupal\graphql\GraphQL\DecoratableTypeResolver|null $resolver
+ * @param \Drupal\graphql\GraphQL\DecoratableTypeResolverInterface|null $resolver
* The previous type resolver if any.
*/
- public function __construct(self $resolver = NULL) {
+ public function __construct(DecoratableTypeResolverInterface $resolver = NULL) {
$this->decorated = $resolver;
}
diff --git a/src/GraphQL/DecoratableTypeResolverInterface.php b/src/GraphQL/DecoratableTypeResolverInterface.php
new file mode 100644
index 0000000..4e4ccf3
--- /dev/null
+++ b/src/GraphQL/DecoratableTypeResolverInterface.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace Drupal\graphql\GraphQL;
+
+/**
+ * A decoratable type resolver to resolve GraphQL interfaces to concrete types.
+ *
+ * Type resolvers should extend this class so that they can be chained in
+ * schema extensions plugins.
+ *
+ * For example with the following class defined.
+ * ```php
+ * class ConcreteTypeResolver extends DecoratableTypeResolver {
+ *
+ * protected function resolve($object) : ?string {
+ * return $object instanceof MyType ? 'MyType' : NULL;
+ * }
+ * }
+ * ```
+ *
+ * A schema extension would call:
+ * ```php
+ * $registry->addTypeResolver(
+ * 'InterfaceType',
+ * new ConcreteTypeResolver($registry->getTypeResolver('InterfaceType'))
+ * );
+ * ```
+ *
+ * TypeResolvers should not extend other type resolvers but always extend this
+ * class directly. Classes will be called in the reverse order of being added
+ * (classes added last will be called first).
+ *
+ * @package Drupal\social_graphql\GraphQL
+ */
+interface DecoratableTypeResolverInterface {
+
+ /**
+ * Create a new decoratable type resolver.
+ *
+ * @param \Drupal\graphql\GraphQL\DecoratableTypeResolverInterface|null $resolver
+ * The previous type resolver if any.
+ */
+ public function __construct(?self $resolver);
+
+ /**
+ * Allows this type resolver to be called by the GraphQL library.
+ *
+ * Takes care of chaining the various type resolvers together and invokes the
+ * `resolve` method for each concrete implementation in the chain.
+ *
+ * @param mixed $object
+ * The object to resolve to a concrete type.
+ *
+ * @return string
+ * The resolved GraphQL type name.
+ *
+ * @throws \RuntimeException
+ * When a type was passed for which no type resolver exists in the chain.
+ */
+ public function __invoke($object) : string;
+
+}
--
2.30.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment