<?php

namespace Drupal\ouatt_graphql\Plugin\GraphQL\SchemaExtension;

use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
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 Drupal\Node\Entity\NodeType;

use GraphQL\Error\Error;

/**
 * @SchemaExtension(
 *   id = "ouatt_extension",
 *   name = "Ou Atterrir extension",
 *   description = "Ou Atterrir extension.",
 *   schema = "ouatt"
 * )
 */
class OuattSchemaExtension extends SdlSchemaExtensionPluginBase {

  /**
   * {@inheritdoc}
   */
  public function registerResolvers(ResolverRegistryInterface $registry) {
    $builder = new ResolverBuilder();

    $this->addRouteResolver($registry, $builder);
    // 
    $this->addConcernements($registry, $builder);
    //
    $this->addEntitesIntegres($registry, $builder);
    // 
    $this->addEntites($registry, $builder);
    // 
    $this->addSource($registry, $builder);
    // 
    $this->addBesoin($registry, $builder);
    // 
    $this->addReponse($registry, $builder);
    // 
    $this->addDoleance($registry, $builder);
    // 
    $this->addGroupeInterets($registry, $builder);
    // 
    $this->addReceptionTraitement($registry, $builder);
    // 
    $this->addMiseEnoeuvreDecision($registry, $builder);
    // 
    $this->addReceptionsApplications($registry, $builder);
    // 
    $this->addStatic($registry, $builder);
    // 
    $this->addGroup($registry, $builder);
    // 
    $this->addUser($registry, $builder);
    //
    $this->addFilefield($registry, $builder);
    //
    $this->addFile($registry, $builder);
    //
    $this->addDate($registry, $builder);
    //
    $this->addVideolink($registry, $builder);
    //
    // $this->addImagefield($registry, $builder);
    //
    $this->addImage($registry, $builder);
    //
    $this->addTaxoterm($registry, $builder);
    //
    $this->addLink($registry, $builder);

    $this->addEntityDefinition($registry, $builder);
    $this->addFieldDef($registry, $builder);

  }

  //  ___          _
  // | _ \___ _  _| |_ ___
  // |   / _ \ || |  _/ -_)
  // |_|_\___/\_,_|\__\___|
  protected function addRouteResolver(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    // 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 'concernement': return 'Concernement';
          case 'entite': return 'Entite';
          case 'besoin': return 'Besoin';
          case 'reponse': return 'Reponse';
          case 'static': return 'Static';
        }
      }
      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())
    ));
  }

  //    ____                                                          _       
  //   / ___|___  _ __   ___ ___ _ __ _ __   ___ _ __ ___   ___ _ __ | |_ ___ 
  //  | |   / _ \| '_ \ / __/ _ \ '__| '_ \ / _ \ '_ ` _ \ / _ \ '_ \| __/ __|
  //  | |__| (_) | | | | (_|  __/ |  | | | |  __/ | | | | |  __/ | | | |_\__ \
  //   \____\___/|_| |_|\___\___|_|  |_| |_|\___|_| |_| |_|\___|_| |_|\__|___/
                                                                         
  protected function addConcernements(ResolverRegistryInterface $registry, ResolverBuilder $builder){
    $registry->addFieldResolver('Query', 'allconcernements',
      $builder->compose(
          $builder->callback(function($parent, $arg){
            $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
            $query = $entity_storage->getQuery()
              ->condition('type', ['concernement'], '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', 'concernements',
      $builder->compose(
          $builder->callback(function($parent, $arg){
            $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
            $query = $entity_storage->getQuery()
              ->condition('type', ['concernement'], 'IN')
              ->accessCheck(TRUE);
            $results = $query->execute();
            return $results;
          }),
          $builder->produce('entity_load_multiple')
          ->map('type', $builder->fromValue('node'))
          ->map('ids', $builder->fromArgument('ids'))
        )
    );

    $registry->addFieldResolver('Query', 'concernement',
      $builder->produce('entity_load')
        ->map('type', $builder->fromValue('node'))
        ->map('id', $builder->fromArgument('id'))
    );

    $registry->addFieldResolver('Query', 'concernementrevisions',
      $builder->compose(
        $builder->produce('entity_load')
        ->map('type', $builder->fromValue('node'))
        ->map('id', $builder->fromArgument('id')),
        $builder->callback(function($parent, $arg){
          $vids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($parent);
          $revisions = [];
          foreach($vids as $vid){
            $revisions[] = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($vid);
          }
          return $revisions;
        })
      )
    );

    $registry->addFieldResolver('Concernement', 'id',
      $builder->produce('entity_id')
        ->map('entity', $builder->fromParent())
      );

    $registry->addFieldResolver('Concernement', 'revision_id',
      $builder->compose(
        $builder->callback(function($parent, $arg){
          $revid = $parent->getRevisionId();
          return $revid;
        })
      )
    );

    $registry->addFieldResolver('Concernement', 'uuid',
      $builder->produce('entity_uuid')
        ->map('entity', $builder->fromParent())
      );

    $registry->addFieldResolver('Concernement', 'path',
      $builder->compose(
        $builder->produce('entity_url')
          ->map('entity', $builder->fromParent()),
        $builder->produce('url_path')
          ->map('url', $builder->fromParent())
      )
    );

    $registry->addFieldResolver('Concernement', 'title',
      $builder->compose(
        $builder->produce('entity_label')
          ->map('entity', $builder->fromParent())
      ));

    $registry->addFieldResolver('Concernement', 'bundle',
      $builder->compose(
        $builder->produce('entity_bundle')
          ->map('entity', $builder->fromParent())
      ));

    $registry->addFieldResolver('Concernement', 'description',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_description.value'))
      );

    $registry->addFieldResolver('Concernement', 'caillou',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_caillou.value'))
      );

    $registry->addFieldResolver('Concernement', 'author',
      $builder->compose(
        $builder->produce('entity_owner')
          ->map('entity', $builder->fromParent())
      ));

    $registry->addFieldResolver('Concernement', 'created',
      $builder->compose(
        $builder->produce('entity_created')
          ->map('entity', $builder->fromParent())
      )
    );

    $registry->addFieldResolver('Concernement', 'changed',
      $builder->compose(
        $builder->produce('entity_changed')
          ->map('entity', $builder->fromParent())
      )
    );

    $registry->addFieldResolver('Concernement', 'lieu',
      $builder->produce('entity_reference')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_lieu'))
    );


    // $registry->addFieldResolver('Concernement', 'recit',
    //   $builder->produce('entity_reference')
    //     ->map('entity', $builder->fromParent())
    //     ->map('field', $builder->fromValue('field_recit'))
    //   );

    $registry->addFieldResolver('Concernement', 'recit',
      $builder->compose(
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_recit')),
        $builder->produce('seek')
          ->map('input', $builder->fromParent())
          ->map('position', $builder->fromValue(0))
      ));

    $registry->addFieldResolver('Concernement', 'recit_colophon',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_recit_colophon.value'))
    );

    $registry->addFieldResolver('Concernement', 'entites',
      $builder->produce('entity_reference_revisions')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_entite'))
      );

    // besoins: [Besoin]
    $registry->addFieldResolver('Concernement', 'besoins',
      $builder->produce('entity_reference')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_besoin'))
    );

    // doleances: [Doleance]
    $registry->addFieldResolver('Concernement', 'doleances',
      $builder->produce('entity_reference')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_doleancer'))
    );

    // revisions: [Concernement]
    $registry->addFieldResolver('Concernement', 'revisions',
      $builder->compose(
        $builder->callback(function($parent, $arg){
          $vids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($parent);
          $revisions = [];
          foreach($vids as $vid){
            $revisions[] = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($vid);
          }
          return $revisions;
        })
      )
    );
    
    // can-update: Boolean
    $registry->addFieldResolver('Concernement', 'can_update',
      $builder->compose(
        $builder->callback(function(/** @var Drupal\node\Entity\node */ $parent, $arg){
          $t="t";
          // $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
          $user = \Drupal::currentUser();
          $can_update = $parent->access('update', $user);
          return $can_update;
        })
      )
    );
    

  }

  //   _____       _   _ _    __        ___       _    __             __           
  //  | ____|_ __ | |_(_) |_ /_/  ___  |_ _|_ __ | |_ /_/  __ _ _ __ /_/  ___  ___ 
  //  |  _| | '_ \| __| | __/ _ \/ __|  | || '_ \| __/ _ \/ _` | '__/ _ \/ _ \/ __|
  //  | |___| | | | |_| | ||  __/\__ \  | || | | | ||  __/ (_| | | |  __/  __/\__ \
  //  |_____|_| |_|\__|_|\__\___||___/ |___|_| |_|\__\___|\__, |_|  \___|\___||___/
  //                                                      |___/                    
  protected function addEntitesIntegres(ResolverRegistryInterface $registry, ResolverBuilder $builder) {

    $registry->addFieldResolver('Entiteintegre', 'entite',
      $builder->compose(
        $builder->produce('entity_reference')
          ->map('entity', $builder->fromParent())
          ->map('field', $builder->fromValue('field_entite')),
        $builder->callback(function ($items) {
            if (isset($items[0])) {
              return $items[0];
            }
          })
      ));

    $registry->addFieldResolver('Entiteintegre', 'actuelfuture',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_actuel_future.value'))
      );

    $registry->addFieldResolver('Entiteintegre', 'menacemaintien',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_menace_maintien_degres.value'))
      );

    $registry->addFieldResolver('Entiteintegre', 'prise',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:paragraph'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_prise.value'))
        );
  }

  //   _____       _   _ _            
  //  | ____|_ __ | |_(_) |_ ___  ___ 
  //  |  _| | '_ \| __| | __/ _ \/ __|
  //  | |___| | | | |_| | ||  __/\__ \
  //  |_____|_| |_|\__|_|\__\___||___/
  protected function addEntites(ResolverRegistryInterface $registry, ResolverBuilder $builder){
    $registry->addFieldResolver('Query', 'allentites',
      $builder->compose(
          $builder->callback(function($parent, $arg){
            $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
            $query = $entity_storage->getQuery()
              ->condition('type', ['entite'], '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', 'entites',
      $builder->compose(
          $builder->callback(function($parent, $arg){
            $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
            $query = $entity_storage->getQuery()
              ->condition('type', ['entite'], 'IN')
              ->accessCheck(TRUE);
            $results = $query->execute();
            return $results;
          }),
          $builder->produce('entity_load_multiple')
          ->map('type', $builder->fromValue('node'))
          ->map('ids', $builder->fromArgument('ids'))
        )
    );

    $registry->addFieldResolver('Query', 'entite',
      $builder->produce('entity_load')
        ->map('type', $builder->fromValue('node'))
        ->map('id', $builder->fromArgument('id'))
    );

    $registry->addFieldResolver('Entite', 'id',
      $builder->produce('entity_id')
        ->map('entity', $builder->fromParent())
      );

    $registry->addFieldResolver('Entite', 'uuid',
      $builder->produce('entity_uuid')
        ->map('entity', $builder->fromParent())
      );

    $registry->addFieldResolver('Entite', 'path',
      $builder->compose(
        $builder->produce('entity_url')
          ->map('entity', $builder->fromParent()),
        $builder->produce('url_path')
          ->map('url', $builder->fromParent())
      )
    );

    $registry->addFieldResolver('Entite', 'title',
      $builder->compose(
        $builder->produce('entity_label')
          ->map('entity', $builder->fromParent())
      ));

    $registry->addFieldResolver('Entite', 'bundle',
      $builder->compose(
        $builder->produce('entity_bundle')
          ->map('entity', $builder->fromParent())
      ));
    
    // image: [Image]
    $registry->addFieldResolver('Entite', 'image',
      // $builder->produce('entity_reference')
      //   ->map('entity', $builder->fromParent())
      //   ->map('field', $builder->fromValue('field_image'))
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_image'))
    );
    

    // $registry->addFieldResolver('Entite', 'image',
    //   $builder->compose(
    //     $builder->produce('entity_reference')
    //       ->map('entity', $builder->fromParent())
    //       ->map('field', $builder->fromValue('field_image')),
    //     $builder->map(
    //       $builder->produce('image_derivative')
    //         ->map('entity', $builder->fromParent())
    //         ->map('style', $builder->fromValue('my_image_style'))
    //     )
    //   )
    // );

    // superposition: [Entite]
    $registry->addFieldResolver('Entite', 'superposition',
      $builder->produce('entity_reference')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_superposition'))
    );

    // proximite: [Entite]
    $registry->addFieldResolver('Entite', 'proximite',
      $builder->produce('entity_reference')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_proximite'))
    );

    // agissante: Boolean
    $registry->addFieldResolver('Entite', 'agissante',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_entite_agissante.value'))
      );
        
    $registry->addFieldResolver('Entite', 'action',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_action.value'))
      );

    $registry->addFieldResolver('Entite', 'menacemaintien',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_menace_maintien.value'))
      );

    // $registry->addFieldResolver('Entite', 'author',
    //   $builder->compose(
    //     $builder->produce('entity_owner')
    //       ->map('entity', $builder->fromParent()),
    //     $builder->produce('entity_label')
    //       ->map('entity', $builder->fromParent())
    //   )
    // );

    $registry->addFieldResolver('Entite', 'author',
      $builder->compose(
        $builder->produce('entity_owner')
          ->map('entity', $builder->fromParent())
      ));

    $registry->addFieldResolver('Entite', 'sources',
      $builder->produce('entity_reference_revisions')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_sources'))
      );

    $registry->addFieldResolver('Entite', 'concernement',
      $builder->compose(
        $builder->callback(function($parent, $arg){
          $entite_nid = $parent->id();
          $node_storage = \Drupal::entityTypeManager()->getStorage('node');
          $paragraph_storage = \Drupal::entityTypeManager()->getStorage('paragraph');
          $query = $node_storage->getQuery()
            ->condition('type', ['concernement'], 'IN')
            ->accessCheck(TRUE);
          $results = $query->execute();
          $concernements = [];
          // loop throught all concernements, then paragraphs to find wich one is pointing to this entite
          foreach ($results as $nid) {
            $concernement = $node_storage->load($nid);
            $entite_integre_paragraph_values = $concernement->field_entite->getValue();
            foreach ($entite_integre_paragraph_values as $paragraph_ids) {
              $paragraph = $paragraph_storage->load($paragraph_ids['target_id']);
              $field_entite_values = $paragraph->field_entite->getValue();
              if ($field_entite_values[0]['target_id'] === $entite_nid) {
                $concernements[] = $nid;
                break;
              }
            }
          }
          return $concernements[0];
        }),
        $builder->produce('entity_load')
          ->map('type', $builder->fromValue('node'))
          ->map('id', $builder->fromParent())
      )
    );

    // can-update: Boolean
    $registry->addFieldResolver('Entite', 'can_update',
      $builder->compose(
        $builder->callback(function(/** @var Drupal\node\Entity\node */ $parent, $arg){
          $user = \Drupal::currentUser();
          $can_update = $parent->access('update', $user);
          return $can_update;
        })
      )
    );
  

  }

  //   ____                           
  //  / ___|  ___  _   _ _ __ ___ ___ 
  //  \___ \ / _ \| | | | '__/ __/ _ \
  //   ___) | (_) | |_| | | | (_|  __/
  //  |____/ \___/ \__,_|_|  \___\___|                           
  protected function addSource(ResolverRegistryInterface $registry, ResolverBuilder $builder) {

    
    $registry->addFieldResolver('Source', 'description',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_description.value'))
      );
    
    $registry->addFieldResolver('Source', 'images',
      $builder->produce('entity_reference')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_images'))
      );

    $registry->addFieldResolver('Source', 'videos',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_videos'))
      );

    $registry->addFieldResolver('Source', 'documents',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_documents'))
      );

    $registry->addFieldResolver('Source', 'liens',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_liens'))
      );

    $registry->addFieldResolver('Source', 'date',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_date'))
      );

    $registry->addFieldResolver('Source', 'audios',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_audio'))
      );

  }

  //   ____                  _       
  //  | __ )  ___  ___  ___ (_)_ __  
  //  |  _ \ / _ \/ __|/ _ \| | '_ \ 
  //  | |_) |  __/\__ \ (_) | | | | |
  //  |____/ \___||___/\___/|_|_| |_|                             
  protected function addBesoin(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    $registry->addFieldResolver('Query', 'allbesoins',
      $builder->compose(
          $builder->callback(function($parent, $arg){
            $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
            $query = $entity_storage->getQuery()
              ->condition('type', ['besoin'], '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', 'besoins',
      $builder->compose(
          $builder->callback(function($parent, $arg){
            $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
            $query = $entity_storage->getQuery()
              ->condition('type', ['besoin'], 'IN')
              ->accessCheck(TRUE);
            $results = $query->execute();
            return $results;
          }),
          $builder->produce('entity_load_multiple')
          ->map('type', $builder->fromValue('node'))
          ->map('ids', $builder->fromArgument('ids'))
        )
    );

    $registry->addFieldResolver('Query', 'besoin',
      $builder->produce('entity_load')
        ->map('type', $builder->fromValue('node'))
        ->map('id', $builder->fromArgument('id'))
    );

    // id: Int!
    $registry->addFieldResolver('Besoin', 'id',
      $builder->produce('entity_id')
        ->map('entity', $builder->fromParent())
      );

    // path: String!
    $registry->addFieldResolver('Besoin', 'path',
      $builder->compose(
        $builder->produce('entity_url')
          ->map('entity', $builder->fromParent()),
        $builder->produce('url_path')
          ->map('url', $builder->fromParent())
      )
    );

    $registry->addFieldResolver('Besoin', 'author',
      $builder->compose(
        $builder->produce('entity_owner')
          ->map('entity', $builder->fromParent()),
        $builder->produce('entity_label')
          ->map('entity', $builder->fromParent())
      ));

    // description: String!        
    $registry->addFieldResolver('Besoin', 'description',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('body.value'))
      );

    // index: Int    
    $registry->addFieldResolver('Besoin', 'index',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_index.value'))
      );
    
    // concernement: Concernement
    $registry->addFieldResolver('Besoin', 'concernement',
      $builder->compose(
        $builder->produce('entity_reference')
          ->map('entity', $builder->fromParent())
          ->map('field', $builder->fromValue('field_concernement')),
        $builder->callback(function($parent, $arg){
          return $parent[0];
        })
      ));

    // reponses: [Reponse]
    $registry->addFieldResolver('Besoin', 'reponses',
      $builder->produce('entity_reference')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_reponse'))
    );

    // can-update: Boolean
    $registry->addFieldResolver('Besoin', 'can_update',
    $builder->compose(
      $builder->callback(function(/** @var Drupal\node\Entity\node */ $parent, $arg){
        $user = \Drupal::currentUser();
        $can_update = $parent->access('update', $user);
        return $can_update;
      })
    )
  );
  }

  //   ____                                 
  //  |  _ \ ___ _ __   ___  _ __  ___  ___ 
  //  | |_) / _ \ '_ \ / _ \| '_ \/ __|/ _ \
  //  |  _ <  __/ |_) | (_) | | | \__ \  __/
  //  |_| \_\___| .__/ \___/|_| |_|___/\___|
  //            |_|                         
  protected function addReponse(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    $registry->addFieldResolver('Query', 'allreponses',
      $builder->compose(
          $builder->callback(function($parent, $arg){
            $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
            $query = $entity_storage->getQuery()
              ->condition('type', ['cercle_politique'], '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', 'reponses',
      $builder->compose(
          $builder->callback(function($parent, $arg){
            $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
            $query = $entity_storage->getQuery()
              ->condition('type', ['reponse'], 'IN')
              ->accessCheck(TRUE);
            $results = $query->execute();
            return $results;
          }),
          $builder->produce('entity_load_multiple')
          ->map('type', $builder->fromValue('node'))
          ->map('ids', $builder->fromArgument('ids'))
        )
    );

    $registry->addFieldResolver('Query', 'reponse',
      $builder->produce('entity_load')
        ->map('type', $builder->fromValue('node'))
        ->map('id', $builder->fromArgument('id'))
    );

    // id: Int!
    $registry->addFieldResolver('Reponse', 'id',
      $builder->produce('entity_id')
        ->map('entity', $builder->fromParent())
      );

    // path: String!
    $registry->addFieldResolver('Reponse', 'path',
      $builder->compose(
        $builder->produce('entity_url')
          ->map('entity', $builder->fromParent()),
        $builder->produce('url_path')
          ->map('url', $builder->fromParent())
      )
    );

    $registry->addFieldResolver('Reponse', 'author',
      $builder->compose(
        $builder->produce('entity_owner')
          ->map('entity', $builder->fromParent()),
        $builder->produce('entity_label')
          ->map('entity', $builder->fromParent())
      ));

    // besoin: Besoin
    $registry->addFieldResolver('Reponse', 'besoin',
      $builder->compose(
        $builder->produce('entity_reference')
          ->map('entity', $builder->fromParent())
          ->map('field', $builder->fromValue('field_besoin_on_reponses')),
        $builder->callback(function($parent, $arg){
          return $parent[0];
        })
      ));

    // qui: String    
    $registry->addFieldResolver('Reponse', 'qui',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_qui.value'))
      );

    // quoi: String    
    $registry->addFieldResolver('Reponse', 'quoi',
    $builder->produce('property_path')
      ->map('type', $builder->fromValue('entity:node'))
      ->map('value', $builder->fromParent())
      ->map('path', $builder->fromValue('field_quoi.value'))
    );

    // ou: String    
    $registry->addFieldResolver('Reponse', 'ou',
    $builder->produce('property_path')
      ->map('type', $builder->fromValue('entity:node'))
      ->map('value', $builder->fromParent())
      ->map('path', $builder->fromValue('field_ou.value'))
    );

    // avec: String    
    $registry->addFieldResolver('Reponse', 'avec',
    $builder->produce('property_path')
      ->map('type', $builder->fromValue('entity:node'))
      ->map('value', $builder->fromParent())
      ->map('path', $builder->fromValue('field_avec.value'))
    );

    // can-update: Boolean
    $registry->addFieldResolver('Reponse', 'can_update',
      $builder->compose(
        $builder->callback(function(/** @var Drupal\node\Entity\node */ $parent, $arg){
          $user = \Drupal::currentUser();
          $can_update = $parent->access('update', $user);
          return $can_update;
        })
      )
    );
  }

  //   ____        _                           
  //  |  _ \  ___ | | ___  __ _ _ __   ___ ___ 
  //  | | | |/ _ \| |/ _ \/ _` | '_ \ / __/ _ \
  //  | |_| | (_) | |  __/ (_| | | | | (_|  __/
  //  |____/ \___/|_|\___|\__,_|_| |_|\___\___|
  protected function addDoleance(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    $registry->addFieldResolver('Query', 'alldoleances',
      $builder->compose(
          $builder->callback(function($parent, $arg){
            $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
            $query = $entity_storage->getQuery()
              ->condition('type', ['cercle_politique'], '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', 'doleances',
      $builder->compose(
          $builder->callback(function($parent, $arg){
            $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
            $query = $entity_storage->getQuery()
              ->condition('type', ['cercle_politique'], 'IN')
              ->accessCheck(TRUE);
            $results = $query->execute();
            return $results;
          }),
          $builder->produce('entity_load_multiple')
          ->map('type', $builder->fromValue('node'))
          ->map('ids', $builder->fromArgument('ids'))
        )
    );

    $registry->addFieldResolver('Query', 'doleance',
      $builder->produce('entity_load')
        ->map('type', $builder->fromValue('node'))
        ->map('id', $builder->fromArgument('id'))
    );

    $registry->addFieldResolver('Doleance', 'id',
      $builder->produce('entity_id')
        ->map('entity', $builder->fromParent())
      );

    $registry->addFieldResolver('Doleance', 'path',
        $builder->compose(
          $builder->produce('entity_url')
            ->map('entity', $builder->fromParent()),
          $builder->produce('url_path')
            ->map('url', $builder->fromParent())
        )
      );

    $registry->addFieldResolver('Doleance', 'uuid',
      $builder->produce('entity_uuid')
        ->map('entity', $builder->fromParent())
      );

    $registry->addFieldResolver('Doleance', 'bundle',
      $builder->compose(
        $builder->produce('entity_bundle')
          ->map('entity', $builder->fromParent())
      ));

    $registry->addFieldResolver('Doleance', 'title',
      $builder->compose(
        $builder->produce('entity_label')
          ->map('entity', $builder->fromParent())
      ));

    $registry->addFieldResolver('Doleance', 'author',
      $builder->compose(
        $builder->produce('entity_owner')
          ->map('entity', $builder->fromParent()),
        $builder->produce('entity_label')
          ->map('entity', $builder->fromParent())
      ));

      // #1
      // leprobleme: String
      $registry->addFieldResolver('Doleance', 'leprobleme',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_le_probleme.value'))
        );
      // date_leprobleme: Date
      $registry->addFieldResolver('Doleance', 'date_leprobleme',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_date_leprobleme'))
        );
      // lenquete: String
      $registry->addFieldResolver('Doleance', 'lenquete',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_enquete_terraindevie.value'))
        );
      // groupesinterets: [GroupeInterets]
      $registry->addFieldResolver('Doleance', 'groupesinterets',
        $builder->produce('entity_reference_revisions')
          ->map('entity', $builder->fromParent())
          ->map('field', $builder->fromValue('field_groupes'))
        );


      // #2
      // date_adresse_doleance: Date
      $registry->addFieldResolver('Doleance', 'date_adresse',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_date_adresse_doleance'))
        );
      // entite_addresse_doleance: String
      $registry->addFieldResolver('Doleance', 'entite_addresse_doleance',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_entite_adresse_doleance.value'))
        );
      // comment_ennonce_doleance: String
      $registry->addFieldResolver('Doleance', 'comment_ennonce_doleance',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_comment_enonce_doleance.value'))
        );
      // aqui_addresse_doleance: String
      $registry->addFieldResolver('Doleance', 'aqui_addresse_doleance',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_a_qui_adresse_doleance.value'))
        );
      // reception_traitement: [ReceptionTraitement]
      $registry->addFieldResolver('Doleance', 'reception_traitement',
        $builder->produce('entity_reference_revisions')
          ->map('entity', $builder->fromParent())
          ->map('field', $builder->fromValue('field_receptions_et_traitements'))
        );

      // #3
      // field_date_decision: Date
      $registry->addFieldResolver('Doleance', 'date_decision',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_date_decision'))
      );
      // entites_decisionnaires: String
      $registry->addFieldResolver('Doleance', 'entites_decisionnaires',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_entites_decisionnaires.value'))
        );
      // decision_formule: String
      $registry->addFieldResolver('Doleance', 'decision_formule',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_decision_formulee.value'))
        );
      // mise_en_oeuvre_decision: [MiseEnoeuvreDecision] 
      $registry->addFieldResolver('Doleance', 'mise_en_oeuvre_decision',
        $builder->produce('entity_reference_revisions')
          ->map('entity', $builder->fromParent())
          ->map('field', $builder->fromValue('field_mise_en_oeuvre_decision'))
        );
      
      // #4
      // date_adresse_decision: Date
      $registry->addFieldResolver('Doleance', 'date_application',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_date_adresse_decision'))
      );
      // entite_addresse_application: String
      $registry->addFieldResolver('Doleance', 'entite_adresse_application',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_entite_adresse_application.value'))
        );
      // aqui_adresse_decision: String
      $registry->addFieldResolver('Doleance', 'aqui_adresse_decision',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_aqui_adresse_decision.value'))
        );
      // comment_formule_decision: String
      $registry->addFieldResolver('Doleance', 'comment_formule_decision',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_comment_formule_decision.value'))
        );
      // receptions_et_applications: [ReceptionsApplications] 
      $registry->addFieldResolver('Doleance', 'receptions_et_applications',
        $builder->produce('entity_reference_revisions')
          ->map('entity', $builder->fromParent())
          ->map('field', $builder->fromValue('field_receptions_et_applications'))
        );
      // probleme_initial_resolu: Int
      $registry->addFieldResolver('Doleance', 'probleme_initial_resolu',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_probleme_initial_resolu.value'))
        );
      // oui_nouvelle_situation: String
      $registry->addFieldResolver('Doleance', 'oui_nouvelle_situation',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_oui_nouvelle_situation.value'))
        );
      // non_adresse_doleance: String
      $registry->addFieldResolver('Doleance', 'non_adresse_doleance',
        $builder->produce('property_path')
          ->map('type', $builder->fromValue('entity:node'))
          ->map('value', $builder->fromParent())
          ->map('path', $builder->fromValue('field_non_adresse_doleance.value'))
        );

      // can-update: Boolean
      $registry->addFieldResolver('Doleance', 'can_update',
        $builder->compose(
          $builder->callback(function(/** @var Drupal\node\Entity\node */ $parent, $arg){
            $user = \Drupal::currentUser();
            $can_update = $parent->access('update', $user);
            return $can_update;
          })
        )
      );
  }
  
  //    ____                              ___       _                _       
  //   / ___|_ __ ___  _   _ _ __   ___  |_ _|_ __ | |_ ___ _ __ ___| |_ ___ 
  //  | |  _| '__/ _ \| | | | '_ \ / _ \  | || '_ \| __/ _ \ '__/ _ \ __/ __|
  //  | |_| | | | (_) | |_| | |_) |  __/  | || | | | ||  __/ | |  __/ |_\__ \
  //   \____|_|  \___/ \__,_| .__/ \___| |___|_| |_|\__\___|_|  \___|\__|___/
  //                        |_|                                              
  protected function addGroupeInterets(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    
    // field_date_groupe_interets: Date
    $registry->addFieldResolver('GroupeInterets', 'date',
    $builder->produce('property_path')
      ->map('type', $builder->fromValue('entity:paragraph'))
      ->map('value', $builder->fromParent())
      ->map('path', $builder->fromValue('field_date_groupe_interets'))
    );
    // groupe_interets: String
    $registry->addFieldResolver('GroupeInterets', 'groupe_interets',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_groupe_interets.value'))
      );
    // accorder_interets: String
    $registry->addFieldResolver('GroupeInterets', 'accorder_interets',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_accorder_interets.value'))
      );
    // formuler: String
    $registry->addFieldResolver('GroupeInterets', 'formuler',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_formuler.value'))
      );
  }

  //   ____                     _   _               _____          _ _                            _   
  //  |  _ \ ___  ___ ___ _ __ | |_(_) ___  _ __   |_   _| __ __ _(_) |_ ___ _ __ ___   ___ _ __ | |_ 
  //  | |_) / _ \/ __/ _ \ '_ \| __| |/ _ \| '_ \    | || '__/ _` | | __/ _ \ '_ ` _ \ / _ \ '_ \| __|
  //  |  _ <  __/ (_|  __/ |_) | |_| | (_) | | | |   | || | | (_| | | ||  __/ | | | | |  __/ | | | |_ 
  //  |_| \_\___|\___\___| .__/ \__|_|\___/|_| |_|   |_||_|  \__,_|_|\__\___|_| |_| |_|\___|_| |_|\__|
  //                     |_|                                                                          
  protected function addReceptionTraitement(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    // field_date_reception_traite: Date
    $registry->addFieldResolver('ReceptionTraitement', 'date',
    $builder->produce('property_path')
      ->map('type', $builder->fromValue('entity:paragraph'))
      ->map('value', $builder->fromParent())
      ->map('path', $builder->fromValue('field_date_reception_traite'))
    );
    // entite_adressee: String
    $registry->addFieldResolver('ReceptionTraitement', 'entite_adressee',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_entite_adressee.value'))
      );
    // doleance_formulee: String
    $registry->addFieldResolver('ReceptionTraitement', 'doleance_formulee',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_doleance_formulee.value'))
      );
    // traite_doleance: String
    $registry->addFieldResolver('ReceptionTraitement', 'traite_doleance',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_traite_doleance.value'))
      );
    // entite_recoit_doleance: String
    $registry->addFieldResolver('ReceptionTraitement', 'entite_recoit_doleance',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_entite_recoit_doleance.value'))
      );
  }

  //   __  __ _            _____                                         ____            _     _             
  //  |  \/  (_)___  ___  | ____|_ __   ___   ___ _   ___   ___ __ ___  |  _ \  ___  ___(_)___(_) ___  _ __  
  //  | |\/| | / __|/ _ \ |  _| | '_ \ / _ \ / _ \ | | \ \ / / '__/ _ \ | | | |/ _ \/ __| / __| |/ _ \| '_ \ 
  //  | |  | | \__ \  __/ | |___| | | | (_) |  __/ |_| |\ V /| | |  __/ | |_| |  __/ (__| \__ \ | (_) | | | |
  //  |_|  |_|_|___/\___| |_____|_| |_|\___/ \___|\__,_| \_/ |_|  \___| |____/ \___|\___|_|___/_|\___/|_| |_|
  protected function addMiseEnoeuvreDecision(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    // field_date_miseeoeuvre_decision: Date
    $registry->addFieldResolver('MiseEnoeuvreDecision', 'date',
    $builder->produce('property_path')
      ->map('type', $builder->fromValue('entity:paragraph'))
      ->map('value', $builder->fromParent())
      ->map('path', $builder->fromValue('field_date_miseeoeuvre_decision'))
    );
    // entite_adresse_decision: String
    $registry->addFieldResolver('MiseEnoeuvreDecision', 'entite_adresse_decision',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_entite_adresse_decision.value'))
      );
    // formule_decision: String
    $registry->addFieldResolver('MiseEnoeuvreDecision', 'formule_decision',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_formule_decision.value'))
      );
    // entite_metenoeuvre_decisio: String
    $registry->addFieldResolver('MiseEnoeuvreDecision', 'entite_metenoeuvre_decisio',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_entite_metenoeuvre_decisio.value'))
      );
  }

  //   ____                     _   _                      _                _ _           _   _                 
  //  |  _ \ ___  ___ ___ _ __ | |_(_) ___  _ __  ___     / \   _ __  _ __ | (_) ___ __ _| |_(_) ___  _ __  ___ 
  //  | |_) / _ \/ __/ _ \ '_ \| __| |/ _ \| '_ \/ __|   / _ \ | '_ \| '_ \| | |/ __/ _` | __| |/ _ \| '_ \/ __|
  //  |  _ <  __/ (_|  __/ |_) | |_| | (_) | | | \__ \  / ___ \| |_) | |_) | | | (_| (_| | |_| | (_) | | | \__ \
  //  |_| \_\___|\___\___| .__/ \__|_|\___/|_| |_|___/ /_/   \_\ .__/| .__/|_|_|\___\__,_|\__|_|\___/|_| |_|___/
  //                     |_|                                   |_|   |_|                                        
  protected function addReceptionsApplications(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    // field_date_date_traitement_decis: Date
    $registry->addFieldResolver('ReceptionsApplications', 'date',
    $builder->produce('property_path')
      ->map('type', $builder->fromValue('entity:paragraph'))
      ->map('value', $builder->fromParent())
      ->map('path', $builder->fromValue('field_date_date_traitement_decis'))
    );
    // applique_decision: String
    $registry->addFieldResolver('ReceptionsApplications', 'applique_decision',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_applique_decision.value'))
      );
      // formule_decision_applic: String
    $registry->addFieldResolver('ReceptionsApplications', 'formule_decision_applic',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_formule_decision_applic.value'))
      );
      // entite_recoit_decision: String
    $registry->addFieldResolver('ReceptionsApplications', 'entite_recoit_decision',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:paragraph'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_entite_recoit_decision.value'))
      );
    
  }
  //  ___ _        _   _
  // / __| |_ __ _| |_(_)__ ___
  // \__ \  _/ _` |  _| / _(_-<
  // |___/\__\__,_|\__|_\__/__/
  protected function addStatic(ResolverRegistryInterface $registry, ResolverBuilder $builder) {

    $registry->addFieldResolver('Query', 'allstatics',
      $builder->compose(
        $builder->callback(function($parent, $arg){
          $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
          $query = $entity_storage->getQuery()
            ->condition('type', ['static'], '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', 'promotedstatics',
      $builder->compose(
        $builder->callback(function($parent, $arg){
          $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
          $query = $entity_storage->getQuery()
            ->condition('type', ['static'], 'IN')
            ->condition('promote', true)
            ->accessCheck(TRUE);
          $results = $query->execute();
          return $results;
        }),
        $builder->produce('entity_load_multiple')
        ->map('type', $builder->fromValue('node'))
        ->map('ids', $builder->fromParent())
      )
    );


    $registry->addFieldResolver('Query', 'statics',
      $builder->produce('entity_load_multiple')
        ->map('type', $builder->fromValue('node'))
        ->map('ids', $builder->fromArgument('ids'))
    );

    $registry->addFieldResolver('Query', 'static',
      $builder->produce('entity_load')
        ->map('type', $builder->fromValue('node'))
        ->map('bundles', $builder->fromValue(['static']))
        ->map('id', $builder->fromArgument('id'))
    );

    $registry->addFieldResolver('Static', 'id',
      $builder->produce('entity_id')
        ->map('entity', $builder->fromParent())
    );

    $registry->addFieldResolver('Static', 'uuid',
      $builder->produce('entity_uuid')
        ->map('entity', $builder->fromParent())
    );

    $registry->addFieldResolver('Static', 'path',
      $builder->compose(
        $builder->produce('entity_url')
          ->map('entity', $builder->fromParent()),
        $builder->produce('url_path')
          ->map('url', $builder->fromParent())
      )
    );

    $registry->addFieldResolver('Static', 'title',
      $builder->compose(
        $builder->produce('entity_label')
          ->map('entity', $builder->fromParent())
    ));

    $registry->addFieldResolver('Static', 'bundle',
      $builder->compose(
        $builder->produce('entity_bundle')
          ->map('entity', $builder->fromParent())
    ));

    $registry->addFieldResolver('Static', 'promoted',
      $builder->produce('property_path')
      ->map('type', $builder->fromValue('entity:node'))
      ->map('value', $builder->fromParent())
      ->map('path', $builder->fromValue('promote.value'))
    );

    $registry->addFieldResolver('Static', 'texte',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('body.value'))
    );

    $registry->addFieldResolver('Static', 'author',
      $builder->compose(
        $builder->produce('entity_owner')
          ->map('entity', $builder->fromParent()),
        $builder->produce('entity_label')
          ->map('entity', $builder->fromParent())
    ));

    $registry->addFieldResolver('Static', 'fichiers',
      $builder->produce('entity_reference')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_fichier'))
      );

  }

  //    ______                      
  //   / ____/________  __  ______ 
  //  / / __/ ___/ __ \/ / / / __ \
  // / /_/ / /  / /_/ / /_/ / /_/ /
  // \____/_/   \____/\__,_/ .___/ 
  //                      /_/      
  protected function addGroup(ResolverRegistryInterface $registry, ResolverBuilder $builder) {

    $registry->addFieldResolver('Query', 'allgroups',
      $builder->compose(
        $builder->callback(function($parent, $arg){
          $entity_storage = \Drupal::entityTypeManager()->getStorage('group');
          $query = $entity_storage->getQuery()
            // ->condition('type', ['static'], 'IN')
            ->accessCheck(TRUE);
          $results = $query->execute();
          return $results;
        }),
        $builder->produce('entity_load_multiple')
        ->map('type', $builder->fromValue('group'))
        ->map('ids', $builder->fromParent())
      )
    );

    $registry->addFieldResolver('Query', 'groups',
      $builder->produce('entity_load_multiple')
        ->map('type', $builder->fromValue('group'))
        ->map('ids', $builder->fromArgument('ids'))
    );

    $registry->addFieldResolver('Query', 'group',
      $builder->produce('entity_load')
        ->map('type', $builder->fromValue('group'))
        // ->map('bundles', $builder->fromValue(['static']))
        ->map('id', $builder->fromArgument('id'))
    );

    $registry->addFieldResolver('Group', 'id',
      $builder->produce('entity_id')
        ->map('entity', $builder->fromParent())
    );

    $registry->addFieldResolver('Group', 'uuid',
      $builder->produce('entity_uuid')
        ->map('entity', $builder->fromParent())
    );

    $registry->addFieldResolver('Group', 'name',
      $builder->compose(
        $builder->produce('entity_label')
          ->map('entity', $builder->fromParent())
    ));

    $registry->addFieldResolver('Group', 'description',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:node'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('field_description.value'))
    );

    $registry->addFieldResolver('Group', 'path',
      $builder->compose(
        $builder->produce('entity_url')
          ->map('entity', $builder->fromParent()),
        $builder->produce('url_path')
          ->map('url', $builder->fromParent())
      )
    );

    $registry->addFieldResolver('Group', 'owner',
      $builder->produce('entity_owner')
        ->map('entity', $builder->fromParent())
    );

    $registry->addFieldResolver('Group', 'members',
      $builder->compose(
        $builder->callback(function ($parent, $args) {
          $memberships = $parent->getMembers();
          $users = [];
          foreach ($memberships as $member) {
            $users[] = $member->getUser();
          }
          return $users;
        })
      )
    );

    // $registry->addFieldResolver('Group', 'concernements',
    //   $builder->compose(
    //     $builder->callback(function ($parent, $args) {
    //       $memberships = $parent->getMembers();
    //       $users = [];
    //       foreach ($memberships as $member) {
    //         $users[] = $member->getUser();
    //       }
    //       return $users;
    //     }),

    //   )
    // );
    
  }

  //    __  __              
  //   / / / /_______  _____
  //  / / / / ___/ _ \/ ___/
  // / /_/ (__  )  __/ /    
  // \____/____/\___/_/     
  protected function addUser(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    
    $registry->addFieldResolver('Query', 'user',
      $builder->produce('entity_load')
        ->map('type', $builder->fromValue('user'))
        // ->map('bundles', $builder->fromValue(['static']))
        ->map('id', $builder->fromArgument('id'))
    );

    $registry->addFieldResolver('User', 'id',
      $builder->produce('entity_id')
        ->map('entity', $builder->fromParent())
    );

    $registry->addFieldResolver('User', 'email',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:user'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('mail.value'))
    );

    $registry->addFieldResolver('User', 'username',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:user'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('name.value'))
    );

    $registry->addFieldResolver('User', 'roles',
        $builder->callback(function ($parent, $args) {
          return $roles_ids = $parent->getRoles();
          // $entity_storage = \Drupal::entityTypeManager()->getStorage('user_role');
          // $roles = $entity_storage->loadMultiple($roles_ids);
          // return $roles;
        })
    );

    // ROLE
    // type Role {
    //   name: String!
    // }
    // $registry->addFieldResolver('Role', 'name',
    //   $builder->produce('entity_label')
    //     ->map('entity', $builder->fromParent())
    // );

    $registry->addFieldResolver('User', 'concernements',
      $builder->compose(
        $builder->callback(function($parent, $arg){
          $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
          $query = $entity_storage->getQuery()
            ->condition('type', ['concernement'], 'IN')
            ->condition('uid', $parent->id())
            ->accessCheck(TRUE);
          $results = $query->execute();
          return $results;
        }),
        $builder->produce('entity_load_multiple')
        ->map('type', $builder->fromValue('node'))
        ->map('ids', $builder->fromParent())
      )
    );
    
    $registry->addFieldResolver('User', 'groups',
      $builder->compose(
        $builder->callback(function($parent, $arg){
          $group_relationship_storage = \Drupal::entityTypeManager()->getStorage('group_relationship');
          
          $query = $group_relationship_storage->getQuery()
            ->condition('entity_id', $parent->id())
            ->accessCheck(TRUE);
          $group_relationship_ids = $query->execute();
          
          $group_relationships = $group_relationship_storage->loadMultiple($group_relationship_ids);
          
          $group_ids = [];
          foreach ($group_relationships as $group_relationship) {
            $group_ids[] = $group_relationship->getGroupId();
          }
          return $group_ids;
        }),
        $builder->produce('entity_load_multiple')
        ->map('type', $builder->fromValue('group'))
        ->map('ids', $builder->fromParent())
      )
    );

    $registry->addFieldResolver('User', 'structure',
      $builder->produce('entity_reference')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_strucure'))
    );
    
  }

  //  ___       _
  // |   \ __ _| |_ ___
  // | |) / _` |  _/ -_)
  // |___/\__,_|\__\___|
  protected function addDate(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    $registry->addFieldResolver('Date', 'start',
      $builder->callback(function ($parent, $args) {
        return isset($parent[0]) ? $parent[0]['value'] : null;
      })
    );
    $registry->addFieldResolver('Date', 'end',
      $builder->callback(function ($parent, $args) {
        return isset($parent[0]) ? $parent[0]['end_value'] : null;
      })
    );
  }

  //  ___ _ _      __ _     _    _
  // | __(_) |___ / _(_)___| |__| |
  // | _|| | / -_)  _| / -_) / _` |
  // |_| |_|_\___|_| |_\___|_\__,_|
  protected function addFilefield(ResolverRegistryInterface $registry, ResolverBuilder $builder) {

    $registry->addFieldResolver('Filefield', 'description',
      $builder->callback(function ($parent, $args) {
        if (is_array($parent) && isset($parent['description'])) {
          return $parent['description'];
        } else if (is_array($parent) && isset($parent[0]['description'])) {
          return $parent[0]['description'];
        } else{
          return null;
        }
      })
    );

    $registry->addFieldResolver('Filefield', 'file',
      $builder->callback(function ($parent, $args) {
        if (is_array($parent) && isset($parent['target_id'])) {
          return \Drupal\file\Entity\File::load($parent['target_id']);
        } else if (is_array($parent) && isset($parent[0]['target_id'])) {
          return \Drupal\file\Entity\File::load($parent[0]['target_id']);
        } else{
          return null;
        }
      })
    );

  }

  //  ___ _ _
  // | __(_) |___
  // | _|| | / -_)
  // |_| |_|_\___|
  protected function addFile(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    $registry->addFieldResolver('File', 'url',
      $builder->callback(function ($parent, $args) {
        return $parent->createFileUrl();
      })
    );

    $registry->addFieldResolver('File', 'filesize',
      $builder->callback(function ($parent, $args) {
        return $parent->getSize();
      })
    );

    $registry->addFieldResolver('File', 'filemime',
      $builder->callback(function ($parent, $args) {
        return $parent->getMimeType();
      })
    );

    $registry->addFieldResolver('File', 'filename',
      $builder->callback(function ($parent, $args) {
        return $parent->getFilename();
      })
    );

    $registry->addFieldResolver('File', 'fid',
      $builder->callback(function ($parent, $args) {
        return $parent->id();
      })
    );

    $registry->addFieldResolver('File', 'uuid',
      $builder->callback(function ($parent, $args) {
        return $parent->uuid();
      })
    );
  }


  // protected function addImageField(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
  //   $registry->addFieldResolver('Imagefield', 'image',
  //     $builder->callback(function ($parent, $args) {
  //       if (is_array($parent) && isset($parent['target_id'])) {
  //         return \Drupal\file\Entity\File::load($parent['target_id']);
  //       } else if (is_array($parent) && isset($parent[0]['target_id'])) {
  //         return \Drupal\file\Entity\File::load($parent[0]['target_id']);
  //       } else{
  //         return $parent;
  //       }
  //     })
      
  //   );


  //   // $registry->addFieldResolver('Imagefield', 'alt',
  //   //   $builder->callback(function ($parent, $args) {
  //   //     $t="t";
  //   //     // return isset($parent[0]) ? $parent[0]['alt'] : null;
  //   //     return "Alt";
  //   //   })
  //   // );

  // }

  //  ___
  // |_ _|_ __  __ _ __ _ ___
  //  | || '  \/ _` / _` / -_)
  // |___|_|_|_\__,_\__, \___|
  //                |___/
  protected function addImage(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    // $registry->addFieldResolver('Image', 'id',
    //   $builder->produce('entity_id')
    //     ->map('entity', $builder->fromParent())
    // );
    
    $registry->addFieldResolver('Image', 'id',  
      $builder->callback(function($parent, $args){
        return $parent['target_id'];
      })
    );

    $registry->addFieldResolver('Image', 'url',
      $builder->compose(
        $builder->callback(function($parent, $args){
          return \Drupal\file\Entity\File::load($parent['target_id']);
        }),
        $builder->produce('image_url')
          ->map('entity', $builder->fromParent()),
      )
      );


    $registry->addFieldResolver('Image', 'alt',
      $builder->callback(function($parent, $args){
        return $parent['alt'];
      })
    );

    $registry->addFieldResolver('Image', 'tilte',
      $builder->callback(function($parent, $args){
        return $parent['tilte'];
      })
    );

    // $registry->addFieldResolver('Image', 'style_minicard',
    //   $builder->produce('image_derivative')
    //     ->map('entity', $builder->fromParent())
    //     ->map('style', $builder->fromValue('card_medium_half'))
    //   );

    // $registry->addFieldResolver('Image', 'style_minicard_url',
    //   $builder->compose(
    //     $builder->produce('image_derivative')
    //       ->map('entity', $builder->fromParent())
    //       ->map('style', $builder->fromValue('card_medium_half')),
    //     $builder->callback(function($parent, $args){
    //         return $parent['url'];
    //       })
    //   ));

  }

  // __   ___    _         _    _      _
  // \ \ / (_)__| |___ ___| |  (_)_ _ | |__
  //  \ V /| / _` / -_) _ \ |__| | ' \| / /
  //   \_/ |_\__,_\___\___/____|_|_||_|_\_\
  protected function addVideolink(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    $registry->addFieldResolver('VideoLink', 'url',
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('field_item:video_embed_field'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('value'))
      );
  }

  //  _____
  // |_   _|_ _ __ _
  //   | |/ _` / _` |
  //   |_|\__,_\__, |
  //           |___/
  protected function addTaxoterm(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    // $registry->addFieldResolver('Query', 'alltags',
    //   $builder->compose(
    //       $builder->callback(function($parent, $arg){
    //         $entity_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    //         $query = $entity_storage->getQuery()
    //           ->condition('vid', ['tag','tag_prod'], 'IN')
    //           ->accessCheck(TRUE);
    //         $results = $query->execute();
    //         return $results;
    //       }),
    //       $builder->produce('entity_load_multiple')
    //       ->map('type', $builder->fromValue('taxonomy_term'))
    //       ->map('ids', $builder->fromParent())
    //     )
    // );

    // $registry->addFieldResolver('Query', 'tag',
    //  $builder->produce('entity_load')
    //    ->map('type', $builder->fromValue('taxonomy_term'))
    //    ->map('bundles', $builder->fromValue(['tag']))
    //    ->map('id', $builder->fromArgument('id'))
    //  );
    // $registry->addFieldResolver('Query', 'famille',
    //   $builder->produce('entity_load')
    //     ->map('type', $builder->fromValue('taxonomy_term'))
    //     ->map('bundles', $builder->fromValue(['famille']))
    //     ->map('id', $builder->fromArgument('id'))
    // );
    // $registry->addFieldResolver('Query', 'tagprod',
    //  $builder->produce('entity_load')
    //    ->map('type', $builder->fromValue('taxonomy_term'))
    //    ->map('bundles', $builder->fromValue(['tag_prod']))
    //    ->map('id', $builder->fromArgument('id'))
    //  );

    // get all tags for a given familly
    // $registry->addFieldResolver('Query', 'famillytags',
    //   $builder->compose(
    //     $builder->callback(function($parent, $arg){
    //       $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
    //       $query = $entity_storage->getQuery()
    //         ->condition('type', ['texte', 'texte_prod'], 'IN')
    //         ->condition('field_famille', $arg->famillyid)
    //         ->accessCheck(TRUE);
    //       $results = $query->execute();
    //       return $results;
    //     }),
    //     $builder->produce('entity_load_multiple')
    //     ->map('type', $builder->fromValue('node'))
    //     ->map('ids', $builder->fromParent())
    //     //  $builder->produce('entity_load_multiple')
    //     //  ->map('type', $builder->fromValue('taxonomy_term'))
    //     //  ->map('ids', $builder->fromParent())
    //   )
    // );


    $registry->addFieldResolver('Taxoterm', 'id',
     $builder->produce('entity_id')
       ->map('entity', $builder->fromParent())
     );

    $registry->addFieldResolver('Taxoterm', 'uuid',
     $builder->produce('entity_uuid')
       ->map('entity', $builder->fromParent())
     );

    $registry->addFieldResolver('Taxoterm', 'name',
      $builder->produce('entity_label')
        ->map('entity', $builder->fromParent())
      );

    $registry->addFieldResolver('Taxoterm', 'vocabulary',
      $builder->produce('entity_bundle')
        ->map('entity', $builder->fromParent())
    );
  }
  
  
  //  _    _      _
  // | |  (_)_ _ | |__
  // | |__| | ' \| / /
  // |____|_|_||_|_\_\
  protected function addLink(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    $registry->addFieldResolver('Link', 'url',
      $builder->callback(function ($parent, $args) {
        // return isset($parent[0]) ? $parent[0]['uri'] : null;
        return $parent['uri'];
      })
    );
    $registry->addFieldResolver('Link', 'title',
      $builder->callback(function ($parent, $args) {
        // return isset($parent[0]) ? $parent[0]['title'] : null;
        return $parent['title'];
      })
    );
  }

  //   _____       _   _ _           ____        __ _       _ _   _             
  //  | ____|_ __ | |_(_) |_ _   _  |  _ \  ___ / _(_)_ __ (_) |_(_) ___  _ __  
  //  |  _| | '_ \| __| | __| | | | | | | |/ _ \ |_| | '_ \| | __| |/ _ \| '_ \ 
  //  | |___| | | | |_| | |_| |_| | | |_| |  __/  _| | | | | | |_| | (_) | | | |
  //  |_____|_| |_|\__|_|\__|\__, | |____/ \___|_| |_|_| |_|_|\__|_|\___/|_| |_|
  //                         |___/                                              
  protected function addEntityDefinition(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    $registry->addFieldResolver('Query', 'entitydef',
      $builder->compose(
        $builder->callback(function($parent, $arg){
          // $entity_object = NodeType::load($builder->fromArgument('bundle'));
          $field_defintions = \Drupal::service('entity_field.manager')->getFieldDefinitions($arg['type'], $arg['bundle']);
          return $field_defintions;
        })
      )
    );

    $registry->addFieldResolver('EntityDefinition', 'fields',
      $builder->callback(function ($parent, $args) {
        return $parent;
      })
    );

  }

  //   _____ _      _     _   ____        __ 
  //  |  ___(_) ___| | __| | |  _ \  ___ / _|
  //  | |_  | |/ _ \ |/ _` | | | | |/ _ \ |_ 
  //  |  _| | |  __/ | (_| | | |_| |  __/  _|
  //  |_|   |_|\___|_|\__,_| |____/ \___|_|                                     
  protected function addFieldDef(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    
    $registry->addFieldResolver('FieldDef', 'type',
      $builder->callback(function ($parent, $args) {
        return $parent->getType();
      })
    );

    $registry->addFieldResolver('FieldDef', 'field_name',
      $builder->callback(function ($parent, $args) {
        return $parent->getName();
      })
    );

    $registry->addFieldResolver('FieldDef', 'label',
      $builder->callback(function ($parent, $args) {
        $label = $parent->getLabel();
        $vartype = gettype($label);
        switch ($vartype) {
          case 'string':
            return $label;
            break;
          case 'object':
            switch (get_class($label)) {
              case 'TranslatableMarkup':
                return $label->render();
                break;
            }
            break;
        }
        return ""; 
      })
    );

    $registry->addFieldResolver('FieldDef', 'description',
      $builder->callback(function ($parent, $args) {
        $description = $parent->getDescription();
        $vartype = gettype($description);
        switch ($vartype) {
          case 'string':
            return $description;
            break;
          case 'object':
            switch (get_class($description)) {
              case 'TranslatableMarkup':
                return $description->render();
                break;
            }
            break;
        }
        return ""; 
      })
    );
  }

}

