How to execute addition using Maths API

We have seen how to execute subtraction using the minus function.

Let’s check how to add a number to 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 plus function to add any numerical constant to the field value of an object.
  • The addition 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": "01FN37H65YB4EMCXM3NEF31M2Z"
      },
      {
        "id": "01FN37H660RK3XKQ6VY042CB99"
      },
      {
        "id": "01FN37H662JPVAMBBZ388N7Q4T"
      }
    ]
  }
}

Sample Query

Add any numbers to the intFld and floatFld values of Hypi objects.

mutation {
  math(
    values: {
      MathObj: [
        { intFld: { hypi: { id: "01FN37H65YB4EMCXM3NEF31M2Z" }, plus: 72 } }
        {
          floatFld: { hypi: { id: "01FN37H65YB4EMCXM3NEF31M2Z" }, plus: 942.98 }
        }
        { intFld: { hypi: { id: "01FN37H662JPVAMBBZ388N7Q4T" }, plus: 23 } }
        {
          floatFld: {
            hypi: { id: "01FN37H662JPVAMBBZ388N7Q4T" }
            plus: 22423.1
          }
        }
      ]
    }
  ) {
    id
  }
}
#result
{
  "data": {
    "math": [
      {
        "id": "01FN37H65YB4EMCXM3NEF31M2Z"
      },
      {
        "id": "01FN37H65YB4EMCXM3NEF31M2Z"
      },
      {
        "id": "01FN37H662JPVAMBBZ388N7Q4T"
      },
      {
        "id": "01FN37H662JPVAMBBZ388N7Q4T"
      }
    ]
  }
} 

Let’s check the resultant values.

 {
  find(type: MathObj, arcql: "*") {
    edges {
      node {
        ... on MathObj {
          intFld
          floatFld
        }
      }
      cursor
    }
  }
}
#result
{
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "intFld": 84,
            "floatFld": 967.22
          },
          "cursor": "01FN37H65YB4EMCXM3NEF31M2Z"
        },
        {
          "node": {
            "intFld": 234,
            "floatFld": 341.21
          },
          "cursor": "01FN37H660RK3XKQ6VY042CB99"
        },
        {
          "node": {
            "intFld": 3455458,
            "floatFld": 22424.2
          },
          "cursor": "01FN37H662JPVAMBBZ388N7Q4T"
        }
      ]
    }
  }
} 

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