Generate the types using npm run graphql:codegen. The command generates the types in src/api/graphql-operations-types.ts. For example:
Response type: GetMyDataQuery
Variables type: GetMyDataQueryVariables
Implement the data access function.
1import{createDataSDK, type NodeOfConnection}from "@salesforce/platform-sdk/data";2import MY_QUERY from "./query/myQuery.graphql?raw";3import type{GetMyDataQuery, GetMyDataQueryVariables}from "../graphql-operations-types";45type MyNode = NodeOfConnection<GetMyDataQuery["uiapi"]["query"]["MyObject"]>;67export async function getMyData(variables: GetMyDataQueryVariables): Promise<MyNode[]>{8 const dataSdk = await createDataSDK();9 const result = await dataSdk.graphql?.query<GetMyDataQuery, GetMyDataQueryVariables>({10 query: MY_QUERY,11 variables,12});1314 if(result?.errors?.length){15 const errorMessages = result.errors.map((e)=> e.message).join("; ");16 throw new Error(`GraphQL Error: ${errorMessages}`);17}1819 return result?.data?.uiapi?.query?.MyObject?.edges?.map((edge)=> edge?.node) || [];20}
Export the GraphQL Return Type
To provide type safety for GraphQL operations, define your return type. These TypeScript interfaces define the shape of data flowing in (query variables) and out (query results) of your GraphQL operations.
Export the interfaces so they can be imported and reused across components. You can also use graphql-codegen to generate the interfaces for the interfaces to stay in sync with your schema. Use the return types in your methods and variables.
GraphQL fragments are reusable units of query logic that are used to define a set of fields.
This example uses GraphQL fragments and directives to keep one query reusable while conditionally including fields.
FinancialFields and ContactFields are reusable field groups for Account.
In the main query, each fragment is attached with @include(if: $...):
When using GraphQL via the Data SDK, consider these usage patterns.
Direct API calls aren’t supported. Always use the Data SDK to make your API calls.
1// Don't do this: Direct axios/fetch for Salesforce APIs2const result = await axios.post("/graphql", {query});3const result = await fetch("/services/data/v{version}/...");4// Do this: Use the DataSDK5const dataSdk = await createDataSDK();6const result = await dataSdk.graphql?.query<ResponseType>({query, variables});7const result = await dataSdk.fetch?.("/services/data/v{version}/...");
Provide the response type instead of making untyped GraphQL calls.
1// Don't do this: Untyped GraphQL calls2await dataSdk.graphql?.query({query});3// Do this: Provide response type generic4await dataSdk.graphql?.query<GetAccountsQuery>({query});
Use the gql template tag or an external .graphql file instead of plain string queries.
1// Don't do this: Plain string queries2const query = `query { ... }`;34// Do this: Use gql tag or .graphql file5const query = gql`query { ... }`;6// or do this: Import the query from an external .graphql file7import QUERY from "./query/myQuery.graphql?raw";
Use optional chaining instead of non-null assertion on a GraphQL call.
1// Don't do this: Non-null assertion on graphql2await dataSdk.graphql!.query<T>({query});3// Do this: Optional chaining4await dataSdk.graphql?.query<T>({query});