Query Objects

AVAILABLE API VERSION
API v56.0 and later
Compound fields available in API v59.0 and later
Optional fields available in API v65.0 and later

Use queries to load objects, such as Accounts or Contacts. The query field under the uiapi field is of RecordQuery type and gives you access to all object types your Salesforce user has access to. Only objects supported by UI API are available for querying.

Object Access Schema 

The context user’s object-level and field-level permissions control access to supported objects and fields on those objects. An object or field is accessible if the user has the correct permissions, and the object is either on the supported object list or is a custom object.

Starting in API v68.0, admins can control unauthenticated guest user access to GraphQL from Setup. Unauthenticated guest user access is turned off by default in new orgs. To update guest user access, from Setup, in the Quick Find box, enter API Access Controls and select it, and then update guest user access. In orgs with guest user access, GraphQL API honors the same object-level and field-level permissions that guest users have in the org. In orgs with guest user access turned off, guest users can’t access the GraphQL endpoint and receive errors when making requests. For more information about guest user access, see Securely Share Your Exerience Cloud Sites with Guest Users.

To construct queries that succeed even if the context user doesn’t have access to a field, use optional fields.

RecordQuery Type 

The RecordQuery type has one field for each object type accessible to the user. Only Account is shown here for brevity.

RecordQuery Specification
1type RecordQuery {
2  Account( # request the "Account" field to query "Account" objects
3    after: String
4    first: Int
5    orderBy: Account_OrderBy
6    scope: ACCOUNT_SCOPE
7    updateMRU: Boolean
8    upperBound: Int
9    where: Account_Filter
10  ): AccountConnection
11}

The RecordQuery type contains these fields.

  • after - Returns the results after the given cursor. See Paginate Results.
  • first - Returns the first n results. See Paginate Results.
  • orderBy - Orders the query using a specified field. See Order Results.
  • scope - An enumeration of the possible scopes that are defined for the object type. Provide this argument to filter the result set to records that have the specified scope. Relay Record Connections don’t have a scope argument, as child relationships can’t be filtered by a scope.
  • updateMRU - If set to true, updates the LastViewedDate field on the records that your query returns. Results for queries with the updateMRU field are the same as those without the field. To see the updated last viewed dates, make another query that includes the LastViewedDate field. The updateMRU field is available in API v66.0 and later.
  • upperBound - Specifies the number of records by which to limit the query. Allows pagination up to upperBound number of records. Valid values are 200 to 2000.
  • where - Filters the query with an argument. See Field Operators.

RecordConnection Type 

The Account field returns the AccountConnection type, which represents a result set of records that ties together the records and page info. AccountConnection ties together the elements of the result set (the edges) with information about the relative position of the returned edges to all possible results. See Paginate Results.

RecordConnection Type
1type AccountConnection {
2  edges: [AccountEdge]
3  pageInfo: PageInfo!
4  totalCount: Int!
5  pageResultCount: Int!
6}

The RecordConnection type contains these fields.

  • edges - A list of RecordEdge types.
  • pageInfo - Information about the relative location in the result set.
  • totalCount - The number of records that are queried.
  • pageResultCount - The number of records in the page of results. Available in API v60.0 and later.

RecordEdge Type 

The RecordEdge type, represented by AccountEdge here, ties together the record and its cursor.

RecordEdge Type
1type AccountEdge {
2  cursor: String!
3  node: Account
4}

The RecordEdge type contains these fields.

  • cursor - An opaque string used to page directly to this item in the result set.
  • node - The GraphQL object equivalent for the Salesforce object. For supported sObjects that are added before API v60.0, the node type name and sObject type name are the same. To avoid name collisions, supported sObjects that are added in API v60.0 and later have a node type name with a _Record suffix. For example, FeedItem is added in API v60.0 and its node type name is FeedItem_Record.

PageInfo Type 

The PageInfo type contains relative position information, which shows where in the entire result set the current page is located. The PageInfo type is the same for every Connection type.

PageInfo Type
1type PageInfo {
2  hasNextPage: Boolean
3  hasPreviousPage: Boolean
4  startCursor: String
5  endCursor: String
6}

The PageInfo type contains these fields.

  • hasNextPage - Specifies whether the result set have a next page
  • hasPreviousPage - Specifies whether the result set have a previous page
  • startCursor - An opaque string used to page to the first record in the result set
  • endCursor - An opaque string used to page to the last record in the result set

For more information about pagination, see Paginate Results.

Tip

Mapping sObjects to GraphQL Objects 

Each supported sObject type the user has access to maps to a corresponding GraphQL object type. For supported sObjects that are added before API v60.0, the sObject type and the corresponding GraphQL object type have the same name. That is, Account maps to a GraphQL object type named Account, or MyCustomObject__c maps to MyCustomObject__c. To avoid name collisions, supported sObjects that are added in API v60.0 and later map to a GraphQL object type name with a _Record suffix. For example, FeedItem is added in API v60.0 and maps to a GraphQL object type named FeedItem_Record.

All object types implement the Record interface, which has a standard set of fields that are useful for most applications.

For a complete list of fields on each object type, we recommend that you review the schema documentation using the Altair GraphQL client.

Tip

Record Interface
1interface Record {
2  Id: ID!
3  ApiName: String!
4  WeakEtag: Long!
5  DisplayValue: String
6  LastModifiedById: IDValue
7  LastModifiedDate: DateTimeValue
8  SystemModStamp: DateTimeValue
9  RecordTypeId: IDValue
10}

Standard fields on the Record interface includes:

  • Id - The record ID.
  • ApiName - The API name of the Salesforce object.
  • WeakEtag - If the weak etag is non-zero, two records that have the same ApiName, Id and WeakEtag is of the same version. This means they represent the same record at that point in time. If two records have the same ApiName and Id but different WeakEtag, the record with the greater WeakEtag is the more recent version of the record.
  • DisplayValue - The displayable value for a field. In API v60.0 and later, DisplayValue is also supported for polymorphic fields. Querying DisplayValue for multiple polymorphic fields requires additional processing time.
  • LastModifiedById - The ID of the user who last updated this record.
  • LastModifiedDate - The date and time when a user last modified this record. Date and time information is in ISO 8601 format.
  • SystemModStamp - The date and time when a user or an automated process (such as a trigger) last modified this record. Date and time information is in ISO 8601 format.
  • RecordTypeId - The record type ID for this record.

An object can map to three categories of fields:

Fields 

Fields on the object are either mapped to a GraphQL Scalar or a “Field Value” type. The GraphQL Schema contains at least one scalar for each field type on Salesforce objects. Mapping a field into the schema uses these scalars.

Scalar types for record fields
1scalar Base64;
2scalar Boolean;
3scalar Currency;
4scalar Date;
5scalar DateTime;
6scalar Double;
7scalar Email;
8scalar EncryptedString;
9scalar ID;
10scalar IdorRef;
11scalar Int;
12scalar JSON;
13scalar Latitude;
14scalar Long;
15scalar LongTextArea;
16scalar Longitude;
17scalar MultiPicklist;
18scalar Percent;
19scalar PhoneNumber;
20scalar Picklist;
21scalar RichTextArea;
22scalar String;
23scalar TextArea;
24scalar Time;
25scalar Url;

This list doesn’t include every scalar type in the schema. Only the scalars that represent possible types for Salesforce object fields are listed.

Note

Field value types have the naming pattern <ScalarTypeName>Value, for example, PicklistValue. All field value types have a field named value, of the corresponding scalar type. That is, PicklistValue has a field value of type Picklist. The field value type has a field called label if the field type supports the toLabel() SOQL function. The field value type has a field called format if the field type supports the format() SOQL function.

In addition, the field value type has a field called displayValue, backed by whichever of the two toLabel() or format() function calls the field type supports.

Currently, no field type supports both format() and toLabel(), and so there’s no ambiguity on which value is used for the displayValue field.

Note

Here’s an example of a few field value types:

PicklistValue Type
1type PicklistValue {
2  value: Picklist       # the raw value of the field on the object
3  label: String         # execute a SOQL toLabel() call
4  displayValue: String  # backed by the toLabel() call
5}
6
7type CurrencyValue {
8  value: Currency       # the raw value of the field on the object
9  format: String        # execute a SOQL format() call
10  displayValue: String  # backed by the format() call
11}

When mapping a Salesforce object field into the schema, the field maps to either a field value type or the scalar directly. This is the process for the field type-mapping operation.

  • If the field name is Id, the scalar ID is used.

  • If the field type is Double, the field participates in a compound field of type Address or Geolocation and the field name is or ends with Latitude then the LatitudeValue object type is used.

  • If the field type is Double, the field participates in a compound field of type Address or Geolocation and the field name is or ends with Longitude then the LongitudeValue object type is used.

  • If the field type is TextArea, and the fields extra type info has the rich text flag, then the RichTextAreaValue object type is used.

  • If the field type is TextArea, and the fields length value is greater than 255, the LongTextAreaValue object type is used.

Otherwise, the field has the corresponding value type for the field type. For example, fields of type Picklist use the PicklistValue type, and fields of type Currency use the CurrencyValue type.

Optional Fields 

Use the @optional field directive for successful query execution even if some fields are inaccessible to the context user. Mark fields, including parent and child relationship fields, as optional anywhere in your query. If the context user has access to the optional field, the query returns all the data. If the context user doesn’t have access to the optional field, the query succeeds but doesn’t return the optional field or any subfields. Arguments can’t take the optional directive, but fields that take arguments can. If an optional field takes an argument that is set to a variable, the context user must also have access to the variable type for the query to succeed.

Compound Fields 

Compound fields correspond to the ObjectName__FieldName__CompoundField GraphQL type. For example, the BillingAddress field on the Account object, or the Address field on the Lead object are compound fields. They correspond to the Account_BillingAddress_CompoundField type and Lead_Address_CompoundField type respectively.

Here’s an example of the Account_BillingAddress_CompoundField type:

Account with compound fields
1type Account {
2  BillingAddress: BillingAddress # `Account_BillingAddress_CompoundField type
3  Location__c: Location__c # Account_Location__c_CompoundField type
4}

An address compound field comprises several constituent fields, like BillingCity and BillingCountry.

BillingAddress with its constituent fields
1type BillingAddress {
2  BillingCity: StringValue
3  BillingCountry: StringValue
4  BillingGeocodeAccuracy: PicklistValue
5  BillingLatitude: LatitudeValue
6  BillingLongitude: LongitudeValue
7  BillingPostalCode: StringValue
8  BillingState: StringValue
9  BillingStreet TextAreaValue
10}

A geolocation compound field includes the FieldName__Latitude__s and FieldName__Longitude__s fields.

Location__c with its constituent fields
1type Account_Location__c_CompoundField {
2  Location__Latitude__s: DoubleValue
3  Location__Longitude__s DoubleValue
4}

Compound fields follow these restrictions.

  • Include a compound field with its constituent fields in a query.
  • Use the constituent fields when ordering by or filtering using a where argument.
  • Use the constituent fields when working with aggregate queries.

This example queries accounts with its billing address fields.

Query compound fields with constituent fields
1query AccountsWithBillingAddress{
2  uiapi {
3    query {
4      Account {
5        edges {
6          node {
7            Name { value  }
8            BillingAddress {
9              BillingCity {
10                value
11                displayValue
12                label
13              }
14              BillingCountry {
15                value
16                displayValue
17                label
18              }
19            }
20          }
21        }
22      }
23    }
24  }
25}

Compound fields follow the limitations described at Object Reference for the Salesforce Platform.

Note

Parent Relationships 

Each parent relationship on an object has a corresponding field on the GraphQL object, if one of the sObjects that participates in the relationship also participates in the schema. The field name on the GraphQL object matches the relationship name. For example, both Contact and Account participate in the schema, so an Account field (relationship name) of type Account (sObject name) is present on type Contact.

If the object pointed at doesn’t appear in the schema, then no field for that relationship is added to the object type. For example, a relationship points at an object that’s not supported by UI API, so it isn’t mapped into the schema. As a result, the relationship isn’t mapped onto the object.

If the relationship is non-polymorphic, then the field has a type of that sObject.

1type Account {
2  LastModifiedBy: User       # lastModifiedBy is non-polymorphic
3  LastModifiedById: IDValue  # The ID field of the parent relationship is included as well
4}
5
6# the 'LastModifiedBy' field on 'Account' points at the 'User' object
7type User {
8  ...
9}

If the relationship is polymorphic, then the field’s name is still the relationship name, but the field type is a Union. The unions type name follows the pattern <ObjectName>_<RelationshipName>. For example, the SocialPost object has a polymorphic relationship named Who, and so the union type name is SocialPost_Who. Each concrete type in the schema that participates in the polymorphic relationship becomes a possible type of the union.

If no object types that participate in the polymorphic relationship are mappable into the schema, then the field for the relationship isn’t added to the type.

1type SocialPost {
2  Who: SocialPost_Who  # the polymorphic relationship becomes a field that has a Union type
3  WhoId: IDValue       # The ID field of the parent relationship is included
4}
5
6union SocialPost_Who = Account | Contact | Lead # the union has one possible type for each concrete type that participates in the polymorphic relationship

Child Relationships 

Each child relationship that points at an sObject type that participates in the schema is added as a field with a Relay Record Connection type to the GraphQL object. The name of the field matches the name of the child relationship. For example, Account has a field named Contacts of type ContactConnection.

Account relationship with Contacts
1type Account {
2  Contacts(                  # the object will have a field for each child relationship
3    first: Int
4    after: String
5    orderBy: Contact_OrderBy
6    where: Contact_Filter
7  ): ContactConnection       # the field type will be a Relay Connection for the child object type
8}

Construct the arguments where and orderBy using the rules outlined in Filtering and Ordering, respectively. Scopes can’t be applied to querying child relationships, and as a result the field doesn’t have a scope argument.

See Also 

Relationship Filters

Polymorphic Relationship Filters