Work with Multiple Repositories
Introduction
Managing multiple repositories in Hasura DDN lets you streamline development across independent subgraphs while maintaining a unified supergraph. This approach provides flexibility for teams to work autonomously on their respective domains, all within a single coordinated API. By organizing your project into multiple repositories, you can iterate and deploy changes efficiently without impacting other subgraphs, ensuring a smooth and collaborative development experience.
You'll learn:
- How to organize your project into subgraphs
- How to provision a "parent" repo
- How to set up your cloud project for multi-subgraph development
- How to create independent subgraph repositories
- How to create, test, and deploy your supergraph as subgraphs develop independently
- How to create relationships across independent subgraphs
This tutorial takes about thirty minutes.
In order to utilize multi-repository collaboration, you must have an active DDN Advanced Plan.
Create the initial project
Begin by creating a "parent project" that will serve as the coordinated supergraph for your independent subgraph repositories. The person creating this will assume the role of supergraph admin and have full control over provisioning subgraphs, inviting collaborators, and managing the API as a whole.
Step 1. Initialize a new local project
ddn supergraph init parent-project && cd parent-project && git init
This will scaffold out the local configuration for a DDN project and initialize a git repository.
Step 2. Create a cloud project
ddn project init
In .hasura/context.yaml
, you'll see a new project
key-value pair with the name of the project returned from the CLI.
Step 3. Create a commit
git add . && git commit -m "Initial commit"
Step 4. Provision subgraphs
We'll add two subgraphs to this supergraph: customers
and billing
.
ddn project subgraph create customers && ddn project subgraph create billing
Step 5. Create a supergraph build
ddn supergraph build create
This will serve as the foundation for your first subgraph build to expand upon.
Step 6. Invite collaborators
Head to the project's console at console.hasura.io and navigate to
Setetings/Collaborators
. Then, invite collaborators based on their role:
Role | Description |
---|---|
Subgraph Admin | Users with permissions to create builds and deploy them to the parent project's endpoint. |
Subgraph Developer | Developers responsible for implementing features and making iterative changes to the subgraph's functionality. |
Learn more about collaborators and roles here.
Each collaborator will receive an email inviting them to your project.
Add independent subgraphs to the project
Each subgraph will live in its own repository. While this can be an existing repository, we're going to demonstrate initializing a new repository below.
Customers
Step 1. Create a new repo for the customers
subgraph
ddn supergraph init customer-team --create-subgraph customers && cd customer-team && git init
This will scaffold out the necessary project structure along with initializing a git repository for version control.
Step 2. Map the local project to the existing cloud project
ddn project init --with-project <project-name>
You'll see the project
key-value pair updated in your .hasura/context.yaml
file.
Step 3. Set the subgraph context
ddn context set subgraph ./customers/subgraph.yaml
This will set your unique subgraph in your .hasura/context.yaml
file and make the CLI commands below more concise.
Step 4. Add prefixing
kind: Subgraph
version: v2
definition:
name: customers
generator:
rootPath: .
namingConvention: graphql
graphqlRootFieldPrefix: customers_
graphqlTypeNamePrefix: customers_
includePaths:
- metadata
These will prevent collisions of GraphQL root fields and GraphQL types when your supergraph is built.
Step 5. Add a data source and generate your first local build
In this tutorial, we'll use the PostgreSQL connector and our sample PostgreSQL database:
postgresql://read_only_user:[email protected]:5432/v3-docs-sample-app
ddn connector init customers_pg -i
Generate the Hasura metadata
ddn connector introspect customers_pg
After running this, you should see a representation of your database's schema in the
customers/connector/customers_pg/configuration.json
file; you can view this using cat
or open the file in your
editor.
ddn models add customers_pg users
Open the customers/metadata
directory and you'll find a newly-generated file: Users.hml
. The DDN CLI will use this
Hasura Metadata Language file to represent the users
table from PostgreSQL in your API as a
model.
Create a new local build and test the API
ddn supergraph build local
The build is stored as a set of JSON files in engine/build
.
ddn run docker-start
Your terminal will be taken over by logs for the different services.
ddn console --local
query GET_USERS {
customers_users {
id
name
}
}
{
"data": {
"customers_users": [
{
"id": "7cf0a66c-65b7-11ed-b904-fb49f034fbbb",
"name": "Sean"
},
{
"id": "82001336-65b7-11ed-b905-7fa26a16d198",
"name": "Rob"
},
{
"id": "86d5fba0-65b7-11ed-b906-afb985970e2e",
"name": "Marion"
},
{
"id": "8dea1160-65b7-11ed-b907-e3c5123cb650",
"name": "Sandeep"
},
{
"id": "9bd9d300-65b7-11ed-b908-571fef22d2ba",
"name": "Abby"
}
]
}
}
Step 6. Create a subgraph build
Now that we've tested our local API and are happy with its state, we can create a build of our independent subgraph on our cloud project.
ddn subgraph build create
The CLI will return the subgraph's build version; you'll need this value in the next step.
Step 7. Create a supergraph build
ddn supergraph build get
Grab the most recent build's version and use it — along with the subgraph build version — in the following command.
ddn supergraph build create --subgraph-version customers:<build-version> --base-supergraph-version <supergraph-build-id>
The CLI will return a build URL for the console so you can explore your build and test your subgraph changes with others before applying the build to the supergraph. In order to apply the build, you must be a subgraph admin or higher:
ddn supergraph build apply <supergraph-build-version>
As a multi-repository setup is intended to be utilized by multiple persons across teams, you're not likely to have
multiple instances of your Hasura DDN Engine and other services running at the same time on your machine. Ensure you've
killed all active services before proceeding to the next step for the billing
subgraph.
Billing
The steps below will allow you to incorporate your independent billing
subgraph into your deployed supergraph API.
Step 1. Create a new repo for the billing
subgraph
ddn supergraph init billing-team --create-subgraph billing && cd billing-team && git init
This will scaffold out the necessary project structure along with initializing a git repository for version control.
Step 2. Map the local project to the existing cloud project
ddn project init --with-project <project-name>
You'll see the project
key-value pair updated in your .hasura/context.yaml
file.
Step 3. Set the subgraph context
ddn context set subgraph ./billing/subgraph.yaml
This will set your unique subgraph in your .hasura/context.yaml
file and make the CLI commands below more concise.
Step 4. Add prefixing
kind: Subgraph
version: v2
definition:
name: billing
generator:
rootPath: .
namingConvention: graphql
graphqlRootFieldPrefix: billing_
graphqlTypeNamePrefix: billing_
includePaths:
- metadata
Like before, this will ensure the types an fields are namespaced to your subgraph.
Step 5. Add a data source and generate your first local build
As before, we'll use the PostgreSQL connector with our sample database:
postgresql://read_only_user:[email protected]:5432/v3-docs-sample-app
ddn connector init billing_pg -i
Generate the Hasura metadata
ddn connector introspect billing_pg
After running this, you should see a representation of your database's schema in the
billing/connector/billing_pg/configuration.json
file; you can view this using cat
or open the file in your editor.
ddn models add billing_pg orders
Open the billing/metadata
directory and you'll find a newly-generated file: Orders.hml
. The DDN CLI will use this
Hasura Metadata Language file to represent the orders
table from PostgreSQL in your API as a
model.
Create a new local build and test the API
ddn supergraph build local
The build is stored as a set of JSON files in engine/build
.
ddn run docker-start
Your terminal will be taken over by logs for the different services.
ddn console --local
query GET_ORDERS {
billing_orders {
id
createdAt
status
}
}
{
"data": {
"billing_orders": [
{
"id": "c7406b75-6b24-41e4-9c5b-ff3feada9447",
"createdAt": "2023-10-29T17:02:50.889261+00:00",
"status": "processing"
},
{
"id": "7ff13435-b590-4d6b-957f-f7fd39d4528a",
"createdAt": "2023-10-29T17:02:50.958076+00:00",
"status": "complete"
},
{
"id": "98612470-1feb-4b91-88f7-9289d652ee87",
"createdAt": "2023-10-29T17:02:51.021317+00:00",
"status": "complete"
},
{
"id": "85581445-752a-4aef-9684-b648eb5d5f42",
"createdAt": "2023-10-29T17:02:51.085229+00:00",
"status": "complete"
},
{
"id": "9891596a-a732-4c1c-902c-1a112da48fec",
"createdAt": "2023-10-29T17:02:51.150084+00:00",
"status": "complete"
}
]
}
}
Step 6. Create a subgraph build
ddn subgraph build create
The CLI will return the subgraph's build version; you'll need this value in the next step.
Step 7. Create a supergraph build
ddn supergraph build get
Grab the most-recent build's version and use it — along with the subgraph build version — in the following command.
ddn supergraph build create --subgraph-version billing:<build-version> --base-supergraph-version <supergraph-build-id>
The CLI will return a build URL for the console so you can explore your build and test your subgraph changes with others before applying the build to the supergraph. In order to apply the build, you must be a subgraph admin or higher:
ddn supergraph build apply <supergraph-build-version>
As a multi-repository setup is intended to be utilized by multiple persons across teams, you're not likely to have multiple instances of your Hasura DDN Engine and other services running at the same time on your machine. Ensure you've killed all active services before proceeding to the next step.
Add a cross-subgraph relationship
We can create relationships across subgraphs that are testable on a Hasura Cloud project. As your locally-running engine will only have access to your local subgraphs and their accompanying metadata, you'll need to define relationships using your knowledge of other subgraphs and then test them using a supergraph build in your hosted environment.
---
kind: Relationship
version: v1
definition:
name: orders
sourceType: Users
target:
model:
subgraph: billing
name: Orders
relationshipType: Array
mapping:
- source:
fieldPath:
- fieldName: id
target:
modelField:
- fieldName: userId
ddn subgraph build create
ddn supergraph build get
ddn supergraph build create --subgraph-version customers:<build-version> --base-supergraph-version <supergraph-build-id>
query GET_USERS_AND_ORDERS {
customers_users {
id
name
orders {
id
createdAt
status
}
}
}
{
"data": {
"customers_users": [
{
"id": "7cf0a66c-65b7-11ed-b904-fb49f034fbbb",
"name": "Sean",
"orders": [
{
"id": "7ff13435-b590-4d6b-957f-f7fd39d4528a",
"createdAt": "2023-10-29T17:02:50.958076+00:00",
"status": "complete"
}
]
},
{
"id": "82001336-65b7-11ed-b905-7fa26a16d198",
"name": "Rob",
"orders": [
{
"id": "9891596a-a732-4c1c-902c-1a112da48fec",
"createdAt": "2023-10-29T17:02:51.150084+00:00",
"status": "complete"
}
]
},
{
"id": "86d5fba0-65b7-11ed-b906-afb985970e2e",
"name": "Marion",
"orders": [
{
"id": "85581445-752a-4aef-9684-b648eb5d5f42",
"createdAt": "2023-10-29T17:02:51.085229+00:00",
"status": "complete"
}
]
},
{
"id": "8dea1160-65b7-11ed-b907-e3c5123cb650",
"name": "Sandeep",
"orders": [
{
"id": "c7406b75-6b24-41e4-9c5b-ff3feada9447",
"createdAt": "2023-10-29T17:02:50.889261+00:00",
"status": "processing"
}
]
},
{
"id": "9bd9d300-65b7-11ed-b908-571fef22d2ba",
"name": "Abby",
"orders": [
{
"id": "98612470-1feb-4b91-88f7-9289d652ee87",
"createdAt": "2023-10-29T17:02:51.021317+00:00",
"status": "complete"
}
]
}
]
}
}
Recap
By organizing your project into multiple repositories, you can create a flexible and collaborative workflow for subgraph development in Hasura DDN. Starting with a parent project, you learned how to provision subgraphs, invite collaborators, and manage builds to integrate subgraph changes into a unified supergraph. This structure ensures teams can work independently while maintaining seamless integration and coordination across the entire API.