From 2175e7ae2d7dfb346a631c2eaa207241c2426f90 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Tue, 22 Dec 2020 19:28:27 +0100 Subject: [PATCH] refactored materioSchemaExtension --- .../MaterioSchemaExtension.php | 912 +++++++++--------- 1 file changed, 461 insertions(+), 451 deletions(-) diff --git a/web/modules/custom/materio_graphql/src/Plugin/GraphQL/SchemaExtension/MaterioSchemaExtension.php b/web/modules/custom/materio_graphql/src/Plugin/GraphQL/SchemaExtension/MaterioSchemaExtension.php index 27069111..8de35d03 100644 --- a/web/modules/custom/materio_graphql/src/Plugin/GraphQL/SchemaExtension/MaterioSchemaExtension.php +++ b/web/modules/custom/materio_graphql/src/Plugin/GraphQL/SchemaExtension/MaterioSchemaExtension.php @@ -26,446 +26,23 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { $this->addMateriau($registry, $builder); - // _ _ _ _ - // /_\ _ _| |_(_)__| |___ - // / _ \| '_| _| / _| / -_) - // /_/ \_\_| \__|_\__|_\___| - $registry->addFieldResolver('Query', 'article', - $builder->produce('entity_load') - ->map('type', $builder->fromValue('node')) - ->map('bundles', $builder->fromValue(['article'])) - ->map('id', $builder->fromArgument('id')) - ); + $this->addArticle($registry, $builder); - $registry->addFieldResolver('Article', 'id', - $builder->produce('entity_id') - ->map('entity', $builder->fromParent()) - ); + $this->addCompany($registry, $builder); - $registry->addFieldResolver('Article', 'uuid', - $builder->produce('entity_uuid') - ->map('entity', $builder->fromParent()) - ); + $this->addAddress($registry, $builder); - $registry->addFieldResolver('Article', 'title', - $builder->compose( - $builder->produce('entity_label') - ->map('entity', $builder->fromParent()) - )); + $this->addFilefield($registry, $builder); - $registry->addFieldResolver('Article', 'author', - $builder->compose( - $builder->produce('entity_owner') - ->map('entity', $builder->fromParent()), - $builder->produce('entity_label') - ->map('entity', $builder->fromParent()) - )); + $this->addFile($registry, $builder); - $registry->addFieldResolver('Article', 'body', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:node')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('body.value')) - ); + $this->addDate($registry, $builder); - $registry->addFieldResolver('Article', 'memo', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:node')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_memo.value')) - ); - // https://github.com/drupal-graphql/graphql/blob/8.x-4.x/doc/SUMMARY.md - // https://blog.chrismitchellonline.com/posts/custom_graphql_data/ - $registry->addFieldResolver('Article', 'linked_materials', - $builder->produce('entity_reference') - ->map('entity', $builder->fromParent()) - ->map('field', $builder->fromValue('field_linked_materials')) - ); + $this->addLink($registry, $builder); - $registry->addFieldResolver('Article', 'images', - $builder->produce('entity_reference') - ->map('entity', $builder->fromParent()) - ->map('field', $builder->fromValue('field_visuel')) - ); - - $registry->addFieldResolver('Article', 'videos', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:node')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_video')) - ); - - $registry->addFieldResolver('Article', 'tags', - $builder->produce('entity_reference') - ->map('entity', $builder->fromParent()) - ->map('field', $builder->fromValue('field_tags')) - ); - - $registry->addFieldResolver('Article', 'thesaurus', - $builder->produce('entity_reference') - ->map('entity', $builder->fromParent()) - ->map('field', $builder->fromValue('field_thesaurus')) - ); - - $registry->addFieldResolver('Article', 'showroom', - $builder->compose( - $builder->produce('entity_reference') - ->map('entity', $builder->fromParent()) - ->map('field', $builder->fromValue('field_showroom')), - $builder->callback(function ($items) { - return $items[0]; - }) - )); - - $registry->addFieldResolver('Article', 'source', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:node')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_source')) - ); - - $registry->addFieldResolver('Article', 'date', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:node')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_date')) - ); - - - // ___ - // / __|___ _ __ _ __ __ _ _ _ _ _ - // | (__/ _ \ ' \| '_ \/ _` | ' \ || | - // \___\___/_|_|_| .__/\__,_|_||_\_, | - // |_| |__/ - $registry->addFieldResolver('Query', 'company', - $builder->produce('entity_load') - ->map('type', $builder->fromValue('taxonomy_term')) - ->map('bundles', $builder->fromValue(['company'])) - ->map('id', $builder->fromArgument('id')) - ); - - $registry->addFieldResolver('Company', 'id', - $builder->produce('entity_id') - ->map('entity', $builder->fromParent()) - ); - - $registry->addFieldResolver('Company', 'uuid', - $builder->produce('entity_uuid') - ->map('entity', $builder->fromParent()) - ); - - $registry->addFieldResolver('Company', 'name', - $builder->produce('entity_label') - ->map('entity', $builder->fromParent()) - ); - - // TODO: property_path helper - // $props = [ - // [ - // "res" => "attachments", - // "type" => "entity:taxonomy_term", - // "path" => "field_attachments", - // ] - // ] - // foreach ($props as $key => $value) { - // $registry->addFieldResolver('Company', $value['res'], - // $builder->produce('property_path') - // ->map('type', $builder->fromValue('entity:taxonomy_term')) - // ->map('value', $builder->fromParent()) - // ->map('path', $builder->fromValue('field_attachments')) - // ); - // } - - $registry->addFieldResolver('Company', 'attachments', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:taxonomy_term')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_attachments')) - ); - - $registry->addFieldResolver('Company', 'memo', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:taxonomy_term')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_memo.value')) - ); - - $registry->addFieldResolver('Company', 'note', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:taxonomy_term')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_note.value')) - ); - - $registry->addFieldResolver('Company', 'website', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:taxonomy_term')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_website')) - ); - - $registry->addFieldResolver('Company', 'departement', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:taxonomy_term')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_departement.value')) - ); - - $registry->addFieldResolver('Company', 'email', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:taxonomy_term')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_public_email.value')) - ); - - $registry->addFieldResolver('Company', 'address', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:taxonomy_term')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_public_address')) - ); - - // field_infos_from_company - // field_public_phone - - - // _ _ _ - // /_\ __| |__| |_ _ ___ ______ - // / _ \/ _` / _` | '_/ -_|_-<_-< - // /_/ \_\__,_\__,_|_| \___/__/__/ - - // $addr_fields = [ - // 'langcode', - // 'country_code', - // 'administrative_area', - // 'locality', - // 'dependent_locality', - // 'postal_code', - // 'sorting_code', - // 'address_line1', - // 'address_line2', - // 'organization', - // 'given_name', - // 'additional_name', - // 'family_name' - // ]; - // - // foreach ($addr_fields as $key => $value) { - // $registry->addFieldResolver('Address', $value, - // $builder->callback(function ($parent, $args) { - // return $parent[0][$value]; - // }) - // ); - // } - - $registry->addFieldResolver('Address', 'langcode', - $builder->callback(function ($parent, $args) { - return $parent[0]['langcode']; - }) - ); - $registry->addFieldResolver('Address', 'country_code', - $builder->callback(function ($parent, $args) { - return $parent[0]['country_code']; - }) - ); - $registry->addFieldResolver('Address', 'administrative_area', - $builder->callback(function ($parent, $args) { - return $parent[0]['administrative_area']; - }) - ); - $registry->addFieldResolver('Address', 'locality', - $builder->callback(function ($parent, $args) { - return $parent[0]['locality']; - }) - ); - $registry->addFieldResolver('Address', 'dependent_locality', - $builder->callback(function ($parent, $args) { - return $parent[0]['dependent_locality']; - }) - ); - $registry->addFieldResolver('Address', 'postal_code', - $builder->callback(function ($parent, $args) { - return $parent[0]['postal_code']; - }) - ); - $registry->addFieldResolver('Address', 'sorting_code', - $builder->callback(function ($parent, $args) { - return $parent[0]['sorting_code']; - }) - ); - $registry->addFieldResolver('Address', 'address_line1', - $builder->callback(function ($parent, $args) { - return $parent[0]['address_line1']; - }) - ); - $registry->addFieldResolver('Address', 'address_line2', - $builder->callback(function ($parent, $args) { - return $parent[0]['address_line2']; - }) - ); - $registry->addFieldResolver('Address', 'organization', - $builder->callback(function ($parent, $args) { - return $parent[0]['organization']; - }) - ); - $registry->addFieldResolver('Address', 'given_name', - $builder->callback(function ($parent, $args) { - return $parent[0]['given_name']; - }) - ); - $registry->addFieldResolver('Address', 'additional_name', - $builder->callback(function ($parent, $args) { - return $parent[0]['additional_name']; - }) - ); - $registry->addFieldResolver('Address', 'family_name', - $builder->callback(function ($parent, $args) { - return $parent[0]['family_name']; - }) - ); - - // ___ _ _ __ _ _ _ - // | __(_) |___ / _(_)___| |__| | - // | _|| | / -_) _| / -_) / _` | - // |_| |_|_\___|_| |_\___|_\__,_| - $registry->addFieldResolver('Filefield', 'description', - $builder->callback(function ($parent, $args) { - return $parent['description']; - }) - ); - $registry->addFieldResolver('Filefield', 'file', - $builder->callback(function ($parent, $args) { - return \Drupal\file\Entity\File::load($parent['target_id']); - }) - ); - - // ___ _ _ - // | __(_) |___ - // | _|| | / -_) - // |_| |_|_\___| - $registry->addFieldResolver('File', 'url', - $builder->callback(function ($parent, $args) { - return $parent->url(); - }) - ); - - $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(); - }) - ); - - - // ___ _ - // | \ __ _| |_ ___ - // | |) / _` | _/ -_) - // |___/\__,_|\__\___| - $registry->addFieldResolver('Date', 'start', - $builder->callback(function ($parent, $args) { - return $parent[0]['value']; - }) - ); - $registry->addFieldResolver('Date', 'end', - $builder->callback(function ($parent, $args) { - return $parent[0]['end_value']; - }) - ); - - - // _ _ _ - // | | (_)_ _ | |__ - // | |__| | ' \| / / - // |____|_|_||_|_\_\ - $registry->addFieldResolver('Link', 'url', - $builder->callback(function ($parent, $args) { - return $parent[0]['uri']; - }) - ); - $registry->addFieldResolver('Link', 'title', - $builder->callback(function ($parent, $args) { - return $parent[0]['title']; - }) - ); - - - // ___ _ - // / __| |_ _____ __ ___ _ ___ ___ _ __ - // \__ \ ' \/ _ \ V V / '_/ _ \/ _ \ ' \ - // |___/_||_\___/\_/\_/|_| \___/\___/_|_|_| - $registry->addFieldResolver('Query', 'showroom', - $builder->produce('entity_load') - ->map('type', $builder->fromValue('taxonomy_term')) - ->map('bundles', $builder->fromValue(['showroom'])) - ->map('id', $builder->fromArgument('id')) - ); - - $registry->addFieldResolver('Showroom', 'id', - $builder->produce('entity_id') - ->map('entity', $builder->fromParent()) - ); - - $registry->addFieldResolver('Showroom', 'uuid', - $builder->produce('entity_uuid') - ->map('entity', $builder->fromParent()) - ); - - $registry->addFieldResolver('Showroom', 'name', - $builder->produce('entity_label') - ->map('entity', $builder->fromParent()) - ); - - - // _____ - // |_ _|_ _ __ _ - // | |/ _` / _` | - // |_|\__,_\__, | - // |___/ - $registry->addFieldResolver('Query', 'tag', - $builder->produce('entity_load') - ->map('type', $builder->fromValue('taxonomy_term')) - ->map('bundles', $builder->fromValue(['tags'])) - ->map('id', $builder->fromArgument('id')) - ); - - $registry->addFieldResolver('Tag', 'id', - $builder->produce('entity_id') - ->map('entity', $builder->fromParent()) - ); - - $registry->addFieldResolver('Tag', 'uuid', - $builder->produce('entity_uuid') - ->map('entity', $builder->fromParent()) - ); - - $registry->addFieldResolver('Tag', 'name', - $builder->produce('entity_label') - ->map('entity', $builder->fromParent()) - ); + $this->addShowroom($registry, $builder); + $this->addTag($registry, $builder); $this->addThesaurus($registry, $builder); @@ -480,15 +57,10 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ]); } - // __ __ _ _ // | \/ |__ _| |_ ___ _ _(_)__ _ _ _ // | |\/| / _` | _/ -_) '_| / _` | || | // |_| |_\__,_|\__\___|_| |_\__,_|\_,_| - /** - * @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry - * @param \Drupal\graphql\GraphQL\ResolverBuilder $builder - */ protected function addMateriau(ResolverRegistryInterface $registry, ResolverBuilder $builder) { $registry->addFieldResolver('Query', 'materiau', $builder->produce('entity_load') @@ -611,15 +183,118 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { } + // _ _ _ _ + // /_\ _ _| |_(_)__| |___ + // / _ \| '_| _| / _| / -_) + // /_/ \_\_| \__|_\__|_\___| + protected function addArticle(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + $registry->addFieldResolver('Query', 'article', + $builder->produce('entity_load') + ->map('type', $builder->fromValue('node')) + ->map('bundles', $builder->fromValue(['article'])) + ->map('id', $builder->fromArgument('id')) + ); + + $registry->addFieldResolver('Article', 'id', + $builder->produce('entity_id') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Article', 'uuid', + $builder->produce('entity_uuid') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Article', 'title', + $builder->compose( + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + )); + + $registry->addFieldResolver('Article', 'author', + $builder->compose( + $builder->produce('entity_owner') + ->map('entity', $builder->fromParent()), + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + )); + + $registry->addFieldResolver('Article', 'body', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('body.value')) + ); + + $registry->addFieldResolver('Article', 'memo', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_memo.value')) + ); + // https://github.com/drupal-graphql/graphql/blob/8.x-4.x/doc/SUMMARY.md + // https://blog.chrismitchellonline.com/posts/custom_graphql_data/ + $registry->addFieldResolver('Article', 'linked_materials', + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_linked_materials')) + ); + + $registry->addFieldResolver('Article', 'images', + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_visuel')) + ); + + $registry->addFieldResolver('Article', 'videos', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_video')) + ); + + $registry->addFieldResolver('Article', 'tags', + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_tags')) + ); + + $registry->addFieldResolver('Article', 'thesaurus', + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_thesaurus')) + ); + + $registry->addFieldResolver('Article', 'showroom', + $builder->compose( + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_showroom')), + $builder->callback(function ($items) { + return $items[0]; + }) + )); + + $registry->addFieldResolver('Article', 'source', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_source')) + ); + + $registry->addFieldResolver('Article', 'date', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_date')) + ); + } + // ___ // |_ _|_ __ __ _ __ _ ___ // | || ' \/ _` / _` / -_) // |___|_|_|_\__,_\__, \___| // |___/ - /** - * @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry - * @param \Drupal\graphql\GraphQL\ResolverBuilder $builder - */ protected function addImage(ResolverRegistryInterface $registry, ResolverBuilder $builder) { $registry->addFieldResolver('Image', 'id', $builder->produce('entity_id') @@ -664,15 +339,10 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { } - // __ ___ _ _ _ _ // \ \ / (_)__| |___ ___| | (_)_ _ | |__ // \ V /| / _` / -_) _ \ |__| | ' \| / / // \_/ |_\__,_\___\___/____|_|_||_|_\_\ - /** - * @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry - * @param \Drupal\graphql\GraphQL\ResolverBuilder $builder - */ protected function addVideolink(ResolverRegistryInterface $registry, ResolverBuilder $builder) { $registry->addFieldResolver('VideoLink', 'url', $builder->produce('property_path') @@ -682,15 +352,10 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ); } - // _____ _ // |_ _| |_ ___ ___ __ _ _ _ _ _ _ _ ___ // | | | ' \/ -_|_-addFieldResolver('Query', 'thesaurus', $builder->produce('entity_load') @@ -715,6 +380,351 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ); } + // _____ + // |_ _|_ _ __ _ + // | |/ _` / _` | + // |_|\__,_\__, | + // |___/ + protected function addTag(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + $registry->addFieldResolver('Query', 'tag', + $builder->produce('entity_load') + ->map('type', $builder->fromValue('taxonomy_term')) + ->map('bundles', $builder->fromValue(['tags'])) + ->map('id', $builder->fromArgument('id')) + ); + + $registry->addFieldResolver('Tag', 'id', + $builder->produce('entity_id') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Tag', 'uuid', + $builder->produce('entity_uuid') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Tag', 'name', + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + ); + } + + // ___ _ + // / __| |_ _____ __ ___ _ ___ ___ _ __ + // \__ \ ' \/ _ \ V V / '_/ _ \/ _ \ ' \ + // |___/_||_\___/\_/\_/|_| \___/\___/_|_|_| + protected function addShowroom(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + $registry->addFieldResolver('Query', 'showroom', + $builder->produce('entity_load') + ->map('type', $builder->fromValue('taxonomy_term')) + ->map('bundles', $builder->fromValue(['showroom'])) + ->map('id', $builder->fromArgument('id')) + ); + + $registry->addFieldResolver('Showroom', 'id', + $builder->produce('entity_id') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Showroom', 'uuid', + $builder->produce('entity_uuid') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Showroom', 'name', + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + ); + } + + // _ _ _ + // | | (_)_ _ | |__ + // | |__| | ' \| / / + // |____|_|_||_|_\_\ + protected function addLink(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + $registry->addFieldResolver('Link', 'url', + $builder->callback(function ($parent, $args) { + return $parent[0]['uri']; + }) + ); + $registry->addFieldResolver('Link', 'title', + $builder->callback(function ($parent, $args) { + return $parent[0]['title']; + }) + ); + } + + // ___ _ + // | \ __ _| |_ ___ + // | |) / _` | _/ -_) + // |___/\__,_|\__\___| + protected function addDate(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + $registry->addFieldResolver('Date', 'start', + $builder->callback(function ($parent, $args) { + return $parent[0]['value']; + }) + ); + $registry->addFieldResolver('Date', 'end', + $builder->callback(function ($parent, $args) { + return $parent[0]['end_value']; + }) + ); + } + + // ___ _ _ __ _ _ _ + // | __(_) |___ / _(_)___| |__| | + // | _|| | / -_) _| / -_) / _` | + // |_| |_|_\___|_| |_\___|_\__,_| + protected function addFilefield(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + $registry->addFieldResolver('Filefield', 'description', + $builder->callback(function ($parent, $args) { + return $parent['description']; + }) + ); + $registry->addFieldResolver('Filefield', 'file', + $builder->callback(function ($parent, $args) { + return \Drupal\file\Entity\File::load($parent['target_id']); + }) + ); + } + + // ___ _ _ + // | __(_) |___ + // | _|| | / -_) + // |_| |_|_\___| + protected function addFile(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + $registry->addFieldResolver('File', 'url', + $builder->callback(function ($parent, $args) { + return $parent->url(); + }) + ); + + $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 addAddress(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + // $addr_fields = [ + // 'langcode', + // 'country_code', + // 'administrative_area', + // 'locality', + // 'dependent_locality', + // 'postal_code', + // 'sorting_code', + // 'address_line1', + // 'address_line2', + // 'organization', + // 'given_name', + // 'additional_name', + // 'family_name' + // ]; + // + // foreach ($addr_fields as $key => $value) { + // $registry->addFieldResolver('Address', $value, + // $builder->callback(function ($parent, $args) { + // return $parent[0][$value]; + // }) + // ); + // } + + $registry->addFieldResolver('Address', 'langcode', + $builder->callback(function ($parent, $args) { + return $parent[0]['langcode']; + }) + ); + $registry->addFieldResolver('Address', 'country_code', + $builder->callback(function ($parent, $args) { + return $parent[0]['country_code']; + }) + ); + $registry->addFieldResolver('Address', 'administrative_area', + $builder->callback(function ($parent, $args) { + return $parent[0]['administrative_area']; + }) + ); + $registry->addFieldResolver('Address', 'locality', + $builder->callback(function ($parent, $args) { + return $parent[0]['locality']; + }) + ); + $registry->addFieldResolver('Address', 'dependent_locality', + $builder->callback(function ($parent, $args) { + return $parent[0]['dependent_locality']; + }) + ); + $registry->addFieldResolver('Address', 'postal_code', + $builder->callback(function ($parent, $args) { + return $parent[0]['postal_code']; + }) + ); + $registry->addFieldResolver('Address', 'sorting_code', + $builder->callback(function ($parent, $args) { + return $parent[0]['sorting_code']; + }) + ); + $registry->addFieldResolver('Address', 'address_line1', + $builder->callback(function ($parent, $args) { + return $parent[0]['address_line1']; + }) + ); + $registry->addFieldResolver('Address', 'address_line2', + $builder->callback(function ($parent, $args) { + return $parent[0]['address_line2']; + }) + ); + $registry->addFieldResolver('Address', 'organization', + $builder->callback(function ($parent, $args) { + return $parent[0]['organization']; + }) + ); + $registry->addFieldResolver('Address', 'given_name', + $builder->callback(function ($parent, $args) { + return $parent[0]['given_name']; + }) + ); + $registry->addFieldResolver('Address', 'additional_name', + $builder->callback(function ($parent, $args) { + return $parent[0]['additional_name']; + }) + ); + $registry->addFieldResolver('Address', 'family_name', + $builder->callback(function ($parent, $args) { + return $parent[0]['family_name']; + }) + ); + } + + + // ___ + // / __|___ _ __ _ __ __ _ _ _ _ _ + // | (__/ _ \ ' \| '_ \/ _` | ' \ || | + // \___\___/_|_|_| .__/\__,_|_||_\_, | + // |_| |__/ + protected function addCompany(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + $registry->addFieldResolver('Query', 'company', + $builder->produce('entity_load') + ->map('type', $builder->fromValue('taxonomy_term')) + ->map('bundles', $builder->fromValue(['company'])) + ->map('id', $builder->fromArgument('id')) + ); + + $registry->addFieldResolver('Company', 'id', + $builder->produce('entity_id') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Company', 'uuid', + $builder->produce('entity_uuid') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Company', 'name', + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + ); + + // TODO: property_path helper + // $props = [ + // [ + // "res" => "attachments", + // "type" => "entity:taxonomy_term", + // "path" => "field_attachments", + // ] + // ] + // foreach ($props as $key => $value) { + // $registry->addFieldResolver('Company', $value['res'], + // $builder->produce('property_path') + // ->map('type', $builder->fromValue('entity:taxonomy_term')) + // ->map('value', $builder->fromParent()) + // ->map('path', $builder->fromValue('field_attachments')) + // ); + // } + + $registry->addFieldResolver('Company', 'attachments', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_attachments')) + ); + + $registry->addFieldResolver('Company', 'memo', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_memo.value')) + ); + + $registry->addFieldResolver('Company', 'note', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_note.value')) + ); + + $registry->addFieldResolver('Company', 'website', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_website')) + ); + + $registry->addFieldResolver('Company', 'departement', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_departement.value')) + ); + + $registry->addFieldResolver('Company', 'email', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_public_email.value')) + ); + + $registry->addFieldResolver('Company', 'address', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_public_address')) + ); + + // field_infos_from_company + // field_public_phone + } + /** * Resolves the response type. *