Parent and Child Relationship Queries in GraphQL | #GraphQL Salesforce ☁️⚡️


 You can specify a parent or child relationship in your query for the GraphQL wire adapter. Objects can have children relationships to the same object, just like a parent account can have child accounts. Some objects have children relationships to other objects. For example, accounts have child relationships to assets, cases, and contacts among other objects.

The GraphQL API schema provides details of parent-child relationships. For example, the Account field is defined as a parentRelationship type on the Contact object. Similarly, the Cases field is defined as a childRelationship type on the Contact object.

In this blog I will show you how you can add Relationship Filters in GraphQL.


Example 1: Child to Parent Relationships - Get list of Contacts where Account Rating is Hot

graphQLAccounts.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { LightningElement,wire } from 'lwc';
import {gql,graphql} from 'lightning/uiGraphQLApi';

export default class GraphQLAccounts extends LightningElement {
  results;
  errors;

 @wire(graphql, {
    query: gql`
      query GetContacts {
        uiapi {
          query {
            Contact(where:{Account:{
              Rating:{eq:"Hot"}}}) {
              edges {
                node {
                  Id
                  Name {
                    value
                  }
                }
              }
            }
          }
        }
    }`,
 })
   
  graphqlQueryResult({ data, errors }) {
    if (data) {
      this.results = data.uiapi.query.Account.edges.map((edge) => edge.node);
      console.log("🚀 ~ this.results:", JSON.stringify(this.results));
    }
    this.errors = errors;
  }
}


The above GraphQL query will give you the same result what you will be getting using below SOQL.

1
SELECT Id, Name FROM Contact WHERE Account.Rating = 'Hot'


Example 2: Parent to Child Relationship - Get list of Account with Related Contacts LastName 

graphQLAccounts.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { LightningElement,wire } from 'lwc';
import {gql,graphql} from 'lightning/uiGraphQLApi';

export default class GraphQLAccounts extends LightningElement {
  results;
  errors;

 @wire(graphql, {
    query: gql`
      query AccountWithName {
        uiapi {
          query {
            Account(first: 5) {
              edges {
                node {
                  Id
                  Name {
                    value
                  }
                  Contacts{
                    edges{
                      node{
                        LastName{ value }
                      }
                    }
                  }
                }
              }
            }
          }
        }
    }`,
  })
  graphqlQueryResult({ data, errors }) {
    if (data) {
      this.results = data.uiapi.query.Account.edges.map((edge) => edge.node);
      console.log("🚀 ~ this.results:", JSON.stringify(this.results));
    }
    this.errors = errors;
  }
}


The above GraphQL query will give you the same result what you will be getting using below SOQL.

1
SELECT Id, Name, (SELECT LastName FROM Contacts) FROM Account


Checkout Complete Video Tutorial Below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Happy Coding :)

Post a Comment

0 Comments