From 5deb568786cdedccba020e6e9912497cba5011a8 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Tue, 22 Dec 2020 18:17:02 +0100 Subject: [PATCH] improving graphql defs, refactoring graphql defs --- .../graphql/materio_extension.base.graphqls | 36 +- .../materio_extension.extension.graphqls | 4 + .../MaterioSchemaExtension.php | 540 +++++++++++++----- 3 files changed, 425 insertions(+), 155 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 eb71a314..28bfe4e4 100644 --- a/web/modules/custom/materio_graphql/graphql/materio_extension.base.graphqls +++ b/web/modules/custom/materio_graphql/graphql/materio_extension.base.graphqls @@ -15,8 +15,8 @@ type Materiau { thesaurus: [Thesaurus] memo: String attachments: [Filefield] - # distributor: - # manufacturer: + distributor: [Company] + manufacturer: [Company] # famille: # index: # reference: @@ -69,6 +69,21 @@ type Showroom { # phone: String } +type Company { + id: Int! + uuid: String! + name: String! + attachments: [Filefield] + memo: String + note: Int + website: Link + departement: String + email: String + address: Address + # infos + # phone +} + type Tag { id: Int! uuid: String! @@ -81,6 +96,23 @@ type Thesaurus { name: String! } +type Address { + langcode: String + country_code: String + administrative_area: String + locality: String + dependent_locality: String + postal_code: String + sorting_code: String + address_line1: String + address_line2: String + organization: String + given_name: String + additional_name: String + family_name: String +} + + type Date { start: String end: String diff --git a/web/modules/custom/materio_graphql/graphql/materio_extension.extension.graphqls b/web/modules/custom/materio_graphql/graphql/materio_extension.extension.graphqls index ccc63637..90e5c423 100644 --- a/web/modules/custom/materio_graphql/graphql/materio_extension.extension.graphqls +++ b/web/modules/custom/materio_graphql/graphql/materio_extension.extension.graphqls @@ -17,3 +17,7 @@ extend type Query { extend type Query { thesaurus(id: Int!): Thesaurus } + +extend type Query { + company(id: Int!): Company +} 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 14e73af8..27069111 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 @@ -24,121 +24,7 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { public function registerResolvers(ResolverRegistryInterface $registry) { $builder = new ResolverBuilder(); - - // __ __ _ _ - // | \/ |__ _| |_ ___ _ _(_)__ _ _ _ - // | |\/| / _` | _/ -_) '_| / _` | || | - // |_| |_\__,_|\__\___|_| |_\__,_|\_,_| - $registry->addFieldResolver('Query', 'materiau', - $builder->produce('entity_load') - ->map('type', $builder->fromValue('node')) - ->map('bundles', $builder->fromValue(['materiau'])) - ->map('id', $builder->fromArgument('id')) - ); - - $registry->addFieldResolver('Materiau', 'id', - $builder->produce('entity_id') - ->map('entity', $builder->fromParent()) - ); - - $registry->addFieldResolver('Materiau', 'uuid', - $builder->produce('entity_uuid') - ->map('entity', $builder->fromParent()) - ); - - $registry->addFieldResolver('Materiau', 'title', - $builder->compose( - $builder->produce('entity_label') - ->map('entity', $builder->fromParent()) - )); - - $registry->addFieldResolver('Materiau', 'body', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:node')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('body.value')) - ); - - $registry->addFieldResolver('Materiau', 'short_description', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:node')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_short_description.value')) - ); - - $registry->addFieldResolver('Materiau', 'author', - $builder->compose( - $builder->produce('entity_owner') - ->map('entity', $builder->fromParent()), - $builder->produce('entity_label') - ->map('entity', $builder->fromParent()) - )); - - // https://github.com/drupal-graphql/graphql/blob/8.x-4.x/doc/SUMMARY.md - // https://blog.chrismitchellonline.com/posts/custom_graphql_data/ - - $registry->addFieldResolver('Materiau', 'linked_materials', - $builder->compose( - $builder->produce('entity_reference') - ->map('entity', $builder->fromParent()) - ->map('field', $builder->fromValue('field_linked_materials')) - )); - - $registry->addFieldResolver('Materiau', 'linked_articles', - $builder->compose( - $builder->produce('entity_reference') - ->map('entity', $builder->fromParent()) - ->map('field', $builder->fromValue('field_linked_articles')) - )); - - $registry->addFieldResolver('Materiau', 'memo', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:node')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_memo.value')) - ); - - $registry->addFieldResolver('Materiau', 'images', - $builder->produce('entity_reference') - ->map('entity', $builder->fromParent()) - ->map('field', $builder->fromValue('field_materiau_images')) - ); - - $registry->addFieldResolver('Materiau', 'attachments', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:node')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_attachments')) - // $builder->produce('entity_reference') - // ->map('entity', $builder->fromParent()) - // ->map('field', $builder->fromValue('field_attachments')) - ); - - $registry->addFieldResolver('Materiau', 'videos', - $builder->produce('property_path') - ->map('type', $builder->fromValue('entity:node')) - ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('field_video')) - ); - - $registry->addFieldResolver('Materiau', 'tags', - $builder->produce('entity_reference') - ->map('entity', $builder->fromParent()) - ->map('field', $builder->fromValue('field_tags')) - ); - - $registry->addFieldResolver('Materiau', 'thesaurus', - $builder->produce('entity_reference') - ->map('entity', $builder->fromParent()) - ->map('field', $builder->fromValue('field_thesaurus')) - ); - - // field_distributor - // field_famille - // field_index - // field_manufacturer - // field_reference - // field_samples + $this->addMateriau($registry, $builder); // _ _ _ _ // /_\ _ _| |_(_)__| |___ @@ -246,25 +132,217 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ); - // ___ _ _ - // | __(_) |___ - // | _|| | / -_) - // |_| |_|_\___| + // ___ + // / __|___ _ __ _ __ __ _ _ _ _ _ + // | (__/ _ \ ' \| '_ \/ _` | ' \ || | + // \___\___/_|_|_| .__/\__,_|_||_\_, | + // |_| |__/ + $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->produce('entity_reference') - // ->map('entity', $builder->fromParent()) - // ->map('field', $builder->fromValue('field_showroom')) $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(); @@ -389,50 +467,160 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ); - // _____ _ - // |_ _| |_ ___ ___ __ _ _ _ _ _ _ _ ___ - // | | | ' \/ -_|_-addFieldResolver('Query', 'thesaurus', + $this->addThesaurus($registry, $builder); + + $this->addVideolink($registry, $builder); + + $this->addImage($registry, $builder); + + // Response type resolver. + $registry->addTypeResolver('Response', [ + __CLASS__, + 'resolveResponse', + ]); + } + + + // __ __ _ _ + // | \/ |__ _| |_ ___ _ _(_)__ _ _ _ + // | |\/| / _` | _/ -_) '_| / _` | || | + // |_| |_\__,_|\__\___|_| |_\__,_|\_,_| + /** + * @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') - ->map('type', $builder->fromValue('taxonomy_term')) - ->map('bundles', $builder->fromValue(['thesaurus'])) + ->map('type', $builder->fromValue('node')) + ->map('bundles', $builder->fromValue(['materiau'])) ->map('id', $builder->fromArgument('id')) ); - $registry->addFieldResolver('Thesaurus', 'id', + $registry->addFieldResolver('Materiau', 'id', $builder->produce('entity_id') ->map('entity', $builder->fromParent()) ); - $registry->addFieldResolver('Thesaurus', 'uuid', + $registry->addFieldResolver('Materiau', 'uuid', $builder->produce('entity_uuid') ->map('entity', $builder->fromParent()) ); - $registry->addFieldResolver('Thesaurus', 'name', - $builder->produce('entity_label') - ->map('entity', $builder->fromParent()) - ); + $registry->addFieldResolver('Materiau', 'title', + $builder->compose( + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + )); - - // __ ___ _ _ _ _ - // \ \ / (_)__| |___ ___| | (_)_ _ | |__ - // \ V /| / _` / -_) _ \ |__| | ' \| / / - // \_/ |_\__,_\___\___/____|_|_||_|_\_\ - $registry->addFieldResolver('VideoLink', 'url', + $registry->addFieldResolver('Materiau', 'body', $builder->produce('property_path') - ->map('type', $builder->fromValue('field_item:video_embed_field')) + ->map('type', $builder->fromValue('entity:node')) ->map('value', $builder->fromParent()) - ->map('path', $builder->fromValue('value')) + ->map('path', $builder->fromValue('body.value')) ); + $registry->addFieldResolver('Materiau', 'short_description', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_short_description.value')) + ); - // ___ - // |_ _|_ __ __ _ __ _ ___ - // | || ' \/ _` / _` / -_) - // |___|_|_|_\__,_\__, \___| - // |___/ + $registry->addFieldResolver('Materiau', 'author', + $builder->compose( + $builder->produce('entity_owner') + ->map('entity', $builder->fromParent()), + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + )); + + // https://github.com/drupal-graphql/graphql/blob/8.x-4.x/doc/SUMMARY.md + // https://blog.chrismitchellonline.com/posts/custom_graphql_data/ + + $registry->addFieldResolver('Materiau', 'linked_materials', + $builder->compose( + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_linked_materials')) + )); + + $registry->addFieldResolver('Materiau', 'linked_articles', + $builder->compose( + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_linked_articles')) + )); + + $registry->addFieldResolver('Materiau', 'memo', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_memo.value')) + ); + + $registry->addFieldResolver('Materiau', 'images', + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_materiau_images')) + ); + + $registry->addFieldResolver('Materiau', 'attachments', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_attachments')) + ); + + $registry->addFieldResolver('Materiau', 'videos', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_video')) + ); + + $registry->addFieldResolver('Materiau', 'tags', + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_tags')) + ); + + $registry->addFieldResolver('Materiau', 'thesaurus', + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_thesaurus')) + ); + + $registry->addFieldResolver('Materiau', 'manufacturer', + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_manufacturer')) + ); + + $registry->addFieldResolver('Materiau', 'distributor', + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('field_distributor')) + ); + + // field_famille + // field_index + // field_reference + // field_samples + + } + + + // ___ + // |_ _|_ __ __ _ __ _ ___ + // | || ' \/ _` / _` / -_) + // |___|_|_|_\__,_\__, \___| + // |___/ + /** + * @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') ->map('entity', $builder->fromParent()) @@ -474,11 +662,57 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ->map('style', $builder->fromValue('article_card_medium')) ); - // Response type resolver. - $registry->addTypeResolver('Response', [ - __CLASS__, - 'resolveResponse', - ]); + } + + + // __ ___ _ _ _ _ + // \ \ / (_)__| |___ ___| | (_)_ _ | |__ + // \ 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') + ->map('type', $builder->fromValue('field_item:video_embed_field')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('value')) + ); + } + + + // _____ _ + // |_ _| |_ ___ ___ __ _ _ _ _ _ _ _ ___ + // | | | ' \/ -_|_-addFieldResolver('Query', 'thesaurus', + $builder->produce('entity_load') + ->map('type', $builder->fromValue('taxonomy_term')) + ->map('bundles', $builder->fromValue(['thesaurus'])) + ->map('id', $builder->fromArgument('id')) + ); + + $registry->addFieldResolver('Thesaurus', 'id', + $builder->produce('entity_id') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Thesaurus', 'uuid', + $builder->produce('entity_uuid') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Thesaurus', 'name', + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + ); } /**