diff --git a/_config/graphql.yml b/_config/graphql.yml index ce700c6..9467df7 100644 --- a/_config/graphql.yml +++ b/_config/graphql.yml @@ -1,13 +1,8 @@ --- Name: linkgraphql --- -SilverStripe\GraphQL\Manager: +SilverStripe\GraphQL\Schema\Schema: schemas: admin: - types: - LinkType: SilverStripe\Link\GraphQL\LinkTypeType - LinkDescription: SilverStripe\Link\GraphQL\LinkDescriptionType - - queries: - readLinkTypes: SilverStripe\Link\GraphQL\LinkTypeQuery - readLinkDescription: SilverStripe\Link\GraphQL\LinkDescriptionQuery + src: + link: silverstripe/link:_graphql diff --git a/_graphql/queries.yml b/_graphql/queries.yml new file mode 100644 index 0000000..703ab00 --- /dev/null +++ b/_graphql/queries.yml @@ -0,0 +1,6 @@ +'readLinkDescription(dataStr: String!)': + type: LinkDescription + resolver: ['SilverStripe\Link\GraphQL\LinkDescriptionResolver', 'resolve'] +'readLinkTypes(keys: [ID])': + type: '[LinkType]' + resolver: ['SilverStripe\Link\GraphQL\LinkTypeResolver', 'resolve'] diff --git a/_graphql/types.yml b/_graphql/types.yml new file mode 100644 index 0000000..70525b1 --- /dev/null +++ b/_graphql/types.yml @@ -0,0 +1,11 @@ +LinkDescription: + description: Given some Link data, computes the matching description + fields: + description: String + +LinkType: + description: Describe a Type of Link that can be managed by a LinkField + fields: + key: ID + handlerName: String! + title: String! diff --git a/src/GraphQL/LinkDescriptionQuery.php b/src/GraphQL/LinkDescriptionQuery.php deleted file mode 100644 index 7759c62..0000000 --- a/src/GraphQL/LinkDescriptionQuery.php +++ /dev/null @@ -1,55 +0,0 @@ - 'readLinkDescription' - ]; - } - - public function type() - { - return $this->manager->getType('LinkDescription'); - } - - public function args() - { - return [ - 'dataStr' => [ - 'type' => GraphqlType::nonNull(GraphqlType::string()), - ], - ]; - } - - public function resolve($object, array $args, $context, ResolveInfo $info) - { - $data = json_decode($args['dataStr'], true); - if (json_last_error() !== JSON_ERROR_NONE) { - throw new \InvalidArgumentException('data must be a valid JSON string'); - } - - if (empty($data['typeKey'])) { - return ['description' => '']; - } - - $type = Registry::singleton()->byKey($data['typeKey']); - if (empty($type)) { - return ['description' => '']; - } - - - return ['description' => $type->generateLinkDescription($data)]; - } -} diff --git a/src/GraphQL/LinkDescriptionResolver.php b/src/GraphQL/LinkDescriptionResolver.php new file mode 100644 index 0000000..076190b --- /dev/null +++ b/src/GraphQL/LinkDescriptionResolver.php @@ -0,0 +1,29 @@ + '']; + } + + $type = Registry::singleton()->byKey($data['typeKey']); + if (empty($type)) { + return ['description' => '']; + } + + + return ['description' => $type->generateLinkDescription($data)]; + } +} diff --git a/src/GraphQL/LinkDescriptionType.php b/src/GraphQL/LinkDescriptionType.php deleted file mode 100644 index 3dcb8f9..0000000 --- a/src/GraphQL/LinkDescriptionType.php +++ /dev/null @@ -1,35 +0,0 @@ - 'LinkDescription', - 'description' => 'Given some Link data, computes the matching description', - ]; - } - - /** - * @return array - */ - public function fields() - { - return [ - 'description' => [ - 'type' => GraphqlType::string(), - ], - ]; - } -} diff --git a/src/GraphQL/LinkTypeQuery.php b/src/GraphQL/LinkTypeResolver.php similarity index 51% rename from src/GraphQL/LinkTypeQuery.php rename to src/GraphQL/LinkTypeResolver.php index c9616fc..6f89588 100644 --- a/src/GraphQL/LinkTypeQuery.php +++ b/src/GraphQL/LinkTypeResolver.php @@ -1,40 +1,14 @@ 'readLinkTypes' - ]; - } - - public function type() - { - return GraphqlType::listOf($this->manager->getType('LinkType')); - } - - public function args() - { - return [ - 'keys' => [ - 'type' => GraphqlType::listOf(GraphqlType::id()), - ], - ]; - } - - public function resolve($object, array $args, $context, ResolveInfo $info) + public static function resolve($object, array $args, $context, ResolveInfo $info) { if (isset($args['keys']) && !is_array($args['keys'])) { throw new \InvalidArgumentException('If `keys` is provdied, it must be an array'); diff --git a/src/GraphQL/LinkTypeType.php b/src/GraphQL/LinkTypeType.php deleted file mode 100644 index 3dcf5fc..0000000 --- a/src/GraphQL/LinkTypeType.php +++ /dev/null @@ -1,41 +0,0 @@ - 'LinkType', - 'description' => 'Describe a Type of Link that can be managed by a LinkField', - ]; - } - - /** - * @return array - */ - public function fields() - { - return [ - 'key' => [ - 'type' => GraphqlType::nonNull(GraphqlType::id()), - ], - 'handlerName' => [ - 'type' => GraphqlType::nonNull(GraphqlType::string()), - ], - 'title' => [ - 'type' => GraphqlType::nonNull(GraphqlType::string()), - ], - ]; - } -} diff --git a/src/Models/Link.php b/src/Models/Link.php index 0df7b25..ee5a048 100644 --- a/src/Models/Link.php +++ b/src/Models/Link.php @@ -102,7 +102,7 @@ function setData($data): JsonData return $jsonData; } - public function jsonSerialize() + public function jsonSerialize(): mixed { $typeKey = Registry::singleton()->keyByClassName(static::class); if (empty($typeKey)) {