In the previous post, we have discussed the @pattern directive to define a pattern for a string.
Now, let’s move on to @computed directive to calculate values of a field using an ArcQL query.
directive @computed(
query: String
type: String
postQueryFn: String
) on FIELD | FIELD_DEFINITION
- Use the
@computed
directive to calculate values of a field using an ArcQL query.query
argument defines the ArcQL query. -
type
argument defines the data type. - Use a groovy-like function to combine the values in the fields. Use
postQueryFn
to define this function. It basically formats the output to combine values from different fields. - Combine String or Date values to match your requirements.
We will work with the below schema. We have defined various fields using @computed
directive.
type ComputedDirective {
#@computed directive
str: String
strNew: String @computed(query: "* SORT str ASC",
type: "ComputedDirective", postQueryFn: """res[0].str + "+" + res[1].str""")
strAugmented: String @computed(postQueryFn:
"""self.str + " Tink" """)
date: String @computed(postQueryFn:
"""import java.time.*;ZonedDateTime zone =ZonedDateTime.now();return zone.plus(Period.ofDays(2))""")
strCombine: String @computed(postQueryFn:
"""self.hypi.id + ":" + self.str """)
strCheck: [String] @computed(query: "* SORT str ASC",
type: "ComputedDirective", postQueryFn: """res.str""")
}
Insert a few values in the str
field. Other field values will get automatically calculated and updated.
-
strNew
value is the String formed by combining the sorted values ofstr
with a ‘+’ sign. Here, only the first and second values of the SORT result (res) are used to compute thestrNew
value. -
strAugmented
has thestr
value of the current object (self) appended withTink
. -
date
has the Groovy function to calculate the date after two days. -
strCombine
combines thehypi.id
andstr
value of the current object. -
strCheck
returns all the values of thestr
field in the Ascending order.
Let’s insert a few values in the str
field and create objects.
mutation {
upsert(
values: {
ComputedDirective: [
{ str: "hypi"}
{ str: "low code" }
{ str: "easy to use backend" }
]
}
) {
id
}
}
#result
{
"data": {
"upsert": [
{
"id": "01FPCN5DJ63RZ8FYHTFZJ4X3TG"
},
{
"id": "01FPCN5DJ85VQPQ2JVD5VGGH2X"
},
{
"id": "01FPCN5DJA1MVATSH7D7FS4ZJY"
}
]
}
}
Sample Query
Now find out the Computed values using the find
function.
{
find(type: ComputedDirective, arcql: "*") {
edges {
node {
... on ComputedDirective {
str
strNew
strAugmented
strCombine
date
strCheck
}
}
cursor
}
}
}
#result
{
"data": {
"find": {
"edges": [
{
"node": {
"str": "hypi",
"strNew": "easy to use backend+hypi",
"strAugmented": "hypi Tink",
"strCombine": "01FPCN5DJ63RZ8FYHTFZJ4X3TG:hypi",
"date": "2021-12-10T09:14:37.037939Z[UTC]",
"strCheck": [
"easy to use backend",
"hypi",
"low code"
]
},
"cursor": "01FPCN5DJ63RZ8FYHTFZJ4X3TG"
},
{
"node": {
"str": "low code",
"strNew": "easy to use backend+hypi",
"strAugmented": "low code Tink",
"strCombine": "01FPCN5DJ85VQPQ2JVD5VGGH2X:low code",
"date": "2021-12-10T09:14:37.173944Z[UTC]",
"strCheck": [
"easy to use backend",
"hypi",
"low code"
]
},
"cursor": "01FPCN5DJ85VQPQ2JVD5VGGH2X"
},
{
"node": {
"str": "easy to use backend",
"strNew": "easy to use backend+hypi",
"strAugmented": "easy to use backend Tink",
"strCombine": "01FPCN5DJA1MVATSH7D7FS4ZJY:easy to use backend",
"date": "2021-12-10T09:14:37.326987Z[UTC]",
"strCheck": [
"easy to use backend",
"hypi",
"low code"
]
},
"cursor": "01FPCN5DJA1MVATSH7D7FS4ZJY"
}
]
}
}
}