0

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.

asked Mar 29, 2023 at 9:54

1 Answer 1

0

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 CustomerInput with CustomerCreateInput and for mutation use createCustomerV2

input CustomerInput {
 custom_attr: String
}
answered Mar 29, 2023 at 12:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.