以下の内容はhttps://www.pandanoir.info/entry/2018/12/11/180453より取得しました。


NodeでGraphQLのリクエストを出す

superagentというパッケージを利用します。

const request = require('superagent');

request
    .post('http://localhost:8080')
    .send(`
query {
    user(name: "香風智乃") {
        name, age
    }
}`)
    .end((_, res) => {
        console.log(JSON.parse(res.text));
    });

めちゃくちゃカンタンですよね。

おまけ: PHPでアクセスする方法

適宜php-curlをインストールしたり、php.iniをいじったりしてください。

<?php
$url = "http://localhost:8080";

$curl = curl_init($url);

$options = array(
  CURLOPT_HTTPHEADER => array( 'Accept: application/json' ),
  CURLOPT_POST => true,
  CURLOPT_POSTFIELDS => 'query {user(name: "香風智乃"){name, age}}',
  CURLOPT_RETURNTRANSFER => true,
);

curl_setopt_array($curl, $options);

$result = curl_exec($curl);

echo $result;

curl_close($curl);



以上の内容はhttps://www.pandanoir.info/entry/2018/12/11/180453より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14