Skip to content

Commit

Permalink
docs: update quickstarts and tutorials for sbt-revolver plugin added (z…
Browse files Browse the repository at this point in the history
…io#9195)

* update quickstarts of sbt-revolver plugin added

* update tutorials
  • Loading branch information
varshith257 authored Nov 1, 2024
1 parent 2c1aef7 commit acc5763
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 38 deletions.
31 changes: 22 additions & 9 deletions docs/guides/quickstarts/graphql-webservice.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ sidebar_label: "GraphQL Web Service"
---

This quickstart shows how to build a GraphQL web service using ZIO. It uses

- [ZIO HTTP](https://zio.dev/zio-http/) for the HTTP server
- [Caliban](https://ghostdogpr.github.io/caliban/) for the GraphQL

Expand All @@ -14,7 +15,7 @@ Creating GraphQL API using Caliban is an easy process. We can define our data mo

First, open the console and clone the [ZIO Quickstarts](https://github.com/zio/zio-quickstarts) project using `git` (or you can simply download the project) and then change the directory:

```bash
```bash
$ git clone https://github.com/zio/zio-quickstarts.git
$ cd zio-quickstarts/zio-quickstart-graphql-webservice
```
Expand All @@ -25,15 +26,27 @@ Once you are inside the project directory, run the application:
sbt run
```

Alternatively, to enable hot-reloading and prevent port binding issues, you can use:

```bash
sbt reStart
```

:::note
If you encounter a "port already in use" error, you can use `sbt-revolver` to manage server restarts more effectively. The `reStart` command will start your server and `reStop` will properly stop it, releasing the port.

To enable this feature, we have included `sbt-revolver` in the project. For more details on this, refer to the [ZIO HTTP documentation on hot-reloading](https://zio.dev/zio-http/installation#hot-reload-changes-watch-mode).
:::

## Testing The Quickstart

In this project, we have defined models of our employees with their names and roles. Then using GraphQL annotations, we asked Caliban to derive the GraphQL schema from these models.

So we can query all the employees that are software developers using the GraphQL query:

```graphql
query{
employees(role: SoftwareDeveloper){
query {
employees(role: SoftwareDeveloper) {
name
role
}
Expand All @@ -50,15 +63,15 @@ The response will be as follows:

```json
{
"data" : {
"employees" : [
"data": {
"employees": [
{
"name" : "Maria",
"role" : "SoftwareDeveloper"
"name": "Maria",
"role": "SoftwareDeveloper"
},
{
"name" : "Peter",
"role" : "SoftwareDeveloper"
"name": "Peter",
"role": "SoftwareDeveloper"
}
]
}
Expand Down
14 changes: 13 additions & 1 deletion docs/guides/quickstarts/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This is the simplest quickstart for a ZIO application. You can download and run

First, open the console and clone the [ZIO Quickstarts](https://github.com/zio/zio-quickstarts) project using `git` (or you can simply download the project) and then change the directory:

```bash
```bash
$ git clone https://github.com/zio/zio-quickstarts.git
$ cd zio-quickstarts/zio-quickstart-hello-world
```
Expand All @@ -21,6 +21,18 @@ Once you are inside the project directory, run the application:
sbt run
```

Alternatively, to enable hot-reloading and prevent port binding issues, you can use:

```bash
sbt reStart
```

:::note
If you encounter a "port already in use" error, you can use `sbt-revolver` to manage server restarts more effectively. The `reStart` command will start your server and `reStop` will properly stop it, releasing the port.

To enable this feature, we have included `sbt-revolver` in the project. For more details on this, refer to the [ZIO HTTP documentation on hot-reloading](https://zio.dev/zio-http/installation#hot-reload-changes-watch-mode).
:::

## Testing The Quickstart

When we prompt the `sbt run` the sbt search for the executable class and will find the `zio.dev.quickstart.MainApp` which contains the following code:
Expand Down
12 changes: 12 additions & 0 deletions docs/guides/quickstarts/restful-webservice.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ Once you are inside the project directory, run the application:
sbt run
```

Alternatively, to enable hot-reloading and prevent port binding issues, you can use:

```bash
sbt reStart
```

:::note
If you encounter a "port already in use" error, you can use `sbt-revolver` to manage server restarts more effectively. The `reStart` command will start your server and `reStop` will properly stop it, releasing the port.

To enable this feature, we have included `sbt-revolver` in the project. For more details on this, refer to the [ZIO HTTP documentation on hot-reloading](https://zio.dev/zio-http/installation#hot-reload-changes-watch-mode).
:::

## Testing The Quickstart

In this quickstart, we will build a RESTful web service that has the following Http apps:
Expand Down
25 changes: 16 additions & 9 deletions docs/guides/tutorials/build-a-graphql-webservice.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: "Tutorial: How to Build a GraphQL Web Service"
sidebar_label: "Building a GraphQL Web Service"
---

Having GraphQL APIs enables the clients the ability to query the exact data they need. This powerful feature makes GraphQL more flexible than RESTful APIs.
Having GraphQL APIs enables the clients the ability to query the exact data they need. This powerful feature makes GraphQL more flexible than RESTful APIs.

Instead of having endpoints for our resources, the GraphQL API only provides a set of types and fields in terms of schemas. The client can ask for this schema, and that will help the client to know what kind of data they can expect from the server, and finally, the client can use the schema to build its queries.

Expand Down Expand Up @@ -88,6 +88,7 @@ case class Queries(
## Running the GraphQL Server

After defining all the queries, in order to serve the GraphQL API, we need to perform the following steps:

1. Create a `GraphQLInterpreter` instance, which is a wrapper around the _GraphQL API_. It allows us to add some middleware around the query execution.
2. Create an `HttpApp` instance from the `GraphQLInterpreter` instance. We can do this by using the `ZHttpAdapter.makeHttpService` defined in the `caliban-zio-http` module.
3. Serve the resulting `HttpApp` instance using the `Server.start` method of the _ZIO HTTP_ module.
Expand Down Expand Up @@ -126,6 +127,12 @@ object MainApp extends zio.ZIOAppDefault {
}
```

:::note
If you encounter a "port already in use" error, you can use `sbt-revolver` to manage server restarts more effectively. The `reStart` command will start your server and `reStop` will properly stop it, releasing the port.

To enable this feature, we have included `sbt-revolver` in the project. For more details on this, refer to the [ZIO HTTP documentation on hot-reloading](https://zio.dev/zio-http/installation#hot-reload-changes-watch-mode).
:::

## Effectful Queries

In the previous section, we used an in-memory data structure to store the data. But, in real-world applications we usually want to perform some kind of effectful queries to retrieve the data from the database. In such cases, we can use queries that return `ZIO` values:
Expand All @@ -148,8 +155,8 @@ In this project, we have defined models of our employees with their names and ro
So we can query all the employees that are software developers using the GraphQL query:

```graphql
query{
employees(role: SoftwareDeveloper){
query {
employees(role: SoftwareDeveloper) {
name
role
}
Expand All @@ -166,15 +173,15 @@ The response will be as follows:

```json
{
"data" : {
"employees" : [
"data": {
"employees": [
{
"name" : "Maria",
"role" : "SoftwareDeveloper"
"name": "Maria",
"role": "SoftwareDeveloper"
},
{
"name" : "Peter",
"role" : "SoftwareDeveloper"
"name": "Peter",
"role": "SoftwareDeveloper"
}
]
}
Expand Down
14 changes: 10 additions & 4 deletions docs/guides/tutorials/build-a-restful-webservice.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ We have the same constructor for failures called `Http.fail`. It creates a `Hand
import zio._
import zio.http._

val app: Handler[Any, Response, Any, Nothing] =
val app: Handler[Any, Response, Any, Nothing] =
handler(ZIO.fail(Response.internalServerError("Something went wrong")))
```

Expand Down Expand Up @@ -157,7 +157,7 @@ Let's see an example of how to pattern match on incoming requests:
import zio.http._

val httpApp: Route[Any, Nothing] =
Method.GET / "greet" / string("name") ->
Method.GET / "greet" / string("name") ->
handler { (name: String, _: Request) =>
Response.text(s"Hello $name!")
}
Expand All @@ -170,7 +170,7 @@ import zio._
import zio.http._

val httpApp: Route[Any, Response] =
Method.GET / "greet" ->
Method.GET / "greet" ->
handler { (req: Request) =>
if (req.url.queryParams.nonEmpty)
ZIO.succeed(Response.text(s"Hello ${req.url.queryParams("name").mkString(" and ")}!"))
Expand All @@ -193,6 +193,12 @@ object Server {
}
```

:::note
If you encounter a "port already in use" error, you can use `sbt-revolver` to manage server restarts more effectively. The `reStart` command will start your server and `reStop` will properly stop it, releasing the port.

To enable this feature, we have included `sbt-revolver` in the project. For more details on this, refer to the [ZIO HTTP documentation on hot-reloading](https://zio.dev/zio-http/installation#hot-reload-changes-watch-mode).
:::

## Greeting App

First, we need to define a request handler that will handle `GET` requests to the `/greet` path:
Expand All @@ -212,7 +218,7 @@ object GreetingRoutes {
s"Hello ${req.url.queryParams("name").map(_.mkString(" and "))}!"
)
)
else
else
ZIO.fail(Response.badRequest("The name query parameter is missing!"))
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ In [this article](enable-logging-in-a-zio-application.md), we enabled logging fo

To run the code, clone the repository and checkout the [ZIO Quickstarts](http://github.com/zio/zio-quickstarts) project:

```bash
```bash
$ git clone https://github.com/zio/zio-quickstarts.git
$ cd zio-quickstarts/zio-quickstart-restful-webservice-custom-logger
```
Expand All @@ -27,6 +27,18 @@ And finally, run the application using sbt:
$ sbt run
```

Alternatively, to enable hot-reloading and prevent port binding issues, you can use:

```bash
sbt reStart
```

:::note
If you encounter a "port already in use" error, you can use `sbt-revolver` to manage server restarts more effectively. The `reStart` command will start your server and `reStop` will properly stop it, releasing the port.

To enable this feature, we have included `sbt-revolver` in the project. For more details on this, refer to the [ZIO HTTP documentation on hot-reloading](https://zio.dev/zio-http/installation#hot-reload-changes-watch-mode).
:::

## Creating a Custom Logger

To create a new logger for the ZIO application, we need to create a new `ZLogger` object. The `ZLogger` is a trait that defines the interface for a ZIO logger. The default logger has implemented this trait through the `ZLogger.default` object.
Expand Down Expand Up @@ -145,6 +157,7 @@ And then we can run the application again:
It works! Now, our ZIO application uses SLF4J for its logging backend.

Similarly, we can bind our application to any other logging framework by adding the appropriate dependency to our `build.sbt` file:

- `slf4j-log4j12`
- `slf4j-reload4j`
- `slf4j-jdk14`
Expand All @@ -160,7 +173,7 @@ To use the `reload4j` logging framework, we need to add the following dependenci

```diff
- libraryDependencies += "org.slf4j" % "slf4j-simple" % "1.7.36"
+ libraryDependencies += "org.slf4j" % "slf4j-reload4j" % "1.7.36"
+ libraryDependencies += "org.slf4j" % "slf4j-reload4j" % "1.7.36"
```

Now we can configure our logger by adding the `log4j.properties` to the resources directory:
Expand Down
20 changes: 19 additions & 1 deletion docs/guides/tutorials/deploy-a-zio-application-using-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,29 @@ In [this quickstart](../quickstarts/restful-webservice.md), we developed a web s

To access the code examples, you can clone the [ZIO Quickstarts](http://github.com/zio/zio-quickstarts) project:

```bash
```bash
$ git clone https://github.com/zio/zio-quickstarts.git
$ cd zio-quickstarts/zio-quickstart-restful-webservice-dockerize
```

Once you are inside the project directory, run the application:

```bash
sbt run
```

Alternatively, to enable hot-reloading and prevent port binding issues, you can use:

```bash
sbt reStart
```

:::note
If you encounter a "port already in use" error, you can use `sbt-revolver` to manage server restarts more effectively. The `reStart` command will start your server and `reStop` will properly stop it, releasing the port.

To enable this feature, we have included `sbt-revolver` in the project. For more details on this, refer to the [ZIO HTTP documentation on hot-reloading](https://zio.dev/zio-http/installation#hot-reload-changes-watch-mode).
:::

## Prerequisites

Before we can dockerize our web service, we need to [download and install Docker](https://docs.docker.com/get-docker/). So we assume that the reader has already installed Docker.
Expand Down
16 changes: 14 additions & 2 deletions docs/guides/tutorials/enable-logging-in-a-zio-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ In [this quickstart](../quickstarts/restful-webservice.md), we developed a web s

To access the code examples, you can clone the [ZIO Quickstarts](http://github.com/zio/zio-quickstarts) project:

```bash
```bash
$ git clone https://github.com/zio/zio-quickstarts.git
$ cd zio-quickstarts/zio-quickstart-restful-webservice-logging
```
Expand All @@ -25,6 +25,18 @@ And finally, run the application using sbt:
$ sbt run
```

Alternatively, to enable hot-reloading and prevent port binding issues, you can use:

```bash
sbt reStart
```

:::note
If you encounter a "port already in use" error, you can use `sbt-revolver` to manage server restarts more effectively. The `reStart` command will start your server and `reStop` will properly stop it, releasing the port.

To enable this feature, we have included `sbt-revolver` in the project. For more details on this, refer to the [ZIO HTTP documentation on hot-reloading](https://zio.dev/zio-http/installation#hot-reload-changes-watch-mode).
:::

## Default Logger

ZIO has a default logger that prints any log messages that are equal, or above the `Level.Info` level:
Expand Down Expand Up @@ -153,7 +165,7 @@ trait UserRepo {
def register(user: User): Task[String]

def lookup(id: String): Task[Option[User]]

def users: Task[List[User]]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ZIO has a built-in metric system that allows us to monitor the performance of ou

To access the code examples, you can clone the [ZIO Quickstarts](http://github.com/zio/zio-quickstarts) project:

```bash
```bash
$ git clone https://github.com/zio/zio-quickstarts.git
$ cd zio-quickstarts/zio-quickstart-restful-webservice-metrics
```
Expand All @@ -23,6 +23,18 @@ And finally, run the application using sbt:
$ sbt run
```

Alternatively, to enable hot-reloading and prevent port binding issues, you can use:

```bash
sbt reStart
```

:::note
If you encounter a "port already in use" error, you can use `sbt-revolver` to manage server restarts more effectively. The `reStart` command will start your server and `reStop` will properly stop it, releasing the port.

To enable this feature, we have included `sbt-revolver` in the project. For more details on this, refer to the [ZIO HTTP documentation on hot-reloading](https://zio.dev/zio-http/installation#hot-reload-changes-watch-mode).
:::

## Trying a Simple Example

Before going to apply the metrics in our application, let's try a simple example:
Expand Down Expand Up @@ -74,7 +86,7 @@ trait UserRepo {
def register(user: User): Task[String]

def lookup(id: String): Task[Option[User]]

def users: Task[List[User]]
}

Expand Down Expand Up @@ -201,7 +213,7 @@ object MainApp extends ZIOAppDefault {

// An layer responsible for storing the state of the `counterApp`
ZLayer.fromZIO(Ref.make(0)),

// To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead
InmemoryUserRepo.layer,

Expand Down Expand Up @@ -298,4 +310,4 @@ Now that we have the metrics as a REST API, we can add this endpoint to our Prom

In this tutorial, we have learned how to define metrics and apply them to our application. We have also learned how to provide the metrics as a REST API which then can be polled by a Prometheus server.

All the source code associated with this article is available on the [ZIO Quickstart](http://github.com/zio/zio-quickstarts) on GitHub.
All the source code associated with this article is available on the [ZIO Quickstart](http://github.com/zio/zio-quickstarts) on GitHub.
Loading

0 comments on commit acc5763

Please sign in to comment.