4IT580: Docs
4IT580 WebGithub

5th Practical Class:
Persistent Backend!

Detailed look at GraphQL server set-up

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

What is going on here:

Connect 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

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 CRUD

Resolver / controller

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 control the business logic of app.

Ideal resolver could look like this:

Error handling in GraphQL

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 control over the app, he can sent what he wants. FE side validation are there for better user experience, BE validations are for security.

How to work with DB on your project