1

In magento2 - I have created one extension attribute for customer using plugin, it's working fine for Rest API also. How can I include this extension attribute in GraphQL?

In createCustomer mutation , all the CustomerInput are added. Can anyone help me in adding an extension attribute to this mutation?

8
  • Did you try to pass that attribute in the CustomerInput, i am asking this based on the product Attribute, If we create a custom product attribute we can get the value by just passing the attribute_code in the query.
    – Mujahidh
    Commented Oct 16, 2019 at 5:24
  • How can I extend the CustomerInput in vendor ?
    – Kavi
    Commented Oct 16, 2019 at 5:35
  • Can you just elaborate on this?
    – Kavi
    Commented Oct 16, 2019 at 5:36
  • 1
    you just pass your new attribute in CustomerInput and see, but currently CustomerInput only have prefix,firstname,middlename,lastname,suffix,email,dob,taxvat,gender,password,is_subscribed fields.if passing the attribute to the mutation not work you have to write your own module to add the attribute and do some work arround in Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomer in your own module.
    – Mujahidh
    Commented Oct 16, 2019 at 5:43
  • study the module-customer-graph-ql in vendor/magento directory.
    – Mujahidh
    Commented Oct 16, 2019 at 5:44

4 Answers 4

4

Magento 2.4

Try This Code

app/code/VendoreName/ModuleName/etc

schema.graphqls

type Customer {
  sample_attribute: String @doc(description: "Customer Custom Attribute Show")
    @resolver(
      class: "\\VendoreName\\ModuleName\\Model\\Resolver\\GetCustomerCustomAttr"
    )
}

input CustomerInput {
  sample_attribute: String @doc(description: "Customer Custom Attribute Val")
}

app/code/VendoreName/ModuleName/Model/Resolver

GetCustomerCustomAttr.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare (strict_types = 1);

namespace VendoreName\ModuleName\Model\Resolver;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
 * Customer custom attribute field resolver
 */
class GetCustomerCustomAttr implements ResolverInterface
{
    /**
     * @var Magento\Customer\Api\CustomerRepositoryInterface
     */
    protected $customerRepositoryInterface;

    /**
     * @param Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
     */
    public function __construct(
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
    ) {
        $this->customerRepositoryInterface = $customerRepositoryInterface;
    }

    /**
     * @inheritdoc
     */
    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        if (!isset($value['model'])) {
            throw new LocalizedException(__('"model" value should be specified'));
        }
        /** @var CustomerInterface $customer */
        $customer = $value['model'];
        $customerId = (int) $customer->getId();
        $customerData = $this->customerRepositoryInterface->getById($customerId);

        /* Get customer custom attribute value */
        if ($customer->getCustomAttribute('sample_attributess')) {
            $customerAttributeVal = $customer->getCustomAttribute('sample_attributess')->getValue();
        } else {
            $customerAttributeVal = null;
        }

        return $customerAttributeVal;
    }
}

Get customer custom attribute value

{
  customer {
    email
    sample_attribute
  }
}

Update customer custom attribute value

mutation {
  updateCustomer(
    input: {
      firstname: "Rob",
      sample_attribute: "Text"
    }
  ) {
    customer {
      firstname
      sample_attribute
    }
  }
}
1
2

For fetching custom attributes data in your customer graphql api you just need to add the attribute into your schema.graphqls file in following way

  type Customer 
{
    mobile_number: String
    customer_city: String
    customer_state: String
    customer_country: String
}

and pass query as

{
  customer {
    firstname
    lastname
    email
    mobile_number
    customer_city
    customer_state
    customer_country
  }
  
}

It will automatically fetch your custom attributes data.

Hope this helps you.

Thank you ..

0

All you have to do is re-define the 'CustomerInput' parameter (with your custom extension attribute) of the createCustomer mutation.

In your module's schema.graphqls file, re-define the 'CustomerInput' type.

input CustomerInput {
    your_extention_attr: String
}

Then run the below commands.

rm -rf generation/

bin/magento c:f

Now you can parse this new parameter in createCuatomer mutation. See the example below.

mutation {
  createCustomer(
    input: {
      firstname: "Bob"
      lastname: "Loblaw"
      email: "[email protected]"
      password: "b0bl0bl@w"
      is_subscribed: true
      your_extention_attr: "this is a test value"
    }
  ) {
    customer {
      id
      firstname
      lastname
      email
      is_subscribed
    }
  }
}
1
  • 1
    this way not working for me... Commented Dec 17, 2019 at 5:43
0

in your schema.graphqls use interface eg I have added these customer attributes

 interface createCustomer{
    mobile_no: String
        @doc(description: "Name of attribute set assigned to the product")
        @resolver(class: "\\CompanyName\\ModuleName\\Model\\Resolver\\CreateCustomer"),
    b2b_category: String
        @doc(description: "Name of attribute set assigned to the product")
        @resolver(class: "\\CompanyName\\ModuleName\\Model\\Resolver\\CreateCustomer"),
    gstin: String
        @doc(description: "Name of attribute set assigned to the product")
        @resolver(class: "\\CompanyName\\ModuleName\\Model\\Resolver\\CreateCustomer"),
    customer_code: String
        @doc(description: "Name of attribute set assigned to the product")
        @resolver(class: "\\CompanyName\\ModuleName\\Model\\Resolver\\CreateCustomer"),
    customer_discount_rate: String
        @doc(description: "Name of attribute set assigned to the product")
        @resolver(class: "\\CompanyName\\ModuleName\\Model\\Resolver\\CreateCustomer")
}

input CustomerInput {
    mobile_no: String
    b2b_category: String
    gstin: String
    customer_code: String
    customer_discount_rate: String
}

Now create a file CompanyName\ModuleName\Model\Resolver\CreateCustomer.php in your CreateCustomer.php create CreateCustomer class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.