Skip to main content

Function Resolver

name​

Type​

string!

Values​

function:run


options​

The options object contains configuration specific to the type of resolver you select.


args​

Type​

ParameterConfig?

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​

ParameterConfig?

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.

Your code can either be written "inline", e.g., ({sum: 1 + 1}) or as a valid ESM, e.g., export default function() { return {sum: 1 + 1}; }.

For inline code the FunctionResolverContext is present as globals, e.g., ({sum: $args.input + 42}), while for ESM code the context is exposed as the args object to the entrypoint function, e.g., export default function({$args}) { return {sum: $args.input + 42}; }.

Imports​

When writing ESM code you can use remote imports, similar to what you'd use in a browser. For example, the code below will make use of the htmlparser2 library.

import {parseDocument} from 'https://esm.sh/htmlparser2@9.1.0';

export default function({$args}) {
return parseDocument(\\`<span>hello \${$args.input}</span>\\`).firstChild.firstChild.data;
}

System-Cached Imports​

A few libraries are cached and can be imported without a network request. Note that the import URL must match this URL exactly to take advantage of the system-cached library imports.

If you would like to see additional libraries cached, please open a support request.

warning

Because non-cached imports can have a significant and unpredictable impact on performance, we recommend only using system-cached imports in production.

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": {
"type": "object",
"properties": {
"name": { "type": "string" }
}
},
"shape": "Score"
},
"shapes": {
"Score": {
"name": "Score",
"title": "Score",
"schema": {
"type": "object",
"properties": {
"score": {
"type": "string"
}
}
}
}
}
}
export default function ({ $args, $results, $resultsById, $resultsByIndex }) {
const { name } = $args;
const sum = $resultsById.second.value + $resultsByIndex[0].value + $results.value;
return { score: `${name}: ${sum}` };
}
{
getScore(name: 'Beagles') {score}
}
{
"data": {
"getScore": { "score": "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": "export default 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.