Polymorphic Relationship Filters

AVAILABLE API VERSION
API v56.0 and later
Event and Task (Beta) available in API v59.0 and later

A polymorphic relationship is one where an object can be one of several object types. For example, an event can have a polymorphic relationship to an account, campaign, or opportunity via the Related To field. Conversely, a contact has a non-polymorphic relationship to an account via the Account Name field.

Common polymorphic fields include the Owner, Who, and What fields.

To query using a polymorphic relationship, use inline fragments with the ... on Object syntax.

The Owner field represents the parent of the record. For example, a case owner can be a Group or User object.

When the parent relationship is polymorphic, an input object following the naming convention <ObjectName>_<RelationshipName>_Filters is constructed and used as the filter type for that parent relationship.

The Owner field follows the naming convention <ObjectName>_Owner, which is used as the filter type for that parent relationship.

For example, the Case object’s Owner filter has a type of Case_Owner_Filters.

The union has one possible type for each concrete type that participates in the polymorphic relationship.

The input object type has one input object field of the filter type for each object that participates in the polymorphic relationship.

Let’s say you query cases that match a specified Owner.Name value. Case records correspond to owners that are either groups or users. This example queries cases where the owner’s username starts with "D" and the Owner.Type is a User object.

The previous query is similar to this SOQL statement.

In the next example, the Name field is available only when the node is of type User.

The Who field represents the person associated with the record. Some objects, like Event, Task, ContactRequest, and SocialPost, have a Who field. For more information, see the Object Reference for the Salesforce Platform.

Not all objects that have a Who field are available in GraphQL API via UI API.

For example, the ContactRequest object's Who filter has a type of ContactRequest_Who_Filters. This input object has one input object field for each concrete type that participates in the polymorphic relationship.

The union has one possible type for each concrete type that participates in the polymorphic relationship.

The input object type has one input object field of the filter type for each object that participates in the polymorphic relationship.

ContactRequest records have a Who field that can reference a contact, lead, name, or user. This example query applies to ContactRequest records whose owners can be either a Group or User object. It filters the records based on the User object and select records where the Who.Type is a Contact object.

The previous query is similar to this SOQL statement.

The What field represents non-person objects that are associated with the record. Some objects, like Event and ContactRequest, have a What field. For more information, see the Object Reference for the Salesforce Platform.

Not all objects that have a Who field are available in GraphQL API via UI API.

For example, the ContactRequest object's What filter has a type of ContactRequest_What_Filters. This input object has one input object field for each concrete type that participates in the polymorphic relationship.

The union has one possible type for each concrete type that participates in the polymorphic relationship.

The input object type has one input object field of the filter type for each object that participates in the polymorphic relationship.

ContactRequest records have a What field that can reference an account, case, name, or opportunity. This example query applies to ContactRequest records whose What field is Account. It returns ContactRequest records with the Name field, the What.Name field where the What.Type is Account.

The previous query is similar to this SOQL statement.

You can filter the result set by the value of the underlying polymorphic types data. Each filter is combined with an and operator to assert the polymorphic relationship is of the filter type.

The query is similar to this SOQL statement.

You can work with multiple fields on a polymorphic filter with an or operator. This example returns ContactRequest records with the Id, Name, and What.Name fields where the What.Type is Account. It filters the records where the What.Name is "Salesforce" or the Who.FirstName contact names start with "R".

The previous query is similar to this SOQL statement where the individual clauses are joined with OR, allowing selective filtering based on the underlying type of the polymorphic relationship.

Although objects with an OwnerId, WhatId, or WhoId field have polymorphic parent relationships, GraphQL API includes as possible types only those objects that are available via the User Interface API.

If the parent relationship is non-polymorphic, then the corresponding filter type for that object is used.

Consider an account that as a parent relationship CreatedBy points to the User object.

As the relationship is non-polymorphic, the filter has a field with the same name of the parent relationship with the corresponding filter type.

To find all accounts created by a particular user, we can construct a where argument as follows.

This query is similar to this SOQL statement.