Create Relationships
Now that we've created our subgraphs, we can start to model the relationships between them. While each of these subgraphs can function independently, we can also model relationships between them to create a single, unified API. This unlocks powerful queries and mutations that span across multiple data sources.
We'll use three examples to demonstrate how to create relationships between subgraphs:
- Users to Orders: connecting our
app
andfulfillment_services
subgraphs. - Orders to Products: connecting our
fulfillment_services
andproduct_management
subgraphs. - Users to Transactions: connecting our
app
andpayment_processing
subgraphs.
We can create these relationships using the same metadata structure we used to create relationships within a single
subgraph. All we need to do is specify the subgraph name in the target
field.
Create a relationship between users and orders
Open Users.hml
and add the following relationship at the bottom of the file:
---kind: Relationshipversion: v1definition:name: orderssource: Userstarget:model:name: OrdersrelationshipType: Arraysubgraph: fulfillment_servicesmapping:- source:fieldPath:- fieldName: idtarget:modelField:- fieldName: customer_id
In this example, we're creating an array relationship between the Users
and Orders
models. This means that a
single user can have multiple orders. We're also specifying the fulfillment_services
subgraph as the target of this
relationship.
Create a relationship between orders and products
Open Orders.hml
and add the following relationship at the bottom of the file:
---kind: Relationshipversion: v1definition:name: productssource: Orderstarget:model:subgraph: product_managementname: ProductsrelationshipType: Arraymapping:- source:fieldPath:- fieldName: productIdtarget:modelField:- fieldName: id
In this example, we're creating an object relationship between the Orders
and Products
models. This means that a
single order can have a single product. We're also specifying the product_management
subgraph as the target of this
relationship.
Create a relationship between users and transactions
Open Users.hml
again and add the following relationship at the bottom of the file:
---kind: Relationshipversion: v1definition:name: paymentssource: Userstarget:model:subgraph: payment_processingname: TransactionsrelationshipType: Arraymapping:- source:fieldPath:- fieldName: idtarget:modelField:- fieldName: userId
In this example, we're creating an array relationship between the Users
and Transactions
models. This means that
a single user can have multiple transactions. We're also specifying the payment_processing
subgraph as the target of
this relationship.
Test it out
We can now head to our project's console and see the visualization of our supergraph 🎉
We can also execute a query that spans across multiple subgraphs. For example, we can query for a user's orders, its associated products, and any payment transactions associated with the user:
query SupergraphQuery {app_users {idorders {idorderDateproducts {idname}}payments {idamounttransactionDatestatus}}}
- Build apps and APIs 10x faster
- Built-in authorization and caching
- 8x more performant than hand-rolled APIs