Warning
You're browsing the documentation for an old version of Webiny. Consider upgrading your project to Webiny 5.41.x.
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.

Overview
anchor

Initially, the CarManufacturerexternal link entity that was created in the scaffolding process only contains two attributes: titleexternal link and descriptionexternal link. 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 CarManufacturerexternal link entity, and then, via the new Admin Area application module, enable logged-in users to modify its value, by adding a simple Switchexternal link form field to the Car Manufacturers form.

GraphQL API
anchor

Starting from the GraphQL API, let’s open our CarManufacturerexternal link entity and simply define the new attribute on it (parts of code removed for brevity):

api/code/graphql/src/plugins/scaffolds/carManufacturers/entities/CarManufacturer.ts

Notice how we’re passing the CarManufacturerEntityexternal link 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:

api/code/graphql/src/plugins/scaffolds/carManufacturers/types.ts

Finally, let’s update our CarManufacturer GraphQL types in our schema, located in the typeDefs.tsexternal link file:

api/code/graphql/src/plugins/scaffolds/carManufacturers/typeDefs.ts

If you’ve run the two webiny watch commands, mentioned in the previous section, the performed application code changes should be automatically rebuilt and redeployed into the cloud.

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.

Admin Area Application
anchor

In the CarManufacturersFormexternal link React component, let’s add a new Switchexternal link 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):

apps/admin/code/src/plugins/scaffolds/carManufacturers/views/CarManufacturersForm.tsx

Once we’ve updated the CarManufacturersFormexternal link React component, the form itself should look like the following:

Updated Car Manufacturers FormUpdated Car Manufacturers Form
(click to enlarge)

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.

Adding the isPopular field can be easily done by updating the CAR_MANUFACTURER_FIELDS_FRAGMENTexternal link GraphQL fragment, located in the graphql.tsexternal link file:

apps/admin/code/src/plugins/scaffolds/carManufacturers/views/hooks/graphql.ts

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.

Updated Car Manufacturers FormUpdated Car Manufacturers Form
(click to enlarge)

FAQ
anchor

I See DynamoDB Toolbox Is Being Used for Interacting With Amazon DynamoDB. Can I Use a Different Library?
anchor

On the GraphQL API side, you might have noticed we’re using DynamoDB Toolboxexternal link, 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 Clientexternal link or maybe some other preferred library.

Can I Bring My Own NPM Libraries?
anchor

You are free to bring your own preferred NPM libraries.

How Can I Perform Data Validation?
anchor

For data validation in general, we recommend you check out the @webiny/validationexternal link library. It’s easy to use, provides a plethora of different data validation rules, and also enables you to expand it with your own.

What About Security (Authentication and Authorization)?
anchor

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.