✏️Editing your project
If you want to add more data models, endpoints, or change the existing configuration of your project, use the following command to edit it:
You will be able to edit its configuration, through the same process you used in the Configuring your project step.
Once you are done, you can choose to immediately deploy your modifications, or deploy later using the following command:
Querying nested elements
When we defined our Planet and Galaxy models, we added a link between them. More specifically, we added a galaxy
property on our Planet model, which is a reference to a planet's parent galaxy. Let's say we now want to retrieve a planet and its parent Galaxy in the same request.
We can add an include_galaxy
query parameter to the getPlanet
operation, which retrieves a single Planet by id.
"Include" parameters are special parameters, designed to include related objects in a response. They must follow the format: include_property
where "property" is the name of a property explicitly listed on your model. This property should be a reference to another existing model.
Let's now try to retrieve planet Neptune which we created earlier and its parent galaxy, using its id. If you can't find it anymore, just make a quick query to retrieve the list of planets.
You should see the following result:
Let's say we now want to do retrieve a galaxy, and all its planets in a single query. We could try to do the same thing we did previously, and add a include_planets
parameter to our API getGalaxy
operation's configuration. But remember, we said that include parameters only work for a relationship if the related element is declared as property on the origin mode, so we first need to explicitly declare a planets
property on our Galaxy model.
Let's run first:
Then, let's add the planets property on the Galaxy model:
We can now add our include_planets
query parameter on our getGalaxy
operation:
Once you have saved these changes and deployed a new version of your project, we can try to make our query using the Milky Way's id
:
This should return the following result:
We can retrieve a galaxy and all its related planets in one query. Victory!
Linking an entity to another one
When we created planet Earth in our data model, we somehow forgot to link it to a galaxy. Now that we have seen how to query related entities, let's say how we can link two related entities together.
We know that our Planet model features a galaxy
property, which is an object reference to a Galaxy model. If we could pass an object containing an existing Galaxy's id in a request to update a Planet, we could link the two together.
Let's edit our API model, and add a PATCH
request under the path /planets/:id
Once you have saved and deployed, we'll be able to try our new method. Let's finally link planet Earth to the Milky Way:
You should see the following result:
We can finally make a request to retrieve the Miky Way and its linked planets:
You should see the following result:
Last updated