added apitems to gql api

This commit is contained in:
2021-06-22 16:40:24 +02:00
parent 59ac63f160
commit c8d0e2976e
3 changed files with 55 additions and 3 deletions
@@ -5,6 +5,15 @@ interface NodeInterface {
path: String!
}
interface MapItemInterface {
id: Int!
uuid: String!
bundle: String!
title: String!
path: String!
author: String
}
interface TextInterface {
id: Int!
uuid: String!
@@ -20,7 +29,7 @@ interface TextInterface {
auteurs: [Auteur]
}
type Textref implements NodeInterface & TextInterface{
type Textref implements NodeInterface & TextInterface & MapItemInterface{
id: Int!
uuid: String!
bundle: String!
@@ -45,7 +54,7 @@ type Textref implements NodeInterface & TextInterface{
notes: [Noteref]
}
type Textprod implements NodeInterface & TextInterface {
type Textprod implements NodeInterface & TextInterface & MapItemInterface {
id: Int!
uuid: String!
bundle: String!
@@ -65,7 +74,7 @@ type Textprod implements NodeInterface & TextInterface {
notes: [Noteprod]
}
type Creation implements NodeInterface {
type Creation implements NodeInterface & MapItemInterface {
id: Int!
uuid: String!
bundle: String!
@@ -2,6 +2,13 @@
# route(path: String!): NodeInterface
# }
extend type Query {
mapitems: [MapItemInterface]
}
extend type Query {
mapitem(id: Int!): MapItemInterface
}
extend type Query {
texts: [TextInterface]
}
@@ -31,6 +31,8 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase {
$this->addTexts($registry, $builder);
$this->addMapItems($registry, $builder);
$this->addTextsdepart($registry, $builder);
$this->addTextref($registry, $builder);
@@ -154,6 +156,40 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase {
);
}
protected function addMapItems(ResolverRegistryInterface $registry, ResolverBuilder $builder){
// Tell GraphQL how to resolve types of a common interface.
$registry->addTypeResolver('MapItemInterface', function ($value) {
switch ($value->bundle()) {
case 'texte': return 'Textref';
case 'texte_prod': return 'Textprod';
case 'creation': return 'Creation';
}
throw new Error('Could not resolve content type.');
});
$registry->addFieldResolver('Query', 'mapitems',
$builder->compose(
$builder->callback(function($parent, $arg){
$entity_storage = \Drupal::entityTypeManager()->getStorage('node');
$query = $entity_storage->getQuery()
->condition('type', ['texte', 'texte_prod', 'creation'], 'IN')
->accessCheck(TRUE);
$results = $query->execute();
return $results;
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('ids', $builder->fromParent())
)
);
$registry->addFieldResolver('Query', 'mapitem',
$builder->produce('entity_load')
->map('type', $builder->fromValue('node'))
->map('id', $builder->fromArgument('id'))
);
}
// _____ _ ___ _
// |_ _|____ _| |_ ___ | \ ___ _ __ __ _ _ _| |_
// | |/ -_) \ / _(_-< | |) / -_) '_ \/ _` | '_| _|