Tutorial #3 - Combining and Querying Multiple Data Streams

This post is part of our introduction series. In the previous tutorials we worked with only one topic. In this tutorial you will learn how to combine and query multiple data streams.

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

We create two topics, product-topic and purchase-topic. See schema definition and data of product-topic in Tutorial #2.
> quick topic create product-topic -k long 
-v schema --schema product-schema.gql
Created new topic product-topic

> quick topic create purchase-topic -k string -v schema --schema purchase-schema.gql
Created new topic purchase-topic
type Purchase {
    purchaseId: String!,
    productId: Int!,
    userId: Int!,
    amount: Int,
    price: Price,
}

type Price {
    total: Float,
    currency: String
}
{
  "key": "abc",
  "value": {
    "purchaseId": "abc",
    "productId": 123,
    "userId": 2,
    "amount": 2,
    "purchasePrice": {
      "total": 39.98,
      "currency": "DOLLAR"
    }
  }
}, ...
In Quick, we connect different data streams using GraphQL. In our example, we create findPurchase to query purchases in purchase-topic. This query takes one argument, purchaseId. In the schema Purchase we add a new field product, adding product details from a different product-topic via the productId given in the purchase data.
type Query {
  findPurchase(purchaseId: String):
   Purchase @topic(name: "purchase-topic", keyArgument: "purchaseId")
}

type Purchase {
  purchaseId: String!,
  productId: Int!,
  userId: Int!,
  product: Product @topic(name: "product-topic", keyField: "productId")
  amount: Int,
  price: Price,
}

type Product {
  productId: Int!,
  name: String,
  description: String,
  price: Price,
  metadata: Metadata
}

type Price {
  total: Float,
  currency: String
}

type Metadata {
  created_at: Int,
  source: String
}
> quick gateway apply product-gateway -f schema.gql
Applied definition for gateway product-gateway

> curl -X POST --url https://$QUICK_URL/ingest/purchase-topic \
    --header 'content-type: application/json' \
    --header 'X-API-Key: $KEY' \
    --data "@./purchases.json"
We query both data streams with GraphQL and get the response in JSON format.
query {
   findPurchase(id: "abc") {
      purchase {
        product {
          productId
          name
        }
        purchaseId
        userId
        amount
        purchasePrice {
          total
          currency
        }
      }
   }
}
{"data":{"findPurchase":{"purchase":{"product":{"productId":123,"name":"T-Shirt"},"purchaseId":"abc","userId":2,"amount":2,"purchasePrice":{"total":39.98,"currency":"DOLLAR"}}}}}