Salesforce GraphQL: Developer Insights
What is GraphQL?
GraphQL is a standard query language for APIs and a runtime for fulfilling those queries with your data. It provides users with a single endpoint to request all necessary data from in a single request. Applications that utilise GraphQL APIs are frequently far more efficient than those that stick with REST APIs.
This is due to their capacity to retrieve all required data in a single invocation, which reduces the number of round trips to the server, allowing you to customise it to meet your needs.
Why GraphQL is better than REST API?
REST API is the gold standard for APIs, yet traditional REST APIs present significant issues when developing modern, highly performant, and scalable online and mobile applications. REST endpoints do not allow client changes to the API endpoint's response.
For example, when fetching a user's phone number from a REST API, receiving extraneous data beyond the number adds unnecessary costs for both the server and the requester. "Field selection" is a feature of GraphQL that allows you to include all the fields in your query that you are interested in and only get the data for those fields.
Features supported by new Salesforce GraphQL API
1. Field selection
2. Resource aggregation
3. Schema introspection
4. sObject queryability
Note: As of now mutation (write operation) is not supported. As of now it supports read or querying data from salesforce.
Salesforce is working on a GraphQL Wire Adapter (It is in Beta) that will bring native GraphQL support to Lightning Web Components (LWC). Additionally, they intend to introduce these new features in 2025.
How to use GraphQL API
To invoke the API, make a POST request to the {{urlBase}}/services/data/{{version}}/graphql endpoint in your LWC. or use the wire services that LWC offers to run a graphql query straight from LWC. refer to the example below.
GraphQL API respects Object Level Security (OLS) and Field Level Security (FLS) settings of current user while fetching.
GraphQL Schema
The core of the schema is split up into a few top-level kinds. Query and mutation types are used by GraphQL schemas to conduct reads and writes, respectively. Only the Query type is now included in the Salesforce schema because the schema is read-only.
query accounts {
uiapi {
query {
Account {
edges {
node {
Id
Name {
value
}
}
}
}
}
}
}
Understanding the structure of graphQL query:
query accounts : Top level type to execute read. "accounts" is name given to your GraphQL query. It's how you refer to this specific query if you have multiple queries defined.
uiapi: Root Field. It serves as the entry point to the UI API Schema. querying against an API called uiapi.
query: Sub-Field. Accessing the query sub-field.
Account: Nested Field. Accessing the Account field to query for account information.
edges & nodes: Edges and Nodes. `edges` & `nodes` represents pagination. Each node represents an individual account.
Fields: Inside each node, we are fetching two fields. ID and name.
Field Arguments: There are no arguments provided in this query. Arguments are parameters that can be passed to fields to filter or specify the data you want to retrieve.
Example:
In LWC
import { gql, graphql } from "lightning/uiGraphQLApi";
@wire(graphql, {query: gql`
query accounts {
uiapi {
query {
Account {
edges {
node {
Id
Name {
value
}
}
}
}
}
}
}
`,
})
graphqlQueryResult({ data, errors }) {
if (data) {
this.results = data.uiapi.query.Account.edges.map((edge) => edge.node);
}
this.errors = errors;
}
Output:blogger
Comments
Post a Comment