GraphQL -- proof of concept

I didn’t have an actual need. Just needed to see if I could do it too.

SNIPPET:

$tpl = $modx->getOption('tpl',$scriptProperties,'tpl.GraphQL_results');
$endpoint = $modx->getOption('endpoint',$scriptProperties,'https://countries.trevorblades.com/');

$query = '
{
  countries {
    code
    name
    currency
    continent {
      name
    }
}
}';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['query' => $query]));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

$response = curl_exec($curl);
curl_close($curl);

$json = json_decode($response, true);

$groups = array_keys($json['data']);

foreach ($groups as $group) {
    $results = $json['data'][$group];

    foreach ($results as $result) {
       $output .= $modx->getChunk($tpl,array($group => $result));
    };
};

return $output;

CHUNK:

<p><strong>[[+countries.code]]</strong> -- [[+countries.name]], [[+countries.continent.name]] ([[+countries.currency]])</p>

BTW anyone have any suggestions on making the query better? As a parameter?

Maybe you can look into this: graphql-php/data-fetching.md at 4af8a10541d183ba3aaa7e198101f8498a23185b · webonyx/graphql-php · GitHub