TransWikia.com

GraphQL and Salesforce Development

Salesforce Asked by Tekill on November 25, 2021

I recently started looking into GraphQL as I have been experimenting with ReactJS. In all my readings I have not really seen anything about GraphQL and Salesforce. I am wondering if there would be any benefit in using GraphQL when developing a visual-force page within SF?

I can’t seem to find any articles about that, I was wondering if someone could enlighten me as to the piece of the puzzle I may be missing.

Would having a GraphQL engine between your visual-force page and your apex controller yield any benefit or would it just be unnecessary fluff?

2 Answers

Yes, there are many benefits to use GraphQL on Salesforce. Some of the benefits are summarised in this reply (and could apply also on VisualForce development but they depend on the specific use case).

Note: this detailed answer is about native GraphQL support on the Salesforce platform (with no mediations). More about GraphQL and Salesforce, in general, can be found on respective resources. The author of the answer is in direct relation to the described product as a developer.

Introduction

GraphQL is a query language for APIs, developed by Facebook and used since 2012., which provides a complete and understandable description of the data in the underlying system. GraphQL specification is open from 2015. and can be found at GraphQL Foundation web This disruptive technology provides the clients with a more efficient way to query the required data in a single request. Accessible on a single endpoint a GraphQL offers to the clients a way to select a specific kind of required data, aggregated in a defined structure, and deep as much as needed. GraphQL is providing great business benefits like:

  • Less Code
    • need for less code as developers can get only what they need (in required and available structure)
  • Rapid Development
    • developers can make changes on the client-side without impacting server
  • Less Total Cost of Ownership
    • less code implies less maintenance
  • Shorter Time to Market
    • quicker development reduces initial costs and time required for go-live
  • Visibility on Available Data Structure / Strong Type
    • with Introspection capability developers can see what they are able to use
  • Fewer Requests
    • A huge benefit for cloud-based systems (usually having various API limits)
  • Reduced Data Fetching Time
    • batching requests saves time spent on roundtrips and data bulkification improves performance

With some of the known advantages for developers:

  • Dynamic Field Selection
    • Developers can dynamically select the exact fields needed by their component
  • Query Aggregation
    • Single query removes the need for multiple API calls
  • Client-Defined Filtration
    • Expressive filtering that eliminates the need to supply record IDs
  • Introspection
    • Programmatic discovery of the Salesforce graph through introspection
  • Raw performance
    • Several orders of magnitude faster than traditional REST
  • Development speed
    • Configuration friendly, easily customizable, virtually no initial setup and maintenance cost

How does GraphQL execution work?

For starters, GraphQL uses a simple concept for how it changes its queries into results. It traverses the query field by field, executing ‘resolvers’ for each field. If a parent resolver function is required by a child, the parent query will resolve first and pass its result to the child for use in its own query. Finally, once the execution algorithm is done, it forms all the data into the correct shape and returns it.

Why GraphQL on the Salesforce?

Salesforce is a big and great platform with a vast of different API options (like REST API, Bulk API, Streaming API, SOAP API, and so on). All these options are good to use in specific use cases and exist by purpose, but Salesforce is a cloud platform and as such it has different limits (like a number of allowed API calls) to prevent rogue scripts to use dedicated resources and to protect the customer instances. So optimizing queries seems also not just a benefit for different trusted data consumers but also it is a benefit for the server, a Salesforce Org (and might have a direct implication on the commercials, as depending on available licenses different limits apply).

Maven Integration Platform (with native GraphQL support)

With a Maven Integration Platform (MIP) a GraphQL language with Queries and standard Salesforce object schema are natively supported and available directly on a Salesforce via existing REST API, exposed on the dedicated endpoint. Currently, MIP is the only fully native package that has a full complaint GraphQL parser available in AppExchange. Because the MIP is a native package it doesn't require any other mediation service, it is queried directly as any other standard Salesforce REST API endpoint (or via Apex API). More about setup and querying can be found at official MIP documentation. Let us summarise some of the benefits of native GraphQL support on the Salesforce platform:

  • Built natively on Salesforce
    • no additional systems or services required
  • Extends existing Salesforce API
    • Extends native and well-known Salesforce capabilities
  • Dynamic Salesforce schema resolution
    • All queries are resolved on the fly and existing Salesforce schema is used (including standard, managed, or custom objects)
  • Standard Salesforce security model is enforced
    • Standard Salesforce security model capabilities are used and consumer connection permissions are enforced
  • Easy setup
    • Less initial setup and maintenance cost (as standard Salesforce Admins and Developers can do the whole work)
  • Plug and Play approach
    • core functionalities are ready immediately after deployment / no configuration
  • Introspection of standard Salesforce schema
    • GraphQL introspection is supported

Answered by Dragan Vujnovic on November 25, 2021

GraphQL is an open source data query and manipulation language meant to provide a more efficient, powerful and flexible alternative to REST and ad-hoc web service architectures.

In practice it acts as an abstraction layer between a client (web, mobile, etc) and a backend (REST or database). The intent is to unify and abstract all communication between these components.

Exposing Salesforce REST services in GraphQL and querying them from a Javascript client included in VisualForce would only introduce an unnecessary and under-performing layer because you can directly access code from the controller.

But there are other use cases where GraphQL may prove beneficial in conjunction with the Salesforce REST services: - a backend where web or mobile clients need to access both Salesforce REST APIs, a database or other unrelated REST APIs.

You can start using this schema https://github.com/jpmonette/salesforce-graphql and enrich it with your Salesforce objects.

Answered by Marius Toth on November 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP