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
- Create Apex Classes for Query Generator
- 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
- Generating an authentication token
- Creating a GraphQL query and
- 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.
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:
Exploring GraphQL API in Salesforce
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
Need Help?
Need some kind of help in implementing this feature, connect on my LinkedIn profile Dhanik Lal Sahni.