From 665a55074829a94443e0c0b2f33b5f1e5e057a16 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Tue, 22 Dec 2020 22:00:12 +0100 Subject: [PATCH] improving graphql defs --- .../graphql/materio_extension.base.graphqls | 24 ++-- .../MaterioSchemaExtension.php | 113 +++++++++++++++--- 2 files changed, 112 insertions(+), 25 deletions(-) diff --git a/web/modules/custom/materio_graphql/graphql/materio_extension.base.graphqls b/web/modules/custom/materio_graphql/graphql/materio_extension.base.graphqls index 28bfe4e..43c3b70 100644 --- a/web/modules/custom/materio_graphql/graphql/materio_extension.base.graphqls +++ b/web/modules/custom/materio_graphql/graphql/materio_extension.base.graphqls @@ -17,10 +17,10 @@ type Materiau { attachments: [Filefield] distributor: [Company] manufacturer: [Company] - # famille: - # index: - # reference: - # samples: + # famille: String + # index: Int + reference: String + samples: [Sample] } type Article { @@ -40,6 +40,11 @@ type Article { memo: String } +type Sample { + showroom: Showroom + location: String +} + type Filefield { file: File! description: String @@ -64,15 +69,16 @@ type Showroom { uuid: String! name: String! images: [Image] - # email: String - # address: Adress - # phone: String + email: String + address: Address + phone: String } type Company { id: Int! uuid: String! name: String! + description: String attachments: [Filefield] memo: String note: Int @@ -80,8 +86,8 @@ type Company { departement: String email: String address: Address - # infos - # phone + phone: String + infos: String } type Tag { 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 8de35d0..3f83c5f 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,6 +26,8 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { $this->addMateriau($registry, $builder); + $this->addSample($registry, $builder); + $this->addArticle($registry, $builder); $this->addCompany($registry, $builder); @@ -175,14 +177,55 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ->map('field', $builder->fromValue('field_distributor')) ); - // field_famille - // field_index - // field_reference - // field_samples + // $registry->addFieldResolver('Materiau', 'famille', + // $builder->produce('property_path') + // ->map('type', $builder->fromValue('entity:node')) + // ->map('value', $builder->fromParent()) + // ->map('path', $builder->fromValue('field_famille.value')) + // ); + // + // $registry->addFieldResolver('Materiau', 'index', + // $builder->produce('property_path') + // ->map('type', $builder->fromValue('entity:node')) + // ->map('value', $builder->fromParent()) + // ->map('path', $builder->fromValue('field_index.value')) + // ); + + $registry->addFieldResolver('Materiau', 'reference', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_reference.value')) + ); + + $registry->addFieldResolver('Materiau', 'samples', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_samples')) + ); } + // ___ _ + // / __| __ _ _ __ _ __| |___ + // \__ \/ _` | ' \| '_ \ / -_) + // |___/\__,_|_|_|_| .__/_\___| + // |_| + protected function addSample(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + $registry->addFieldResolver('Sample', 'showroom', + $builder->callback(function($parent, $args){ + return \Drupal\taxonomy\Entity\Term::load($parent['target_id']); + }) + ); + $registry->addFieldResolver('Sample', 'location', + $builder->callback(function($parent, $args){ + return $parent['location']; + }) + ); + } + // _ _ _ _ // /_\ _ _| |_(_)__| |___ // / _ \| '_| _| / _| / -_) @@ -435,6 +478,28 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { $builder->produce('entity_label') ->map('entity', $builder->fromParent()) ); + + $registry->addFieldResolver('Showroom', '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('Showroom', 'address', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_public_address')) + ); + + $registry->addFieldResolver('Showroom', 'phone', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_public_phone.value')) + ); + } // _ _ _ @@ -626,7 +691,6 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ); } - // ___ // / __|___ _ __ _ __ __ _ _ _ _ _ // | (__/ _ \ ' \| '_ \/ _` | ' \ || | @@ -641,19 +705,25 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ); $registry->addFieldResolver('Company', 'id', - $builder->produce('entity_id') - ->map('entity', $builder->fromParent()) - ); + $builder->produce('entity_id') + ->map('entity', $builder->fromParent()) + ); $registry->addFieldResolver('Company', 'uuid', - $builder->produce('entity_uuid') - ->map('entity', $builder->fromParent()) - ); + $builder->produce('entity_uuid') + ->map('entity', $builder->fromParent()) + ); $registry->addFieldResolver('Company', 'name', - $builder->produce('entity_label') - ->map('entity', $builder->fromParent()) - ); + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Company', 'description', + $builder->callback(function($parent, $args){ + return $parent->getDescription(); + }) + ); // TODO: property_path helper // $props = [ @@ -721,8 +791,19 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ->map('path', $builder->fromValue('field_public_address')) ); - // field_infos_from_company - // field_public_phone + $registry->addFieldResolver('Company', 'phone', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_public_phone.value')) + ); + + $registry->addFieldResolver('Company', 'infos', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:taxonomy_term')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_infos_from_company.value')) + ); } /**