Share Your Experience With Others

REST API Vs GraphQL API in Salesforce

If you are a Salesforce developer, you might have wondered what is the difference between Salesforce REST API and GraphQL in Salesforce.

In this blog post, we will compare these two technologies and see their pros and cons for building applications on the Salesforce platform.

We will also use an interesting use case to help you understand the concepts better. 😊

Salesforce REST API is a standard way of accessing data and functionality on Salesforce using HTTP methods such as GET, POST, PATCH, DELETE, etc.

Each resource on Salesforce has a unique URL that can be used to perform operations on it. For example, to retrieve an account record, you can use a GET request to https://yourinstance.salesforce.com/services/data/v52.0/sobjects/Account/001D000000K0fXOIAZ.

GraphQL in Salesforce is a newer way of querying and manipulating data on Salesforce using a declarative query language.

GraphQL allows you to specify exactly what fields and relationships you want to fetch from the server, and it returns only the data you need in a single request.

For example, to retrieve the same account record with its contacts and opportunities, you can use a POST request to https://yourinstance.salesforce.com/services/graphql with a query like this:

query {
account(id: “001D000000K0fXOIAZ”) {
name
industry
contacts {
name
email
phone
}
opportunities {
name
stageName
amount
}
}
}

Now, let’s see some pros and cons of using Salesforce REST API and GraphQL in Salesforce.

Pros of Salesforce REST API:

  • It is widely supported by many tools and libraries, and it is easy to integrate with other systems.
  • It follows the standard HTTP protocol, which makes it simple and familiar to use.
  • It has built-in features such as authentication, authorization, caching, versioning, etc.

Cons of Salesforce REST API:

  • It can be verbose and repetitive, requiring multiple requests to fetch related data or perform complex operations.
  • It can return more data than you need, which can affect the performance and bandwidth usage of your application.
  • It can be hard to maintain consistency and documentation across different resources and endpoints.

Pros of GraphQL in Salesforce:

  • It is flexible and expressive, allowing you to tailor your queries to your exact needs and avoid over-fetching or under-fetching data.
  • It reduces the number of requests and the amount of data transferred between the client and the server, which can improve the performance and user experience of your application.
  • It has a schema that defines the types and fields available on the server, which makes it easier to validate, document, and evolve your API.

Cons of GraphQL in Salesforce:

  • It is not as widely supported as REST API, and it may require additional tools and libraries to implement and use.
  • It does not follow the standard HTTP protocol, which may make it harder to use with some existing systems or frameworks.
  • It does not have built-in features such as authentication, authorization, caching, versioning, etc., which may require extra work or customization.

To illustrate the difference between Salesforce REST API and GraphQL in Salesforce, let’s imagine a scenario where you are building an app that shows account details with their contacts and opportunities.

You want to use either REST API or GraphQL to fetch the data from Salesforce. Here is how your app might look like:

πŸ‘©β€πŸ’» You: Hi, I’m a Salesforce developer and I want to build an app that shows account details with their contacts and opportunities. Which technology should I use: REST API or GraphQL?
πŸ€– App: Hello, I’m your app and I can help you with that. Let’s see how each technology works.

πŸ€– App: If you use REST API, you will need to make three requests to get the data you need: one for the account, one for the contacts, and one for the opportunities. Here are the URLs you will use:

GET https://yourinstance.salesforce.com/services/data/v52.0/sobjects/Account/001D000000K0fXOIAZ
GET https://yourinstance.salesforce.com/services/data/v52.0/sobjects/Account/001D000000K0fXOIAZ/contacts
GET https://yourinstance.salesforce.com/services/data/v52.0/sobjects/Account/001D000000K0fXOIAZ/opportunities

πŸ€– App: The server will return three JSON responses with all the fields for each resource. Here are some examples of what you will get:

{
“attributes”: {
“type”: “Account”,
“url”: “/services/data/v52.0/sobjects/Account/001D000000K0fXOIAZ”
},
“Id”: “001D000000K0fXOIAZ”,
“Name”: “Acme Corporation”,
“Industry”: “Manufacturing”,
“Phone”: “(415) 555-1234”,
“Website”: “www.acme.com”,
…
}

{
“totalSize”: 2,
“done”: true,
“records”: [
{
“attributes”: {
“type”: “Contact”,
“url”: “/services/data/v52.0/sobjects/Contact/003D000000Q1o2OIAS”
},
“Id”: “003D000000Q1o2OIAS”,
“Name”: “John Doe”,
“Email”: “john.doe@acme.com”,
“Phone”: “(415) 555-5678”,
…
},
{
“attributes”: {
“type”: “Contact”,
“url”: “/services/data/v52.0/sobjects/Contact/003D000000Q1o2PIAS”
},
“Id”: “003D000000Q1o2PIAS”,
“Name”: “Jane Doe”,
“Email”: “jane.doe@acme.com”,
“Phone”: “(415) 555-9012”,
…
}
]
}

{
“totalSize”: 1,
“done”: true,
“records”: [
{
“attributes”: {
“type”: “Opportunity”,
“url”: “/services/data/v52.0/sobjects/Opportunity/006D000000R1o2QIAS”
},
“Id”: “006D000000R1o2QIAS”,
“Name”: “Acme – 100 Widgets”,
“StageName”: “Closed Won”,
“Amount”: 10000,
…
}
]
}

πŸ€– App: You will need to parse the JSON responses and extract the fields you want to display in your app. You may also get some fields that you don’t need, such as the attributes object or some other fields that are not relevant for your app.

πŸ‘©β€πŸ’» You: I see. That seems a bit tedious and inefficient. Is there a better way?

πŸ€– App: Yes, there is. If you use GraphQL, you will need to make only one request to get the data you need. Here is the URL and the query you will use:

POST https://yourinstance.salesforce.com/services/graphql

query {
account(id: “001D000000K0fXOIAZ”) {
name
industry
contacts {
name
email
phone
}
opportunities {
name
stageName
amount
}
}
}

πŸ€– App: The server will return a single JSON response with only the fields and relationships you specified in your query. Here is an example of what you will get:

{
“data”: {
“account”: {
“name”: “Acme Corporation”,
“industry”: “Manufacturing”,
“contacts”: [
{
“name”: “John Doe”,
“email”: “john.doe@acme.com”,
“phone”: “(415) 555-5678”
},
{
“name”: “Jane Doe”,
“email”: “jane.doe@acme.com”,
“phone”: “(415) 555-9012”
}
],
“opportunities”: [
{
“name”: “Acme – 100 Widgets”,
“stageName”: “Closed Won”,
“amount”: 10000
}
]
}
}
}

πŸ€– App: You will not need to parse the JSON response or extract the fields you want to display in your app. You will also not get any extra data that you don’t need, such as the attributes object or some other fields that are not relevant for your app.

πŸ‘©β€πŸ’» You: Wow, that’s much better. I think I will use GraphQL for my app.

πŸ€– App: Great choice! I hope this blog post helped you understand the difference between Salesforce REST API and GraphQL in Salesforce, and their pros and cons. If you have any questions or feedback, please leave a comment below. Thanks for reading! 😊

Leave a comment