Как я могу POST GraphQL мутации в NodeJS? - PullRequest
0 голосов
/ 21 июня 2019

Я имею в виду процесс, указанный в https://develop.zendesk.com/hc/en-us/articles/360001331787 Запуск сеанса агента .Я сгенерировал токен доступа.Теперь, когда я пытаюсь выполнить POST-запрос, я получаю ответ Bad Request error.

Я использую облачную функцию Платежная среда для выполнения кода, я пытался использовать JSON.Я также пытался отправить мутации непосредственно как тело.Но ничего не работает.Все, что я получаю, это ошибка Bad Request.

'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const https = require("https");
process.env.DEBUG = 'dialogflow:debug';

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });

function welcome(agent) {  agent.add('My external DF Welcome agent!'); }

function getData(agent){
      const query = `mutation {  startAgentSession(access_token: "MyToken") { websocket_url session_id client_id}}`;
      console.log(JSON.stringify(query));
      const options = {
      hostname:'chat-api.zopim.com',
      path:'/graphql/request',
      method:'POST',
      query: query,
      headers: {'Content-Type':  'application/json'}
    };
      console.log(options);

      const req = https.request(options, (res) => {
              console.log('statusCode:', res.statusCode);
              console.log('headers:', res.headers);  
              res.on('data', (d) => {console.log('Data from res is>>>>', d.toString('utf8'));
                     agent.add(`Received data`);
                                    });   
         });

          req.on('error', (e) => {console.log("Error encountered>>>>.", e);  });
          req.end();
    }
  let intentMap = new Map(); 
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('getcode', getData);
  agent.handleRequest(intentMap);
  });

Ожидаемый результат должен быть похож на

{
  "data": {
    "startAgentSession": {
      "websocket_url":"wss://chat-api.zopim.com/stream/0.1/graphql/8b40lm:9012841289",
      "session_id":"MysessionID",
      "client_id":"MYClientID"
    }
  }
}

Вот текст параметра const, напечатанный, чтобы узнать, что идет как ввод:

 textPayload:  "{ hostname: 'chat-api.zopim.com',
  path: '/graphql/request',
  method: 'POST',
  query: 'mutation {  startAgentSession(access_token: "My_Token") { websocket_url session_id client_id}}',
  headers: { 'Content-Type': 'application/json' } }"  
 timestamp:  "2019-06-24T10:36:40.344Z"  

Вывод логов здесь:

<code> resource: {…}  
 severity:  "INFO"  
 textPayload:  "Data from res is>>>> <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Bad Request
"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...