How to execute multiplication using Maths API

We have seen how to execute division using the div function.

Let’s check how to multiply a field value with a number using Maths API.

  • Maths APIs work only on floats and integers, i.e. numerical values.
  • Mathematical operations can only be performed on an existing object.
  • Use the times function to multiply the field value of an object with any numerical constant.
  • The multiplication gets updated as the new field value.

We will work with the same schema.

type MathObj {
    intFld: Int
    floatFld: Float
}

Let’s insert a few records.

mutation {
  upsert(
    values: {
      MathObj: [
        { intFld: 12, floatFld: 24.24 }
        { intFld: 234, floatFld: 341.21 }
        { intFld: 3455435, floatFld: 1.1 }
       ]
    }
  ) {
    id
  }
}
#result
{
  "data": {
    "upsert": [
      {
        "id": "01FMSSFSS33NDE9DGFDZSCXNMV"
      },
      {
        "id": "01FMSSFSS7ECGX3WK36P2YGAGQ"
      },
      {
        "id": "01FMSSFSS9PSHN9SW0SE9DCHYE"
      }
    ]
  }
}

Sample Query

Multiply the intFld and floatFld values of Hypi objects with constants.

mutation {
  math(
    values: {
      MathObj: [
        { intFld: { hypi: { id: "01FMSSFSS33NDE9DGFDZSCXNMV" }, times: 4 } }
        { floatFld: { hypi: { id: "01FMSSFSS33NDE9DGFDZSCXNMV" }, times: 4 } }
        { intFld: { hypi: { id: "01FMSSFSS7ECGX3WK36P2YGAGQ" }, times: 2 } }
        { floatFld: { hypi: { id: "01FMSSFSS7ECGX3WK36P2YGAGQ" }, times: 2.1 } }
      ]
    }
  ) {
    id
  }
}
 
#result
{
  "data": {
    "math": [
      {
        "id": "01FMSSFSS33NDE9DGFDZSCXNMV"
      },
      {
        "id": "01FMSSFSS33NDE9DGFDZSCXNMV"
      },
      {
        "id": "01FMSSFSS7ECGX3WK36P2YGAGQ"
      },
      {
        "id": "01FMSSFSS7ECGX3WK36P2YGAGQ"
      }
    ]
  }
}

Let’s check the resultant values.

{
  find(type: MathObj, arcql: "*") {
    edges {
      node {
        ... on MathObj {
          intFld
          floatFld
        }
      }
      cursor
    }
  }
}
#result
 {
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "intFld": 48,
            "floatFld": 96.96
          },
          "cursor": "01FMSSFSS33NDE9DGFDZSCXNMV"
        },
        {
          "node": {
            "intFld": 468,
            "floatFld": 716.5409999999999
          },
          "cursor": "01FMSSFSS7ECGX3WK36P2YGAGQ"
        },
        {
          "node": {
            "intFld": 3455435,
            "floatFld": 1.1
          },
          "cursor": "01FMSSFSS9PSHN9SW0SE9DCHYE"
        }
      ]
    }
  }
}

Check the POSTMAN collection for the times query in different programming languages! Click </> and choose the programming language of your choice.
Don’t forget to insert your own Authorization key and Hypi Domain under Headers to test the results!
Run in Postman