• Admin

Salesforce + GraphQL

Updated: Jun 25, 2018

Part 1: Building APIs with GraphQL and Salesforce

This is the first instalment in a two-part series, exploring and understanding the capabilities of GraphQL with Salesforce and building a simple Query and Mutation to provide the basic building blocks of developing a GraphQL-Salesforce solution.


Over the course of this series, we will talk about:

  • What is GraphQL?

  • What are the benefits of GraphQL to Salesforce?

  • How do we build GraphQL for Salesforce?

  • Setting up nForce as a Salesforce Connected App

  • Setting up GraphQL

  • Building Queries

  • Building Mutations

  • Enhancing Scalability


What is GraphQL?

GraphQL is a query language for APIs and an execution engine that provides a simple and effective abstraction between its data sources and the requester of the data. It provides a simple syntax system which is easy to learn, human-friendly and flexible enough to be considered a replacement or used in conjunction with the REST framework.

GraphQL's data sources can be as simple as a hard-coded Strings or as complex as multiple databases running in conjunction with various platforms and data services.

One of the major differences between GraphQL and the REST framework is that GraphQL transactions are called from a single endpoint while REST endpoints can differ depending on the nature of the resource and the purpose of the transaction. As an example, for a REST method to GET a single account, we need to send a request to the following endpoint (i.e. https://example.com/accounts/1). If we were to request for another account, we'll need to send a request to another endpoint (i.e. https://example.com/accounts/2) or just query all accounts (i.e. https://example.com/accounts) then look for account IDs 1 and 2. In GraphQL, we only call one endpoint (i.e. https://example.com/graphql) and provide a GraphQL query designed to the specifications of the data requester.

Below are examples of GraphQL queries for retrieving accounts:

query getFirstAccount {
    account(id: "1") {
        name
        description
    } 
}
query getSecondAccount {
    account(id: "2") {
        name
        description
    } 
}
query getAllAccounts {
    accounts {
        name
        description
    } 
}

The GraphQL query syntax looks very close to JSON syntax. The only difference is that it does not represent a key-value pair but rather it represents the operation, the object name and the fields that the user requested.

It is also worth taking note that the response data of a REST endpoint for a GET request will always output all the fields in its response schema even if the user does not need them. In GraphQL, the request will output only the fields that the requester defined in the query.

So how does the requester know about the possible fields to query then? This is where the GraphQL tooling comes into play. When the option is enabled in its configuration, GraphQL's endpoint is actually a user interface when fired up in a browser.



The GraphiQL interface provides a Document Explorer on the right side of the screen which is searchable and easy to navigate. The interface also provides an area on the left side of the screen to test GraphQL queries. This is a very intuitive tool and relatively simple to operate.

To learn more about GraphQL queries, the official documentation can be found at http://graphql.org/learn/

What are the benefits of GraphQL to Salesforce?

When it comes to Salesforce, the biggest buzzword is adoption. That is because the Salesforce platform is very customisable, easy to learn and has a plethora of guided tutorials, detailed documentation and massive support of community following.

Majority of Salesforce platform adopters may have other systems in their infrastructure which may come with data that they need to use in conjunction with Salesforce and they need to invoke these data in the structure that they are most comfortable with. The power of GraphQL lies in its ability to shape its data responses from any number of data sources while its implementation can be written in any programming language. This provides a lot of flexibility for developers as they can work with the language they are most comfortable with.

It is worth taking note that while Salesforce is taking the world by storm, there has been an increase of usage in native mobile platforms, modernised single page applications and tons of use cases for the internet of things. The obvious fact in all of these is that each technology requires the means to consume the data. With GraphQL, consuming the data will not be an issue as its community has spawned a number of solutions to serve every platform. For example, in React and React Native applications, we can use the Apollo GraphQL client to request GraphQL queries and mutations, and use them alongside React components.

Ultimately, GraphQL will not only provide a gateway for Salesforce data to be served on mobile and IoT platforms, it will also help increase the adoption of Salesforce.

That's all for now... keep an eye out for Part 2 where we use GraphQL with Salesforce!

#beamav3rik


- Glenn -

447 views

Recent Posts

See All

Async Component in LWC

Sameed is back at it with a blog post on the application of Lightning Web Components (LWC) to build out an asynchronous component that will be useful in addressing a fairly common real world scenario

  • LinkedIn - Black Circle
  • Twitter - Black Circle
  • Instagram - Black Circle

© 2020 | mav3rik.com