How can I use the GraphQL API to update rules on an Asset?

In this post I will cover how to pull the rules on an asset (in this case we are updating the constants on a proportional threshold rule) and then how to update the rule as well, all through the GraphQL API. Please note that this guide is for a basic threshold rule and more complex rules may have more adjusting needed to the queries and/or mutations. Please reach out to support@exosite.com for any assistance if needed.

You may use the example below for your Query to pull the existing rules on an asset:

query assetInsightRulez($assetId: ID!) {
    asset(id: $assetId) {
        rulez {
            category
            id
            name
            insight_id
            subscribes {
                id
                name
            }
            constants
        }
    }
}
//Variables
{
"assetId": "Your asset ID"
}

Please substitute in your asset ID that you are working with.

You should have a returned message that looks like the following:

{
	"data": {
		"asset": {
			"name": "Simulator Asset",
			"rulez": [
				{
					"subscribes": [
						{
							"tag": "A",
							"id": "ID A"
						},
						{
							"tag": "B",
							"id": "ID B"
						}
					],
					"insight_id": "Insight ID",
					"group_id": "",
					"id": "ID for Mutation",
					"category": "default",
					"function_id": "Function ID",
					"name": "Proportional Rule",
					"constants": {
						"Warning_Proportion": [
							20
						],
						"Critical_Proportion": [
							50
						]
					}
				}
			]
		}
	}
}

In your returned message, the “ID A”, “Insight ID”, etc. fields will all be filled out with actual IDs that we will be using in the mutation section below.

Now that we have the information for the rule we want to update, we can put it through a GraphQL mutation using the following template:

// Query (mutation)
mutation updateInsightRule($id: ID!, $rule: InsightRuleInput) {
    updateInsightRule(id: $id, rule: $rule) {
        category
        id
        name
        subscribes {
            id
            name
        }
        constants
    }
}
// Variables
{
    "id": "ID For Mutation (from above query)",
    "rule": {
        "name": "[Rule Name]",
        "subscribes": [
			{
				"tag": "B",
				"id": "ID B"
			},
			{
				"tag": "A",
				"id": "ID A"
			}
		],
        "insight_id": "Insight ID from above",
        "group_id": "",
        "function_id": "Function ID from above",
        "constants": {
            "Warning_Proportion": [New Warning Proportion Goes Here],
            "Critical_Proportion": [New Critical Proportion Goes Here]
        }
    }
}

Once you send that mutation through you should be able to verify that these constants have been updated in ExoSense! You can use these templates and these steps to help change various asset rules via GraphQL now.