---
schema_version: "1"
id: ce187377a13a285ff8a2f462628c6597d9ef601187d9b17ac5057a7d2ef69437
title: Query Multiple Objects Dynamically
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/graphql-api/graphql/guides/wire/graphql-wire-lwc-multiple-objects.md
canonical_url: https://developer.salesforce.com/docs/platform/graphql/guide/graphql-wire-lwc-multiple-objects.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 5b7b0519a9ee086b94761bedf5570777f1b3d8d2847f05dbf941274c57992b77
products:
  - GraphQL API
---
# Query Multiple Objects Dynamically

GraphQL API simplifies how you query records across multiple objects. You can write a query that asks for the exact fields you want in one call. Using GraphQL API to query multiple objects is more performant than making multiple Apex wire calls. Previously, you could also achieve this by calling a custom Apex controller and then pass the returned records to [the `getRecords` wire adapter](https://developer.salesforce.com/docs/platform/lwc/guide/reference-wire-adapters-records.html) with additional parameters that request records from other objects.

Additionally, when statically defined, GraphQL API queries support [referential integrity](https://developer.salesforce.com/docs/platform/graphql/guide/graphql-wire-lwc-best.md#use-aliases-with-custom-objects-and-fields) for references to objects and fields. You don’t need to [import references to objects and fields](https://developer.salesforce.com/docs/platform/lwc/guide/data-wire-service-about.html#import-references-to-salesforce-objects-and-fields) like you do with other LWC wire adapters.

## Use GraphQL Variables in Multiple Objects Query

When you request records across multiple objects, you don’t always know the record ID. For example, the record ID can vary depending on the record page you’re viewing, or the user ID can depend on the context user. To provide the record ID as a dynamic value for your query, use [the `variables` configuration parameter](https://developer.salesforce.com/docs/platform/graphql/guide/graphql-wire-lwc-reactive.md).

**tip:** The [`graphqlMultipleObjects` component](https://github.com/trailheadapps/lwc-recipes/tree/main/force-app/main/default/lwc/graphqlMultipleObjects) in the [lwc-recipes repo](https://github.com/trailheadapps/lwc-recipes) retrieves and displays account and contact records using a single query.

This example queries all contacts related to an account and displays the contacts with record data from the User object. The query requests the `Id` and `Name` fields on the Contact object, along with the `Id` and `Name` fields on the User object.

The `AccountId` field on the Contact object corresponds to the ID of an account record. Use the `recordId` property to include the account [record context](https://developer.salesforce.com/docs/platform/lwc/guide/use-record-context.html) for the contacts query.

To get record data about the current user, import the [`@salesforce/user/Id` module](https://developer.salesforce.com/docs/platform/lwc/guide/get-current-user.html).

```javascript
//multipleObjectsGraphQL.js
import { LightningElement, wire, api } from "lwc";
import { gql, graphql } from "lightning/graphql";
import Id from "@salesforce/user/Id";

export default class MultipleObjectsGraphQL extends LightningElement {
  queryError;
  contacts;

  @api recordId;

  userName;
  userId = Id;

  @wire(graphql, {
    query: "$contactQuery",
    variables: "$queryData",
  })
  recordsQueryResult({ data, errors }) {
    if (data) {
      this.contacts = data.uiapi.query.Contact.edges.map((edge) => edge.node);
      this.userName = data.uiapi.query.User.edges.map((edge) => edge.node);
    }

    if (errors) {
      this.queryError = errors;
    }
  }

  get contactQuery() {
    return gql`
      query contactsOnAccount($recordId: ID!, $userId: ID!) {
        uiapi {
          query {
            Contact(where: { AccountId: { eq: $recordId } }) {
              edges {
                node {
                  Id
                  Name {
                    value
                  }
                }
              }
            }
            User(where: { Id: { eq: $userId } }) {
              edges {
                node {
                  Id
                  Name {
                    value
                  }
                }
              }
            }
          }
        }
      }
    `;
  }

  get queryData() {
    return {
      recordId: this.recordId,
      userId: this.userId,
    };
  }
}
```

Display the records using the `for:each` directive.

<!-- prettier-ignore -->
```html
<template>
  <lightning-card title="MultipleObjectsGraphQL" icon-name="standard:contact">
    <div class="slds-var-m-around_medium">
      <template lwc:if={contacts}>
        <template for:each={contacts} for:item="contact">
          <p key={contact.Id}>{contact.Name.value}</p>
        </template>
      </template>
      <template lwc:if={userName}>
        <template for:each={userName} for:item="user">
          <p key={user.Id}>{user.Name.value}</p>
        </template>
      </template>

      <template lwc:elseif={errors}>
        <!-- Display errors -->
      </template>
    </div>
  </lightning-card>
</template>
```

## See Also

[Query Objects Examples](https://developer.salesforce.com/docs/platform/graphql/query/query-record-examples.md)

[Call Apex Methods](https://developer.salesforce.com/docs/platform/lwc/guide/apex.html)

[GraphQL API Query Limitations](https://developer.salesforce.com/docs/platform/graphql/query/query-limits.md)
