4IT580: Docs
4IT580 WebGitLab

5th Practical Class:
Persistent Backend!

Detailed look at GraphQL server set-up

In you have this:

What is going on here:

Connect to server

Mutations

The is then called similarly to :

Input x Type

Example Type

Example Input - full CRUD

Resolver

Resolver for mutations are same as for queries, they have to follow the schema, so accept defined arguments and returns the return type.

Here is an example from previous lesson with a mutation (try the code here):

Same as for more complex objects can be defined as arguments and return type.

Error handling in GraphQL

We can add simple condition to our previous code, where we use custom error and throw :

This custom handling changes just the error message. In reality we want better error handling and therefore use defined codes so we can react on them on our frontends.

It's also used with pre-defined error codes by Apollo Server, try passing a string instead of an integer.

This is the result error:

Notice the field. This one is preferred to be used for determination of the errors in order to react correctly.

To define our own codes, you pass them into the like this:

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 send what he wants. FE side validation are there for better user experience, BE validations are for security.

How to work with DB and structure project

Live coding

Now let's finish the Quacker mutations