Kaizen 149 : Using GraphQL APIs to fetch data in a consolidated way

Kaizen 149 : Using GraphQL APIs to fetch data in a consolidated way



Hello everyone!
Welcome to this week's post in the Kaizen Series! This week, we will discuss GraphQL APIs, a query language that provides an efficient, powerful, and flexible method for fetching and manipulating data from Zoho CRM.
Let us consider the scenario of a large industrial equipment manufacturer,  Zylker Manufacturing.  They utilize Zoho CRM to streamline their sales processes and customer relationship management. Their sales support team uses a legacy system to manage their activities. When a contact seeks support, the support team must have access to the latest data from Zoho CRM to assist them better. They need:
  • details of the contact, such as email, phone, and Account details
  • details of the ongoing deals of the contact, including potential revenue and stages .
With GraphQL APIs, with a single API call, you can create custom unified view that combine information from multiple Zoho CRM modules. This allows the support team to view all relevant information in one place, making it easier to manage sales processes and customer interactions. To create the view, metadata of the fields is also required.  This structured approach in GraphQL allows Zylker Manufacturing to efficiently aggregate data.


You need to construct a GraphQL query to fetch specific data related to a contact and associated deals, as well as metadata about the fields in the Contacts and Deals modules. Let's break down each part of the query that can achieve this.

1. Fetching Contact and Deal Information


  • Records: Records comprises the records from the different Zoho CRM modules.
  • Contacts: Specifies that we want to query the records of the Contacts module. The where clause filters the contacts based on their email address.
  • _data: This indicates the fields we want to retrieve from the records. In this case:
    • Email: Fetches the email of the contact.
    • Full_Name: Fetches the full name of the contact.
    • Deals__r: Fetches the related deal record of the Contact. 
      • Expected_Revenue: Fetches the expected revenue from each deal.
      • Deal_Name: Fetches the name of each deal.
      • Stage: Fetches the current stage of each deal.


Records {
    Contacts(where: { Email: { equals: "krismarrier@noemail.com" } }) {
        _data {
            Email {
                value}
            Full_Name {
                value}
            Deals__r {
                _data {
                    Expected_Revenue {
                        value}
                    Deal_Name {
                        value}
                    Stage {
                        value
                    }}}}}}




2. Fetching Metadata for Contacts


  • Meta: This fetches the details of the metadata for Contacts module. It uses the alias "contact_meta"
  • Modules(filter: { api_name: "Contacts" }): Specifies that we want to fetch the metadata of the Contacts module.
  • _data: This indicates the fields we want to retrieve. In this case:
    • id: The unique identifier for the module.
    • api_name: The API name for the module.
    • module_name: The name of the module.
    • description: The description of the module.
    • plural_label, singular_label: Labels used for the module.
    • fields: Retrieves metadata for specific fields within the module. filter: { api_names: ["Last_Name", "Email"] }: Specifies which fields' metadata to retrieve.For each field, it retrieves:
      • id: Field identifier.
      • api_name: API name for the field.
      • display_label: The display name of the field.
      • json_type: The data type as represented in JSON.
      • data_type: The Zoho CRM data type of the field.
 contact_meta: Meta {
    Modules(filter: { api_name: "Contacts" }) {
      _data {
        id
        api_name
        module_name
        description
        singular_label
        plural_label
        fields(filter: { api_names: ["Last_Name", "Email"] }) {
          _data {
            id
            api_name
            display_label
            json_type
            data_type
          }
        }
      }
    }
 }

3. Fetching Metadata for Deals


  • Meta: This fetches the details of the metadata for Deals module. It uses the alias "deal_meta"
  • Modules(filter: { api_name: "Deals" }): Specifies that we want to fetch the metadata of the Deals module.
  • _data: This indicates the fields we want to retrieve. In this case:
    • id: The unique identifier for the module.
    • api_name: The API name for the module.
    • module_name: The name of the module.
    • description: description of the module.
    • plural_label, singular_label: Labels used for the module.
    • fields: Retrieves metadata for specific fields within the module.
    • filter:  api_names: ["Expected_Revenue", "Deal_Name","Stage"] }: Specifies which fields' metadata to retrieve.For each field, it retrieves:
      • id: Field identifier.
      • api_name: API name for the field.
      • display_label: The display name of the field.
      • json_type: The data type as represented in JSON.
      • data_type: The Zoho CRM data type of the field.
deal_meta: Meta {
    Modules(filter: { api_name: "Deals" }) {
        _data {
            id
            api_name
            module_name
            description
            singular_label
            plural_label
            fields(filter: { api_names: ["Expected_Revenue", "Deal_Name","Stage"] }) {
                _data {
                    api_name
                    id
                    display_label
                    json_type
                    data_type
                }
            }
        }
    }
}

The complete query will look this:


query {
  Records {
    Contacts(where: { Email: { equals: "krismarrier@noemail.com" } }) {
      _data {
        Email {
          value
        }
        Full_Name {
          value
        }
        Deals__r {
          _data {
            Expected_Revenue {
              value
            }
            Deal_Name {
              value
            }
            Stage {
              value
            }
          }
        }
      }
    }
  }
  contact_meta: Meta {
    Modules(filter: { api_name: "Contacts" }) {
      _data {
        plural_label
        id
        api_name
        module_name
        description
        singular_label
        fields(filter: { api_names: ["Last_Name", "Email"] }) {
          _data {
            id
            api_name
            display_label
            json_type
            data_type
          }
        }
      }
    }
  }
  deal_meta: Meta {
    Modules(filter: { api_name: "Deals" }) {
      _data {
        plural_label
        id
        api_name
        module_name
        description
        singular_label
        fields(
          filter: { api_names: ["Expected_Revenue", "Deal_Name", "Stage"] }
        ) {
          _data {
            api_name
            id
            display_label
            json_type
            data_type
          }
        }
      }
    }
  }
}


If you were to fetch this data using REST APIs, it will involve multiple calls to the 
  • Query API
  • Related Records API
  • Modules meta API, and 
  • Fields meta API.
Using the GraphQL query you can fetch the required data alone in a less round trip time. While constructing a query for your custom requirement, please note that you can query up to three levels of depth (nesting levels of a field) for Records and 7 levels of depth for Metadata. 

Constructing GraphQL query using Postman

To construct a GraphQL query in Postman to suit your requirements, type {api_domain}/crm/graphql in the URL box. Postman will automatically fetch the query schema in the schema explorer, which can be used to explore the types of queries that can be made, including what fields are available on each type, what arguments those fields accept, and what other types they return.   
Constructing a Zoho CRM GraphQL query in Postman using schema explorer



You can refer to the help documentation on GraphQL APIs for more details on GraphQL APIs. 

Update 25th Sep 2024:
GraphQL APIs are now open across all DCs including IN DC for Enterprise, Zoho One Enterprise, CRM Plus and Ultimate edition orgs. Please note that GraphQL APIs are not available for Trial Edition of these editions.



We hope you found this post useful. We will meet you next week with another interesting topic!
If you have any questions, let us know in the comment section or reach out to us directly at support@zohocrm.com.

Further Reading.                                                                                                                                



    Access your files securely from anywhere

        Zoho Developer Community




                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now