// import { createClient, SubscribePayload } from 'graphql-ws';
const { createClient, SubscribePayload } = require('graphql-ws');
const { WebSocket } = require('ws');

console.log('start');

const headers = {
    'x-api-key': 'Yn7}a4fe`j(]k+Zu.Bk|}Hoz7RgAgdv<',
    'Content-Type': 'application/json',
    origin: 'abcdefg',
  };

class MyWebSocket extends WebSocket {
  constructor(address, protocols) {
    super(address, protocols, {
      headers: headers
    });
  }
}
let client;
async function clientFunction() {
  client = createClient({
    url: 'wss://apibundle.17.dev.ekika.co/user-graphql-apikey',
    //url: 'wss://easyapi.ekika.app/user-graphql-apikey',
    webSocketImpl: MyWebSocket,
  });

  const query = client.iterate({
    query: `
    subscription MySub{
      ResPartnerNew{
          id
          name
          write_date
      }
    }
    `,
    operationName: 'SubscriptionType',
    variables: {},
  });

  try{
    for await (const result of query) {
      console.log(result)
    }
  } catch(err) {
    console.log(err)
  }
};
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

clientFunction();
a => setTimeout(a, 5000);
client.terminate();



function comment() {

// import { createClient, SubscribePayload } from 'graphql-ws';
const { createClient, SubscribePayload } = require('graphql-ws');
const { WebSocket } = require('ws');

console.log('start');

const headers = {
    'x-api-key': 'Yn7}a4fe`j(]k+Zu.Bk|}Hoz7RgAgdv<',
    //'x-api-key': `Af+"E<y.-Rs<q9")|'%7A&@U.fU}JV~:`,
    'Content-Type': 'application/json',
    origin: 'abcdefg',
    // Other headers as needed
  };

// const ws = WebSocket('ws://localhost:8016/user-graphql-basic', {
//     headers: headers
// })

class MyWebSocket extends WebSocket {
    constructor(address, protocols) {
      super(address, protocols, {
        headers: headers
      });
    }
  }


// const client = createClient({
//   url: 'wss://apibundle.17.dev.ekika.co/user-graphql-apikey',
//   //url: 'wss://easyapi.ekika.app/user-graphql-apikey',
//   webSocketImpl: MyWebSocket,
// });
let client = null
async function clientFunction() {
  if (client){
    client.terminate()
  }
  else {
    client = createClient({
      url: 'wss://localhost:8016/gqlapi',
      //url: 'wss://easyapi.ekika.app/user-graphql-apikey',
      webSocketImpl: MyWebSocket,
    });
  }

  const query = client.iterate({
    query: `
    subscription MySub{
      ResPartnerNew{
          id
          name
    write_date
      }
    }
    `,
    operationName: 'SubscriptionType',
    variables: {},
  });

  try{
    for await (const result of query) {
      console.log(result)
      // next = result = { data: { greetings: 5x } }
      // "break" to dispose
    }
  } catch(err) {
    console.log(err)
  }
//   try {
//     const { value } = await query.next();
//     console.log(value)
//     // next = value = { data: { hello: 'Hello World!' } }
//     // complete
//   } catch (err) {
//     console.log(err)
//   }
};

clientFunction()
setInterval(clientFunction, 6000);

// import { createClient, SubscribePayload } from 'graphql-ws';
 
// const client = createClient({
//   url: 'ws://iterators.ftw:4000/graphql',
// });
 
// (async () => {
//   const subscription = client.iterate({
//     query: 'subscription { greetings }',
//   });
//   // "subscription.return()" to dispose
 
//   for await (const result of subscription) {
//     // next = result = { data: { greetings: 5x } }
//     // "break" to dispose
//   }
//   // complete
// })();
}