ordersByOrderId (orderId:String!):Order
--------------------
--------------------
Order
--------------------
orderId: String!
Items: Item[]
customerId: String!
How Links Between Object Types Work in Anypoint DataGraph
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 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
:
Order Schema | Customer Schema |
---|---|
|
Your unified schema currently looks like this:
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:
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 theOrder
object type that now returns the linkedCustomer
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 theCustomer
API schema do not affect yourOrder
API schema: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:
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
:
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
:
customerDetail (id: string!, status: string!): Customer
--------------------
--------------------
Customer
--------------------
customerDetail: string!, string!
customerId: String!
customerStatus: String!
Assuming the same Order
schema from above:
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:
ordersByOrderId (orderId:String!):Order
--------------------
--------------------
Order
--------------------
orderId: String!
Items: Item[]
customerId: Customer
customerStatus: Customer
--------------------
--------------------
Customer
--------------------
--------------------
customerDetail: String!, String!
customerId: String!
customerStatus: String!