---
schema_version: "1"
id: 5c8a20ecdddc48dc2dbee72602b1269746eb75a316ed508f639925766e128985
title: How Links Between Object Types Work in Anypoint DataGraph
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/datagraph/linking.md
canonical_url: https://docs.mulesoft.com/datagraph/linking
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 3fc97d4acf2caae03613915a52f6467c26418eb74ce3fd9acf618b3aa2334ec6
description: MuleSoft Documentation Site
latest: true
---
---
product: DataGraph
---

# How Links Between Object Types Work in Anypoint DataGraph

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

You can link your current object type to a different target object type to improve the query
experience. Linking enables you to join fields from two related types that describe different
objects and create a connection between those types.

You can link your current object type to a target object type if your current type is not
collaboration enabled; however, the target must be collaboration enabled. See
[Enabling Object Type Collaboration in Anypoint DataGraph](https://docs.mulesoft.com/datagraph/collaboration) for more information about
how collaboration works.

## Creating a Link Between Object Types

Consider two API schemas, one with the object type `Order` and the other with the object type
`Customer`:

<table><colgroup><col> <col></colgroup><thead><tr><th>Order Schema</th><th>Customer Schema</th></tr></thead><tbody><tr><td><div><div><div><pre><code>ordersByOrderId (orderId:String!):Order
--------------------
--------------------
Order
--------------------
orderId: String!
Items: Item[]
customerId: String!</code></pre></div></div></div></td><td><div><div><div><pre><code>customersByCustomerId (customerId:String!): Customer
--------------------
--------------------
Customer
--------------------
customerId: String!
name: String!</code></pre></div></div></div></td></tr></tbody></table>

Your unified schema currently looks like this:

```none
ordersByOrderId (orderId:String!):Order
customersByCustomerId (customerId:String!): Customer
--------------------
--------------------
Order
--------------------
orderId: String!
Items: Item[]
customerId: String!
--------------------
--------------------
Customer
--------------------
customerId: String!
name: String!
```

In this current configuration, to query the order and the customer datasets at the same time, you
must query both types separately:

```none
ordersByOrderId (orderId: “123”) {
  orderId
  customerId
}
customersByCustomerId (customerId: “CC83”) {
  customerId
  name
}
```

Additionally, to query the customer data for the order, you must know the customer ID associated
with it.

However, note that the `Order` object type has a field that returns `customerId`. You can _link_
your `Order` object type to the `Customer` object type using `customerId: String!` as a _foreign
key_, pointing to `Customer` as your target type. Doing so causes your selected foreign key to map
to the primary key `customerId:String!` on the `Customer` type.

As a result of this link:

- A new field called `Customer` is created in the `Order` object type that now returns the linked
  `Customer` object type.

- The new object type generated in your API schema acts as a reference to the type in the `Customer`
  API schema, ensuring that any changes made to the `Customer` API schema do not affect your `Order`
  API schema:

  ```none
  ordersByOrderId (orderId:String!):Order
  --------------------
  --------------------
  Order
  --------------------
  orderId: String!
  Items: Item[]
  customer: Customer
  --------------------
  --------------------
  Customer
  --------------------
  --------------------
  customerId: String!
  ```

In the updated unified schema, you can see that the `Order` type is linked to `Customer` type:

```none
ordersByOrderId (orderId:String!):Order
--------------------
--------------------
Order
--------------------
orderId: String!
Items: Item[]
customer: Customer
--------------------
--------------------
Customer
--------------------
--------------------
customerId: String
name: String
```

After linking, consumers of your unified schema can query the `Order` dataset along with its related
`Customer` dataset without having to know the associated `customerId`:

```none
ordersByOrderId (orderId: “123”) {
  orderId
  customer {
    name
  }
}
```

## Creating a Link Between Object Types That Use Composite Keys

The default query method for a type can also use _composite keys_.

Consider a target type `Customer` that uses composite keys for its default query method:
`customerId` and `customerStatus`:

```none
customerDetail (id: string!, status: string!): Customer
--------------------
--------------------
Customer
--------------------
customerDetail: string!, string!
customerId: String!
customerStatus: String!
```

Assuming the same `Order` schema from above:

```none
ordersByOrderId (orderId:String!):Order
--------------------
--------------------
Order
--------------------
orderId: String!
Items: Item[]
customerId: String!
```

If you linked the `Customer` type as a target for the `Order` type, the unified schema reflects the
link as follows:

```none
ordersByOrderId (orderId:String!):Order
--------------------
--------------------
Order
--------------------
orderId: String!
Items: Item[]
customerId: Customer
customerStatus: Customer
--------------------
--------------------
Customer
--------------------
--------------------
customerDetail: String!, String!
customerId: String!
customerStatus: String!
```

## Additional Resources

- [Create and Edit Links Between Object Types](https://docs.mulesoft.com/datagraph/manage-links)
