4IT580: Docs
4IT580 WebGitLab

4th Practical Class:
GraphQL and BackEnd!

Few links

Client ↔️ Server communication

Terms explanation

Note that alternatives listed here are far from being complete list. And some of the technologies have different purposes - aka can be used to do same stuff, but are mainly build to do some other stuff.

API

Aplication Programming Interface - application designed to provide data to it's consumers. The data are usually provided wia some data-transfer protocol, and being really common. API application is commonly called or , it's consumers are commonly called or . There are many ways how to build API's, each fitting different purpose. Quite commonly used are and API patterns.

Node.js

Node.js is out of browser implementation of EcmaScript specification, using Chrome V8 runtime. It can be used for scripting, server implementation, desktop app creation. Visual Studio Code and Atom are both running using Node.js.

Express

Express is simple HTTP server running on . It simplifies work with HTTP requests, using approach. In node.js ecosystem alternatives would be , , . Alternatives outside of JS ecosystem would be , , . And bunch of cloud based HTTP services like , .

GraphQL

GraphQL is query language specification, used to build API on top of your data. Focus of today's lesson :)

Be aware that GraphQL does not have to be suitable in all cases for you. For some use cases REST for example might be a better option.

GraphQL vs REST

(according to AWS)

Topic

REST

GraphQL

What is it?

set of rules that defines structured data exchange between a client and a server

query language, architecture style, and set of tools to create and manipulate APIs

Best suited for

good for simple data sources where resources are well defined

good for large, complex, and interrelated data sources

Data access

multiple endpoints in the form of URLs to define resources

single URL endpoint
Data returned

fixed structure defined by the server

flexible structure defined by the client

How data is structured and defined

weakly typed, so the client must decide how to interpret the formatted data when it is returned

strongly typed, so the client receives data in predetermined and mutually understood formats

Error checking

client must check if the returned data is valid

invalid requests are typically rejected by schema structure, this results in an autogenerated error message

HTTP response

uses HTTP response status codes

always response with , and respond with only if there is some code error and server crashes

Apollo server

One of many JS implementation of GraphQL.

Basics of GraphQL

GraphQL properties

GraphQL Server example:

You can try out following examples in this codesandbox.

Here is an example of a simple standalone Apollos server.

-> Click on Open Sandbox (bottom right corner of the example).


Example of the Query for the example server:

Result in simple string response:

Schema definition language (SDL)

Typed human-readable language for defining the schema, which defined types and their relations.

Example of defining MathResults type, that represent operations on two given integers.

Resolvers for given schema:

And finally query that gets only addition. Notice that you can always only ask for what you need.

The arguments in the query are filled when calling the query. In the resolver, the argument has to be extracted and used for computations.

-> Click on Open Sandbox (bottom right corner of the example).

Resolvers

Each resolver has to be a function that returns the schema defined type for single field.

Resolver example with all arguments:

Each resolver has 4 optional arguments, now we focus on the argument that is an object containing all the arguments defined in the schema.

See Apollo documentation for more info about the schema definition and resolvers.

Nullability of fields

Following example prevents null for each computation result.

Nullability for arrays/lists can prevent the whole list to be null and always be at least empty list, or prevent its items to be null.

Data flow Client -> server and back

Data flow inside GraphQL server

How to work with GraphQL server schema