I Have created a module to add custom attribute to createCustomer mutation.
Vendor/Module/etc/extension_attributes.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
<attribute code="custom_attr" type="string"/>
</extension_attributes>
Vendor/Module/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Customer\Api\Data\CustomerInterface">
<arguments>
<argument name="extensionAttributesObjects" xsi:type="array">
<item name="custom_attr" xsi:type="string">Vendor\Module\Api\Data\CustomerExtensionInterface</item>
</argument>
</arguments>
</type>
Vendor\Module\Api\Data\CustomerExtensionInterface.php
<?php
namespace Vendor\Module\Api\Data;
interface CustomerExtensionInterface
{
/**
* Get customAttr
*
* @return string|null
*/
public function getCustomAttr(): ?string;
/**
* Set custom_attr
*
* @param string|null $custom_attr
* @return $this
*/
public function setCustomAttr(?string $custom_attr): self;
}
Now I am trying this request in createCustomer Mutattion
mutation {
createCustomer(
input: {
firstname: "test"
lastname: "test"
email: "[email protected]"
is_subscribed: true
custom_attr: "test value"
}
) {
customer {
id
firstname
lastname
email
}
}
}
Note: created the "custom_attr" customer attribute with data patch.
Getting below error
"message": "Field "custom_attr" is not defined by type CustomerInput.",
Can someone help me on this how to custom attribute in the create customer mutation in graphQL.
1 Answer 1
To extend existed GraphQL schema you need to redefine the needed type/interface/input like we do with XML schema in Magento.
First things first you should create the schema.graphqls file in your custom module under etc directory.
In this file you need to redefine the CustomerInput input type (your custom input option will be merged with already defined input options in Magento):
If you are using Magento version 2.4.6 please replace
CustomerInputwithCustomerCreateInputand for mutation usecreateCustomerV2
input CustomerInput {
custom_attr: String
}