Function Resolver
name
Type
string!
Values
function:run
options
The options object contains configuration specific to the type of resolver you select.
args
Type
Description
Allows mapping from the QueryContext to the args values (input arguments) for the resolver. Results are not serialized. For more information read about the ParameterConfig.
results
Type
Description
Allows mapping from the QueryContext to the results for the resolver step in a ComposeResolver or whole query. Results are not serialized. For more information read about the ParameterConfig.
javascript
Type
string!
Description
Allows writing arbitrary Javascript code with access to the FunctionResolverContext.
This code must include a function named handler which will be executed in a sandbox. hander is passed the FunctionResolverContext.
Utilities
Utility function are provided in the sandboxed runtime.
These include:
_.get - lodash get
_.set - lodash set
Javascript Function Example
{
"getScore": {
"resolver": {
"compose": [
{
"id": "first",
"name": "util:noop",
"value": {
"value": 1
}
},
{
"id": "second",
"name": "util:noop",
"value": {
"value": 2
}
},
{
"name": "function:run",
"javascript": "// see below"
}
]
},
"args": {
"name": {
"type": "string"
}
}
}
}
function handler({
$args,
$results,
$resultsByIndex
}) {
const {name} = $args;
const sum = $resultsById.second.value + $resultsByIndex[0].value + $results.value;
return \\`\${name}: \${sum}\\`;
}
{
getScore(name: 'Beagles')
}
{
"data": {
"getScore": "Beagles: 5"
}
}
Schema Example
Using https://api.weather.gov/gridpoints/OKX/36,34/forecast as a data source.
{
"properties": {
"units": "us",
"forecastGenerator": "BaselineForecastGenerator",
"generatedAt": "2024-07-02T17:33:30+00:00",
"updateTime": "2024-07-02T16:32:21+00:00",
"validTimes": "2024-07-02T10:00:00+00:00/P7DT3H",
"elevation": {
"unitCode": "wmoUnit:m",
"value": 18.897600000000001
},
"periods": [
{
"number": 1,
"name": "This Afternoon",
"startTime": "2024-07-02T13:00:00-04:00",
"endTime": "2024-07-02T18:00:00-04:00",
"isDaytime": true,
"temperature": 84,
"temperatureUnit": "F",
"temperatureTrend": "",
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": null
},
"windSpeed": "5 to 9 mph",
"windDirection": "SE",
"icon": "/icons/land/day/few?size=medium",
"shortForecast": "Sunny",
"detailedForecast": "Sunny. High near 84, with temperatures falling to around 80 in the afternoon. Southeast wind 5 to 9 mph."
}
]
}
}
{
"getForecast": {
"resolver": {
"compose": [
{
"id": "weather",
"name": "rest:get",
"service": "weather",
"path": "/gridpoints/OKX/36,34/forecast"
},
{
"name": "function:run",
"javascript": "function handler({ $resultsById }) { const period = $resultsById.weather.properties.periods[0]; return `${period.name} will be ${period.temperature}${period.temperatureUnit}.`}"
}
]
}
}
}
query {
getForecast
}
{
"data": {
"getForecast": "This Afternoon will be 84F."
}
}
Function Resolver Context
A special context available to the function resolver. Contains the follow properties.
$args
See the definition
$claims
See the definition
$source
See the definition
$request
See the definition
$resolver
Type
FunctionResolver!
Description
The resolver config that is running this function.
$first
Type
any!
Description
The results of the first resolver to execute.
$results
Type
any!
Description
The results of the last (most recent or previous) resolver to execute.
$resultsById
Type
Record<string, any>!
Description
The results of a resolver by the provided id.
$resultsByIndex
Type
Array<any>!
Description
An array of zero-indexed resolver results.