Tutorial #5 - How to Build Your First Application with Quick

In this tutorial you will learn how to build your first application with Quick. We develop a TinyURL app that shortens any given URL to a tiny token of your choice. Shortened URLs are quite helpful, for example, for people posting on Twitter. The app will also support some simple analytics and tracks the number of clicks.

You can find the commands and the schemas used for this tutorial in our quick-examples repository

To be able to use your TinyURL app you need to create your project first. To do that, open your terminal and initialize quick with the given $QUICK_HOST and $KEY.
> quick context create
Use the quick-cli tool to create a new topic tiny-url in which all (token, url) pairs will be stored. The topic is immutable, that means that there will be no two keys that are the same.
> quick topic create tiny-url \
    --key string \
    --value string --immutable
Created new topic tiny-url
> curl --request POST --url http://$QUICK_HOST/ingest/tiny-url/ 
 --header 'content-type: application/json' \
 --header 'X-API-Key: $KEY ' \
 --data '{"key": "d9p", "value": "https://d9p.io"}'
In order to count how many times someone fetched an url we develop an application tiny-url-counter. To use it we create input and output topics, track-fetch and count-fetch. With every url fetch we expect an ingest in the input topic, track-fetch.
> quick topic create track-fetch \
    --key string \
    --value string
Created new topic track-fetch

> quick topic create count-fetch \
    --key string \
    --value long
Created new topic count-fetch
> curl --request POST --url http://$QUICK_HOST/ingest/track-fetch/  \
 --header 'content-type: application/json' \
 --header 'X-API-Key: $KEY' \
 --data '{"key": "d9p", "value": ""}'
Quick supports built-in applications. We develop a counter application tiny-url-counter that returns how many times a key is stored in a topic. The application is written as Kafka Streams application with our streams-bootstrap library. You can find the full code in our repository quick-examples on GitHub. The counter application can be deployed using the quick-cli tool.
public void buildTopology(final StreamsBuilder builder) {

 final KStream inputStream =
  builder.stream(this.getInputTopic(), Consumed.with(Serdes.String(), Serdes.String()));

 inputStream
  .groupByKey(Grouped.with(Serdes.String(), Serdes.String()))
  .count()
  .toStream()
  .to(this.outputTopic, Produced.with(Serdes.String(), Serdes.Long()));
}
> quick app deploy tiny-url-counter --registry us.gcr.io/d9p-quick/demo \
--image tiny-url-counter \
--tag 1.0.0 \
--args input-topics=track-fetch output-topic=count-fetch productive=false
We create a new gateway tinyurl-gateway and apply our GraphQL schema on it.
type Query {
  fetchURL(token: String): TinyUrl
}
type TinyUrl {
  url: String @topic(name: "tiny-url", keyArgument: "token")
  count: Long @topic(name: "count-fetch", keyArgument: "token")
}
> quick gateway create tinyurl-gateway
Create gateway tinyurl-gateway (this may take a few seconds)
> quick gateway apply tinyurl-gateway -f schema.gql
Applied definition for gateway tinyurl-gateway
Finally, we fetch all information with GraphQL for a given token.
query {
   fetchURL(token: "d9p") {
      url
      count
   }
}
{"data":{"fetchURL":{"url":"https:\/\/d9p.io","count":2}}}