How to execute subtraction using Maths API

We have seen how to execute multiplication using the times function.

Let’s check how to subtract a number from a field value 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 minus function to subtract any numerical constant from the field value of an object.
  • The subtraction 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": "01FMVVKX3THZK9HD8W83RFMDFS"
      },
      {
        "id": "01FMVVKX3XDWYEGM2RH1WVMFHK"
      },
      {
        "id": "01FMVVKX40VS9ZC2JFCPBCVA3G"
      }
    ]
  }
}

Sample Query

Subtract any number from the intFld and floatFld values of Hypi objects.

mutation {
  math(
    values: {
      MathObj: [
        { intFld: { hypi: { id: "01FMVVKX3XDWYEGM2RH1WVMFHK" }, minus: 2 } }
        { floatFld: { hypi: { id: "01FMVVKX3XDWYEGM2RH1WVMFHK" }, minus: 452  } }
        { intFld: { hypi: { id: "01FMVVKX40VS9ZC2JFCPBCVA3G" }, minus: 23443 } }
        { floatFld: { hypi: { id: "01FMVVKX40VS9ZC2JFCPBCVA3G" }, minus: 2242323.1 } }
      ]
    }
  ) {
    id
  }
}
#result
{
  "data": {
    "math": [
      {
        "id": "01FMVVKX3XDWYEGM2RH1WVMFHK"
      },
      {
        "id": "01FMVVKX3XDWYEGM2RH1WVMFHK"
      },
      {
        "id": "01FMVVKX40VS9ZC2JFCPBCVA3G"
      },
      {
        "id": "01FMVVKX40VS9ZC2JFCPBCVA3G"
      }
    ]
  }
}

Let’s check the resultant values.

 {
  find(type: MathObj, arcql: "*") {
    edges {
      node {
        ... on MathObj {
          intFld
          floatFld
        }
      }
      cursor
    }
  }
}
#result
{
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "intFld": 12,
            "floatFld": 24.24
          },
          "cursor": "01FMVVKX3THZK9HD8W83RFMDFS"
        },
        {
          "node": {
            "intFld": 232,
            "floatFld": -110.79000000000002
          },
          "cursor": "01FMVVKX3XDWYEGM2RH1WVMFHK"
        },
        {
          "node": {
            "intFld": 3431992,
            "floatFld": -2242322
          },
          "cursor": "01FMVVKX40VS9ZC2JFCPBCVA3G"
        }
      ]
    }
  }
}

Check the POSTMAN collection for the minus 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