Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Thursday, May 29
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»GraphQL Query Generator in Salesforce Apex

    GraphQL Query Generator in Salesforce Apex

    Dhanik Lal SahniBy Dhanik Lal SahniSeptember 30, 2023Updated:January 15, 2025No Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    GraphQL Query Generator
    Share
    Facebook Twitter LinkedIn Pinterest Email

    GraphQL can retrieve data from multiple sources in a single query. This will reduce the number of HTTP requests, reducing latency and boosting performance, particularly in mobile and low-bandwidth devices. To retrieve data we have to create SQOL in GraphQL format. In this post, we will build a GraphQL Query Generator in Salesforce Apex that will give us a valid GraphQL query. We will also use generated SOQL to retrieve data using GraphQL API.

    To explore GraphQL in Salesforce you can check our other post Exploring GraphQL API in Salesforce.

    Steps to create a GraphQL query generator

    1. Create Apex Classes for Query Generator
    2. Use Generate GraphQL Query to retrieve data

    1. Create Apex Classes for Query Generator

    To use GraphQL API, we have to pass a valid GraphQL query. Below is a basic query to retrieve Account records data. Only the ID and Name fields will be retrieved.

    Let us create classes that will generate a query similar to the above query at runtime based on given parameters.

    Apex Class Detail

    GraphQLArgument

    This class will create the filter condition for a graphQL query. Example is Account(where:{Name:{eq:"Demo App Test"}})

    GraphQLNode

    This class will create an object node and add fields and conditions to the query.

    GraphQLQuery

    This class will generate a query for a given object and fields.

    2. Use Generate GraphQL Query to retrieve data

    Let us use the above classes and get data from GraphQL API. To call graphQL we need to generate an authentication token and then we can call GraphQL API to retrieve records.

    GraphQL API Url depends on the org used. The sample URL will be https://vagminecodex-dev-ed.my.salesforce.com/services/data/v57.0/graphql. We can generate a GraphQL API URL at runtime using an instance URL which is generated when the access token is generated.

    Class GraphQLController is doing the below steps

    1. Generating an authentication token
    2. Creating a GraphQL query and
    3. Calling GraphQL API to retrieve records using the query

    To get the client ID and secret used in the token API call, you need to create a connected app in Salesforce org. You can refer post Generate Salesforce Authentication Token using Postman to create connected app.

    ExternalCallout is a generic class to call external API. You can take this class code from the post Generic Apex class for Calling External System

    Test Output

    The above code will generate a response similar to the below image.

    GraphQL Query Generator - SalesforceCodex

    Expected Errors

    You might get errors like [Unexpected character (‘0’ (code 48)): was expecting comma to separate OBJECT entries at [line:1, column:80] or Validation error of type FieldUndefined: Field ‘query’ in type ‘Query’ is undefined @ ‘query’. These errors are related to invalid JSON format of GraphQL query so try to remove unwanted characters from the query which will be generated at runtime.

    Note: Add API URLs in the Remote Site Setting or create a Named Credential.

    References:

    apex-graphql-query

    Exploring GraphQL API in Salesforce

    GraphQL API

    Other Related Posts

    Displaying Tabular Data with GraphQL in Lightning Web Component

    GraphQL Query Generator in Salesforce Apex

    Similar Posts

    Low Code Integration for Text Translation using Systran API

    Extract Demographic Detail using Trestle Reverse Phone API

    Verify Phone in Salesforce using VeriPhone API

    Named Entity Recognition using Salesforce Einstein API

    AWS Signature 4 Signing in Salesforce

    Upload File to AWS S3 Server

    Need Help?

    Need some kind of help in implementing this feature, connect on my LinkedIn profile Dhanik Lal Sahni.

    apex api integration GraphQL GraphQL API GraphQL API benefits GraphQL API in Salesforce GraphQL Query GraphQL Query generator GraphQL Query Salesforce rest api salesforce salesforce api integration Salesforce Graph API Salesforce GraphQL Salesforce GraphQL API
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleCapture Images in Salesforce using Mobile App
    Next Article Integrate Pexel API for Seamless Image Retrieval
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni4 Mins Read

    How to Build a Generic Modal Window in Lightning Web Component

    May 26, 2025
    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.