4IT580: Docs
4IT580 WebGithub

5th Practical Class:
Persistent Backend!

Conect to server

Mutation x Query differences

Query serves to provide data, without mutating (changing them). REST method would be GET Mutation servers for all data altering operations. REST methods would be POST, PUT, DELETE

Detailed look at GraphQL server set-up

In backend/src/index.js you have this:

What is going on here:

Input x Type

Type describes shape of data that will be provided by API - output shape Input describes shape of data that API will receive - input shape

Example Type

Example Input - full CUD

Code!

Let's stop talking and do some stuff!

In memory

DB insert

Edit

Validation

Why we should validate data on BE, when we are already doing validation on FE? Never thrust data sent over the internet! Client has full constroll over the app, he can sent what he wants. FE side validation are there for better user experience, BE validations are for security.

Resolver / controler

Eq the famous problem - where to put the logic?

Let's take Quack DB resolver as an example:

For a small and straight-forward functions, this code is OK, no need to optimize it. Let's have another example, this times BAD one:

As you can see, really long and ugly function. We can improve it by extracting logic to stand-alone functions:

fetchData and prepareQuery are named pretty generically, they need better names. But they could be extracted to separate files, and potentially reused in other resolvers or even other parts of app. Usually such files are called controller, since they controll the business logic of app.

Ideal resolver could look like this:

How to work with DB on your project