added pathauto, refactored article to load gql directly from path, the vuejs route and drupal route can finally match, HALLELUYA git status

This commit is contained in:
2020-12-24 17:11:51 +01:00
parent ee97e675e3
commit 78155f83b8
12 changed files with 220 additions and 181 deletions

View File

@@ -1,6 +1,10 @@
scalar Violation
type Materiau {
interface NodeInterface {
id: Int!
}
type Materiau implements NodeInterface {
id: Int!
uuid: String!
title: String!
@@ -23,7 +27,7 @@ type Materiau {
samples: [Sample]
}
type Article {
type Article implements NodeInterface {
id: Int!
uuid: String!
title: String!

View File

@@ -1,3 +1,7 @@
extend type Query {
route(path: String!): NodeInterface
}
extend type Query {
materiau(id: Int!): Materiau
}

View File

@@ -3,7 +3,10 @@
namespace Drupal\materio_graphql\Plugin\GraphQL\Schema;
use Drupal\graphql\Plugin\GraphQL\Schema\ComposableSchema;
use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistry;
// use Drupal\graphql\Plugin\GraphQL\Schema\SdlSchemaPluginBase;
use GraphQL\Error\Error;
/**
* @Schema(
* id = "materio",
@@ -13,4 +16,32 @@ use Drupal\graphql\Plugin\GraphQL\Schema\ComposableSchema;
*/
class MaterioSchema extends ComposableSchema {
// /**
// * {@inheritdoc}
// */
// public function getResolverRegistry() {
// $builder = new ResolverBuilder();
// $registry = new ResolverRegistry();
//
// // Tell GraphQL how to resolve types of a common interface.
// $registry->addTypeResolver('NodeInterface', function ($value) {
// if ($value instanceof NodeInterface) {
// switch ($value->bundle()) {
// case 'article': return 'Article';
// case 'materiau': return 'Materiau';
// }
// }
// throw new Error('Could not resolve content type.');
// });
//
// $registry->addFieldResolver('Query', 'route', $builder->compose(
// $builder->produce('route_load')
// ->map('path', $builder->fromArgument('path')),
// $builder->produce('route_entity')
// ->map('url', $builder->fromParent())
// ));
//
// return $registry;
// }
}

View File

@@ -8,6 +8,9 @@ use Drupal\graphql\GraphQL\Response\ResponseInterface;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;
use Drupal\materio_graphql\GraphQL\Response\MaterioResponse;
use Drupal\Core\Url;
use GraphQL\Error\Error;
/**
* @SchemaExtension(
* id = "materio_extension",
@@ -24,6 +27,30 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
public function registerResolvers(ResolverRegistryInterface $registry) {
$builder = new ResolverBuilder();
// Tell GraphQL how to resolve types of a common interface.
$registry->addTypeResolver('NodeInterface', function ($value) {
$path = explode('\\', get_class($value));
$class = array_pop($path);
if ($class === 'Node') {
switch ($value->bundle()) {
case 'article': return 'Article';
case 'materiau': return 'Materiau';
}
}
throw new Error('Could not resolve content type.');
});
$registry->addFieldResolver('Query', 'route',
$builder->compose(
$builder->produce('route_load')
->map('path', $builder->fromArgument('path')),
$builder->produce('route_entity')
->map('url', $builder->fromParent())
));
$this->addMateriau($registry, $builder);
$this->addSearchResult($registry, $builder);
@@ -577,12 +604,12 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
protected function addLink(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Link', 'url',
$builder->callback(function ($parent, $args) {
return $parent[0]['uri'];
return isset($parent[0]) ? $parent[0]['uri'] : null;
})
);
$registry->addFieldResolver('Link', 'title',
$builder->callback(function ($parent, $args) {
return $parent[0]['title'];
return isset($parent[0]) ? $parent[0]['title'] : null;
})
);
}
@@ -594,12 +621,12 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
protected function addDate(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Date', 'start',
$builder->callback(function ($parent, $args) {
return $parent[0]['value'];
return isset($parent[0]) ? $parent[0]['value'] : null;
})
);
$registry->addFieldResolver('Date', 'end',
$builder->callback(function ($parent, $args) {
return $parent[0]['end_value'];
return isset($parent[0]) ? $parent[0]['end_value'] : null;
})
);
}