Introducing the Salesforce GraphQL API

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

Over the past decade, REST has become the standard (yet a fuzzy one) for designing web APIs. It offers some great ideas, such as stateless servers and structured access to resources. However, REST APIs have shown to be too inflexible to keep up with the rapidly changing requirements of the clients that access them.

GraphQL was developed to cope with the need for more flexibility and efficiency! It solves many of the shortcomings and inefficiencies that developers experience when interacting with REST APIs.

let’s consider a simple example scenario: In a blogging application, an app needs to display the titles of the posts of a specific user. The same screen also displays the names of the last 3 followers of that user. How would that situation be solved with REST and GraphQL?

Data Fetching with REST vs GraphQL

With a REST API, you would typically gather the data by accessing multiple endpoints. In the example, these could be /users/<id> endpoint to fetch the initial user data. Secondly, there’s likely to be a /users/<id>/posts endpoint that returns all the posts for a user. The third endpoint will then be the /users/<id>/followers that returns a list of followers per user.

In GraphQL on the other hand, you’d simply send a single query to the GraphQL server that includes the concrete data requirements. The server then responds with a JSON object where these requirements are fulfilled.

Benefits to salesforce developers

GraphQL is a new way to address many pain points for application developers; all GraphQL servers adhere to a specification, which features patterns that make it easier to build complex experiences across a wide variety of platforms and devices.

Salesforce GraphQL API is available in EnterprisePerformanceUnlimited, and Developer Editions. It adheres to the June 2018 version of the spec.

The following benefits are available for Salesforce developers, including:

  • Field selection
  • Resource aggregation
  • Schema introspection

The first of these patterns is field selection, which requires the client generating the GraphQL query to explicitly declare which fields they would like to receive in the response. This technique reduces the size of the payload, as the client only receives fields that were included in the query.

The second pattern is aggregation, which allows the client to specify a traversal across the fields in the GraphQL schema. Aggregations reduce round trips between the client and server, returning a set of related resources within a single response.

The final pattern is introspection, which is the ability to send a query to the GraphQL server to ask questions about the types, fields, and their relationships within the schema. When all these patterns are combined, it results in more responsive, higher-quality applications.