WHAT YOU’LL LEARN
- how to continue developing on top of the generated application code
- in the GraphQL API, how to extend the initially created entity with new attributes
- in the Admin Area, how to extend the initially created form with new form fields
Can I use this?
In order to follow this tutorial, you must use Webiny version 5.9.0 or greater.
CarManufacturer entity that was created in the scaffolding process only contains two attributes:
description. Of course, these are just starter attributes and we are certainly encouraged to extend the entity with additional ones.
Let's see how we can add the new
isPopular boolean attribute to the
CarManufacturer entity, and then, via the new Admin Area application module, enable logged-in users to modify its value, by adding a simple
Switch form field to the Car Manufacturers form.
Starting from the GraphQL API, let's open our
CarManufacturer entity and simply define the new attribute on it (parts of code removed for brevity):
Notice how we're passing the
CarManufacturerEntity interface upon instantiating the
Entity class. Since the interface is essentially listing all attributes that our entity consists of, let's update it as well:
Finally, let's update our
CarManufacturer GraphQL types in our schema, located in the
Once we've done all of the above shown changes, we can switch to our Admin Area application, and continue by updating the Car Manufacturers form.
CarManufacturersForm React component, let's add a new
Switch form element, which will enable logged-in users to mark a particular car manufacturer as popular or not popular (parts of code removed for brevity):
Once we've updated the
CarManufacturersForm React component, the form itself should look like the following:
Finally, we need to add the
isPopular field to all relevant GraphQL query and mutation operations. This ensures that we're both sending and receiving the
isPopular value while interacting with the GraphQL API.
With this final change in place, we should be able to edit the isPopular attribute for each car manufacturer entry. To test this, we can simply open an existing entry, try marking it as popular, and submitting the form. If everything was done correctly, the form submission should be successful.
On the GraphQL API side, you might have noticed we're using DynamoDB Toolbox, which is a neat little library that makes interaction with DynamoDB a bit easier. But, note that if you wanted, you could easily replace it with the default AWS DynamoDB Document Client or maybe some other preferred library.
You are free to bring your own preferred NPM libraries.
For data validation in general, we recommend you check out the
@webiny/validation library. It's easy to use, provides a plethora of different data validation rules, and also enables you to expand it with your own.
Because this is not something we can effectively predict, the generated application code does not include any authentication and authorization logic. But luckily, with a couple of built-in utilities, this is not too hard to implement.