From 830f5a5909e968a483abc299efb5768b4a52e9f4 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Mon, 4 Jan 2021 21:44:13 +0100 Subject: [PATCH] added commerce's products and variations queries to graphql --- .../graphql/materio_extension.base.graphqls | 31 ++++ .../materio_extension.extension.graphqls | 16 ++ .../MaterioSchemaExtension.php | 164 ++++++++++++++++++ 3 files changed, 211 insertions(+) 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 93daf2a..9c57bce 100644 --- a/web/modules/custom/materio_graphql/graphql/materio_extension.base.graphqls +++ b/web/modules/custom/materio_graphql/graphql/materio_extension.base.graphqls @@ -72,6 +72,37 @@ type SearchResult { reference: String } +type Product { + id: Int! + uuid: String! + title: String + bundle: String + body: String + path: String + variations: [Variation] +} + +type Variation { + id: Int! + uuid: String! + title: String + description: String + subscription_type: String + sku: String + price: Price + product_id: Int + # list_price + # billing_schedule + # license_expiration + # license_type + # field_multiple +} + +type Price { + value: Int + currency: String +} + type Sample { showroom: Showroom location: 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 ea7557d..a43b1ba 100644 --- a/web/modules/custom/materio_graphql/graphql/materio_extension.extension.graphqls +++ b/web/modules/custom/materio_graphql/graphql/materio_extension.extension.graphqls @@ -37,3 +37,19 @@ extend type Query { extend type Query { company(id: Int!): Company } + +extend type Query { + product(id: Int!): Product +} + +extend type Query { + products(ids: [Int]): [Product] +} + +# extend type Query { +# variation(id: Int!): Variation +# } +# +# extend type Query { +# variations(id: [Int]): Variation +# } 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 c65cbd6..d2b95e5 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 @@ -41,6 +41,12 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { $this->addCompany($registry, $builder); + $this->addProduct($registry, $builder); + + $this->addVariation($registry, $builder); + + $this->addPrice($registry, $builder); + $this->addAddress($registry, $builder); $this->addFilefield($registry, $builder); @@ -1075,6 +1081,164 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase { ); } + protected function addProduct(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + $registry->addFieldResolver('Query', 'product', + $builder->produce('entity_load') + ->map('type', $builder->fromValue('commerce_product')) + // ->map('bundles', $builder->fromValue(['materiau'])) + ->map('id', $builder->fromArgument('id')) + ); + + $registry->addFieldResolver('Query', 'products', + $builder->produce('entity_load_multiple') + ->map('type', $builder->fromValue('commerce_product')) + // ->map('bundles', $builder->fromValue(['materiau'])) + ->map('ids', $builder->fromArgument('ids')) + ); + + $registry->addFieldResolver('Product', 'id', + $builder->produce('entity_id') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Product', 'uuid', + $builder->produce('entity_uuid') + ->map('entity', $builder->fromParent()) + ); + + + $registry->addFieldResolver('Product', 'bundle', + $builder->produce('entity_bundle') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Product', 'path', + $builder->compose( + $builder->produce('entity_url') + ->map('entity', $builder->fromParent()), + $builder->produce('url_path') + ->map('url', $builder->fromParent()) + ) + ); + + $registry->addFieldResolver('Product', 'title', + $builder->compose( + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + )); + + $registry->addFieldResolver('Product', 'body', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('body.value')) + ); + + $registry->addFieldResolver('Product', 'variations', + $builder->produce('entity_reference') + ->map('entity', $builder->fromParent()) + ->map('field', $builder->fromValue('variations')) + ); + } + + protected function addVariation(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + // $registry->addFieldResolver('Query', 'product', + // $builder->produce('entity_load') + // ->map('type', $builder->fromValue('commerce_product_variation')) + // // ->map('bundles', $builder->fromValue(['materiau'])) + // ->map('id', $builder->fromArgument('id')) + // ); + + // $registry->addFieldResolver('Query', 'product', + // $builder->produce('entity_load_multiple') + // ->map('type', $builder->fromValue('product')) + // // ->map('bundles', $builder->fromValue(['materiau'])) + // ->map('ids', $builder->fromArgument('ids')) + // ); + + $registry->addFieldResolver('Variation', 'id', + $builder->produce('entity_id') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Variation', 'uuid', + $builder->produce('entity_uuid') + ->map('entity', $builder->fromParent()) + ); + + + $registry->addFieldResolver('Variation', 'bundle', + $builder->produce('entity_bundle') + ->map('entity', $builder->fromParent()) + ); + + $registry->addFieldResolver('Variation', 'path', + $builder->compose( + $builder->produce('entity_url') + ->map('entity', $builder->fromParent()), + $builder->produce('url_path') + ->map('url', $builder->fromParent()) + ) + ); + + $registry->addFieldResolver('Variation', 'title', + $builder->compose( + $builder->produce('entity_label') + ->map('entity', $builder->fromParent()) + )); + + $registry->addFieldResolver('Variation', 'description', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:commerce_product_variation')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_description.value')) + ); + + $registry->addFieldResolver('Variation', 'subscription_type', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:commerce_product_variation')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('subscription_type.value')) + ); + + $registry->addFieldResolver('Variation', 'sku', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:commerce_product_variation')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('sku.value')) + ); + + $registry->addFieldResolver('Variation', 'price', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:commerce_product_variation')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('price')) + ); + + // product_id + // list_price + // billing_schedule + // license_expiration + // license_type + // field_multiple + } + + protected function addPrice(ResolverRegistryInterface $registry, ResolverBuilder $builder) { + + $registry->addFieldResolver('Price', 'value', + $builder->compose( + $builder->callback(function($parent, $args){ + return $parent[0]['number']; + }) + )); + + $registry->addFieldResolver('Price', 'currency', + $builder->compose( + $builder->callback(function($parent, $args){ + return $parent[0]['currency_code']; + }) + )); + } /** * Resolves the response type. *