Salesforce GraphQL

Salesforce requires most of the time multiple integrations with other systems (e.g. ERP, Billing, Marketing, etc.), and GraphQL API is a relatively new API approach which makes integrations way easier.

Salesforce Rest API is Good

The Salesforce Rest API is easy to understand and use. Basically, you can set a REST API client (for each programming language a library exists for it, for example for JavaScript it is jsforce), retrieve data or metadata, and then insert/update them back to Salesforce. Getting and setting the data with REST API work fine but the integration requires more than these. Since every system has its own unique data structure and name convention, we need to make multiple data retrieve operations and transform them into the desired structure with a required name convention then interact with other systems data. If we have multiple integrations on multiple services then we need to duplicate these steps for each of them.

GraphQL is Better

Duplicating the work multiple times is not the best practice. In order to reduce the work, we need to make our integrations dynamic. In this content, GraphQL comes into consideration. It is a relatively new concept of API to retrieve and upsert data. For instance, in your integration, if you require to retrieve Accounts with Contacts and Accounts Tasks in a JSON format, then you can either set service on the Salesforce side or make multiple API calls to get the records and transform them in desired name conversion and structure. GraphQL provides this out of the box. For example this GraphQL query:

query {
 Customer: Account(Status__c:'Active') {
   AccountId: Id
   Name
   Industry
   Status__c
   CustomerUser: Contact {
    ContactId: Id
       Name
   }
   Task{
    Id
       Subject
   }
 }
}

will return the accounts with contacts and tasks in the same structure above as it is requested. You can also set an alias for object and field names.

GraphSQL is Best

To achieve this we need to implement a class to parse the requests and response in the same structure. Appitek has already implemented GraphQL as a native Salesforce package called GraphSQL which is fully free and well documented. Personally, I appreciate the work they have done. It makes things really easier and fun.

A Sample

As the sample request above you can query it either in the playground of GraphSQL or in a REST client of Salesforce.

Playground

REST Client:

Request:

curl ${INSTANCE_URL}/services/apexrest/aptk_graphql/graphql -X POST -H "Content-Type: application/json" -H "Authorization: Bearer ${ACCESS_TOKEN}" -H "X-PrettyPrint:1" -d "{ \"query\": \"query { Customer: Account(Status__c:'Active') { AccountId: Id Name Industry Status__c CustomerUser: Contact { ContactId: Id Name } Task{ Id Subject}}}\" }" -s

Response :

"{\"queries\":[\"SELECT Id, Name, Industry, Status__c FROM Account WHERE Status__c = 'Active'\",\"SELECT Id, Name, AccountId FROM Contact WHERE AccountId IN ('0011i00000TTHKeAAP') \",\"SELECT Id, Subject, WhatId FROM Task WHERE WhatId IN ('0011i00000TTHKeAAP') \"],\"mutations\":null,\"errors\":[],\"data\":{\"Customer\":[{\"Task\":[{\"Subject\":\"Call\",\"Id\":\"00T1i00000o3MEGEA2\"}],\"CustomerUser\":[{\"Name\":\"test test\",\"ContactId\":\"0031i00000TOBtTAAX\"},{\"Name\":\"testt3 test 3\",\"ContactId\":\"0031i00000TOBulAAH\"},{\"Name\":\"test 4 test 4\",\"ContactId\":\"0031i00000TOC6XAAX\"}],\"Status__c\":\"Active\",\"Industry\":null,\"Name\":\"Trial 2\",\"AccountId\":\"0011i00000TTHKeAAP\"}]}}"

Aykut Parlak
Aykut Parlak

Lead Salesforce Developer

Articles: 3

One comment

  1. Thanks.How is it different from Composite API where also one can executes a series of REST API requests in a single call?

Leave a Reply

Your email address will not be published. Required fields are marked *