I have basic knowledge about GraphQL. Now, I would like to convert my knowledge into action. For that, I want to know about few things like how can I add my custom field to any GraphQL Query and mutations. I have also read some blog posts for this but they don't have proper guidance which I am looking for.

Can someone please explain with example of extending an existing GraphQL API for both Query and Mutations? That would be really handy.

Thanks in advance!

  • Do you expecting existing queries & mutation extend or creating custom queries and Mutation? – Mujahidh May 18 at 8:04
  • @Mujahidh, I am expecting to extend/override existing (default magento) queries and mutations. – Pratik Navapara May 18 at 9:35
  • Did you check my answer for your question? – Mujahidh May 22 at 4:51
  • 1
    @Mujahidh, Sorry for late response. I was busy with other stuff. I checked your detailed response. It looks fine and I think it will help me as well. I upvoted your reply. Cheers! – Pratik Navapara Jun 19 at 13:12

One of the possible ways of extending existing GraphQl is using the GraphQl interface. As for example, you have a new product attribute barcode now you want it in GraphQl response. In that case in your custom module's etc > schema.graphqls file add as following

interface ProductInterface {
  barcode: String @resolver (class: "Vendor\\Module\\Model\\Resolver\\ProductCustomAttribute")

After implementation flush the cache. You will be get your desire result.

| improve this answer | |
  • Thanks for your reply. It is useful for interface. What about query and mutations? How can I add new fields in those type of api calls? – Pratik Navapara May 6 at 7:57

We can do our custom endpoints too. I have extend some existing default graphql queries.

1 . Query storeConfig -( module-store-graph-ql )

2 . Query products's ProductFilterInput - ( module-catalog-graph-ql )

1 . Query storeConfig -( module-store-graph-ql )

I extended storeConfig using following steps. create a di.xml file under module's etc/graphql directory and define the new items like below.

<?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\StoreGraphQl\Model\Resolver\Store\StoreConfigDataProvider">
        <argument name="extendedConfigData">
<!-- Default store config sets-->
    <item name="general_store_information_name" xsi:type="string">general/store_information/name</item>
    <item name="general_store_information_additional_phone" xsi:type="string">general/store_information/additional_phone</item>
    <!-- Custom store config sets-->
    <item name="contact_us_forms_title" xsi:type="string">contact_us/forms/title</item>
    <item name="contact_us_forms_info_title" xsi:type="string">contact_us/forms/info_title</item>

and write the schema.graphqls file

type StoreConfig {
general_store_information_name : String  @doc(description: "Store Name"),
general_store_information_phone : String  @doc(description: "Store Phone Number "),
contact_us_forms_title : String  @doc(description: "contact Us form title text"),
contact_us_forms_info_title : String  @doc(description: "contact Us information section title text")

After setup:upgrade and chache:clean you can run the graphql query like below to get the result.

storeConfig {

For better understanding read How to get the store configuration values using GraphQl in magento 2?

2 . Query products's ProductFilterInput - ( module-catalog-graph-ql ) for this section please read magento 2 graphql product query

All these customization i did based on my requirements with personally modification. It's worked out for me. Hope this will help you and some one who read this.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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