How to save key-value pairs

In the earlier post, we have seen how to save user account details.

Let’s see how to save key-value pairs.

Key-value pair (KVP) is an effective way to store large quantities of data. It helps to easily organize and sort information.

Pair is an in-built data type that maps key-value pairs. Check Core app that includes Pair data type.

Use upsert function to insert key-value pair data.

mutation Upsert($values: HypiUpsertInputUnion!) {
  upsert(values: $values) {
    id
  }
}
#variables
{
  "values": {
    "Pair": [
      {
        "key": "colour",
        "value": "blue"
      }
    ]
  }
}
#result
{
  "data": {
    "upsert": [
      {
        "id": "01ge5046nqa7kj0tq7y6qgx8q8"
      }
    ]
  }
}

You can use find query to retrieve key-value pair data.

{
  find(type: Pair, arcql: "*") {
    edges {
      node {
        ... on Pair {
          key
          value
        }
      }
      cursor
    }
  }
}

# result

{
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "key": "colour",
            "value": "blue"
          },
          "cursor": "01ge5046nqa7kj0tq7y6qgx8q8"
        }
      ]
    }
  }
}

We will see few more inbuilt data types in the next post!