graphql: added TextInterface and Texts query

This commit is contained in:
2021-02-25 17:32:22 +01:00
parent d1722dfb27
commit a49edd4c48
3 changed files with 79 additions and 8 deletions
@@ -5,39 +5,58 @@ interface NodeInterface {
path: String! path: String!
} }
type Textref implements NodeInterface { interface TextInterface {
id: Int! id: Int!
uuid: String! uuid: String!
bundle: String!
title: String! title: String!
path: String! path: String!
author: String author: String
texte: String texte: String
text_en_rebond: [Textref]
text_produits: [Textprod]
images: [Image] images: [Image]
videos: [VideoLink] videos: [VideoLink]
tags: [Taxoterm]
familles: [Taxoterm] familles: [Taxoterm]
}
type Textref implements NodeInterface & TextInterface{
id: Int!
uuid: String!
bundle: String!
title: String!
path: String!
author: String
texte: String
images: [Image]
videos: [VideoLink]
familles: [Taxoterm]
#
text_produits: [Textprod]
text_en_rebond: [Textref]
tags: [Taxoterm]
# field_degres_detrangement # field_degres_detrangement
# notes: [Note] # notes: [Note]
# field_titre
# auteur: Auteur # auteur: Auteur
# edition: Edition # edition: Edition
# Date: String # Date: String
} }
type Textprod implements NodeInterface { type Textprod implements NodeInterface & TextInterface {
id: Int! id: Int!
uuid: String! uuid: String!
bundle: String!
title: String! title: String!
path: String! path: String!
author: String author: String
texte: String texte: String
text_de_depart: [Textref]
images: [Image] images: [Image]
videos: [VideoLink] videos: [VideoLink]
tagsprod: [Taxoterm]
familles: [Taxoterm] familles: [Taxoterm]
#
text_de_depart: [Textref]
tagsprod: [Taxoterm]
# notes: [Note] # notes: [Note]
# field_titre
# auteur: Auteur # auteur: Auteur
# edition: Edition # edition: Edition
# date: Date # date: Date
@@ -2,6 +2,10 @@
# route(path: String!): NodeInterface # route(path: String!): NodeInterface
# } # }
extend type Query {
texts: [TextInterface]
}
extend type Query { extend type Query {
textsdepart: [Textref] textsdepart: [Textref]
} }
@@ -29,6 +29,8 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase {
$this->addRouteResolver($registry, $builder); $this->addRouteResolver($registry, $builder);
$this->addTexts($registry, $builder);
$this->addTextsdepart($registry, $builder); $this->addTextsdepart($registry, $builder);
$this->addTextref($registry, $builder); $this->addTextref($registry, $builder);
@@ -90,7 +92,7 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase {
if ($class === 'Node') { if ($class === 'Node') {
switch ($value->bundle()) { switch ($value->bundle()) {
case 'texte': return 'Textref'; case 'texte': return 'Textref';
case 'texte_prod': return 'TextProd'; case 'texte_prod': return 'Textprod';
} }
} }
throw new Error('Could not resolve content type.'); throw new Error('Could not resolve content type.');
@@ -105,6 +107,33 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase {
)); ));
} }
protected function addTexts(ResolverRegistryInterface $registry, ResolverBuilder $builder){
// Tell GraphQL how to resolve types of a common interface.
$registry->addTypeResolver('TextInterface', function ($value) {
switch ($value->bundle()) {
case 'texte': return 'Textref';
case 'texte_prod': return 'Textprod';
}
throw new Error('Could not resolve content type.');
});
$registry->addFieldResolver('Query', 'texts',
$builder->compose(
$builder->callback(function($parent, $arg){
$entity_storage = \Drupal::entityTypeManager()->getStorage('node');
$query = $entity_storage->getQuery()
->condition('type', ['texte', 'texte_prod'], 'IN')
->accessCheck(TRUE);
$results = $query->execute();
return $results;
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('ids', $builder->fromParent())
)
);
}
// _____ _ ___ _ // _____ _ ___ _
// |_ _|____ _| |_ ___ | \ ___ _ __ __ _ _ _| |_ // |_ _|____ _| |_ ___ | \ ___ _ __ __ _ _ _| |_
@@ -178,6 +207,12 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase {
->map('entity', $builder->fromParent()) ->map('entity', $builder->fromParent())
)); ));
$registry->addFieldResolver('Textref', 'bundle',
$builder->compose(
$builder->produce('entity_bundle')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Textref', 'texte', $registry->addFieldResolver('Textref', 'texte',
$builder->produce('property_path') $builder->produce('property_path')
->map('type', $builder->fromValue('entity:node')) ->map('type', $builder->fromValue('entity:node'))
@@ -220,6 +255,13 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase {
->map('path', $builder->fromValue('field_video')) ->map('path', $builder->fromValue('field_video'))
); );
$registry->addFieldResolver('Textref', 'titre_ref',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_titre'))
);
$registry->addFieldResolver('Textref', 'tags', $registry->addFieldResolver('Textref', 'tags',
$builder->produce('entity_reference') $builder->produce('entity_reference')
->map('entity', $builder->fromParent()) ->map('entity', $builder->fromParent())
@@ -280,6 +322,12 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase {
->map('entity', $builder->fromParent()) ->map('entity', $builder->fromParent())
)); ));
$registry->addFieldResolver('Textprod', 'bundle',
$builder->compose(
$builder->produce('entity_bundle')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Textprod', 'texte', $registry->addFieldResolver('Textprod', 'texte',
$builder->produce('property_path') $builder->produce('property_path')
->map('type', $builder->fromValue('entity:node')) ->map('type', $builder->fromValue('entity:node'))