wasp/web/docs/tutorials/dev-excuses-app/03-adding-operations.md

36 lines
1.1 KiB
Markdown
Raw Normal View History

Docs update for Hacktoberfest (#731) * Initial docs update for Hacktoberfest * Contributing.md update * Update "Before you begin" section * Update web/docs/tutorials/dev-excuses-app/creating-the-project.md Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com> * Update web/docs/tutorials/dev-excuses-app/updating-main-page-js-file.md Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com> * Update CONTRIBUTING.md Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com> * Update web/docs/tutorials/dev-excuses-app/modifying-main-wasp-file.md Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com> * Update the section names * Update CONTRIBUTING.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/tutorials/dev-excuses-app/updating-main-page-js-file.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/contributing.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/contributing.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/contributing.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Add Discord component and some minor fixes * Add unclear notice * Update web/docs/pick-a-tutorial.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/tutorials/dev-excuses-app.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/tutorials/dev-excuses-app.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/tutorials/dev-excuses-app.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update CONTRIBUTING.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/tutorials/dev-excuses-app/modifying-main-wasp-file.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/getting-started.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/tutorials/dev-excuses-app/modifying-main-wasp-file.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Review updates * Update web/docs/tutorials/dev-excuses-app/modifying-main-wasp-file.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update suggestion * Update web/docs/tutorials/dev-excuses-app/perform-migration-and-run.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update web/docs/tutorials/dev-excuses-app/perform-migration-and-run.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update * Update web/docs/tutorials/dev-excuses-app/updating-main-page-js-file.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update CONTRIBUTING.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Remove lines * Update CONTRIBUTING.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update CONTRIBUTING.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update CONTRIBUTING.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update getExcuse function * Update page naming convention * Remove Main.css * Update migration text * Update image * Update "Wasp's compiler is built with Haskell" * Add tutorials description * Update web/docs/pick-a-tutorial.md Co-authored-by: Matija Sosic <matija.sosic@gmail.com> * Update Discord link and tutorial ending * Update links Co-authored-by: mkhamrov <mkhamrov@redhat.com> Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com> Co-authored-by: Matija Sosic <matija.sosic@gmail.com>
2022-09-30 15:31:09 +03:00
---
id: 03-adding-operations
title: Adding operations
---
import useBaseUrl from '@docusaurus/useBaseUrl';
Now you'll need to create two files: `actions.js` and `queries.js` in the `ext` folder.
Lets add `saveExcuse()` action to our `actions.js` file. This action will save the text of our excuse to the database.
```js title=".../ext/actions.js | Defining an action"
export const saveExcuse = async (excuse, context) => {
return context.entities.Excuse.create({
data: { text: excuse.text }
})
}
```
Then we need to create two queries in the `queries.js` file. First, one `getExcuse` will call an external API and fetch a new excuse. The second one, named `getAllSavedExcuses`, will pull all the excuses weve saved to our database.
```js title=".../ext/queries.js | Defining queries"
import axios from 'axios';
export const getExcuse = async () => {
const response = await axios.get('https://api.devexcus.es/')
return response.data
}
export const getAllSavedExcuses = async (_args, context) => {
return context.entities.Excuse.findMany()
}
```
Thats it! We finished our back-end. 🎉 Now, lets use those queries/actions on our UI.