1

The aim of the script is to keep retrying to connect to the server endpoint and generate a graphql schema when it can.

The script works fine if I run it in another command prompt. It's just I want to run one command in one command prompt.

Instead I am getting the error:

(node:4212) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added. Use emitter.setMaxListeners() to increase limit

Which makes no sense to me because the listener should only be getting set once, not 11 times...

dotnet-run just runs the asp.net server. Obviously this takes some time to start up, which is why I wrote this script to keep retrying to hit the server.

"start": "npm-run-all --parallel dotnet-run get-schema:dev",
"get-schema:dev": "node scripts/getSchema.js --url http://localhost:8080/graphql"

I am using npm-run-all npm package

getSchema script:

require('isomorphic-fetch');
const getArgs = require('get-args');
const FormData = require('form-data');
const fs = require('fs');
const {
  buildClientSchema,
  introspectionQuery,
  printSchema,
} = require('graphql/utilities');
const path = require('path');

const schemaPath = path.join(__dirname, '../schema/schema');
const formData = new FormData();

formData.append('query', introspectionQuery);
const args = getArgs();
const url = args.options.url;

let intervalId = null;

// Save JSON of full schema introspection for Babel Relay Plugin to use
const setSchema = () => {
  fetch(url, {
    method: 'post',
    body: formData,
  })
    .then((res) => {
      if (res.ok) {
        return res.json();
      }
      throw new Error(res.statusText);
    })
    .then((schemaJSON) => {
      fs.writeFileSync(
        `${schemaPath}.json`,
        JSON.stringify(schemaJSON, null, 2),
      );

      // Save user readable type system shorthand of schema
      const graphQLSchema = buildClientSchema(schemaJSON.data);

      fs.writeFileSync(
        `${schemaPath}.graphql`,
        printSchema(graphQLSchema),
      );

      clearInterval(intervalId);
      console.log('Successfuly updated schema.');
    })
    .catch((error) => {
      console.log(error);
    });
};

intervalId = setInterval(setSchema, 1000);

After a while of this script running I get the error even though the server is up:

FetchError: request to http://localhost:8080/graphql failed, reason: socket hang up

Martin Dawson
  • 7,455
  • 6
  • 49
  • 92
  • To more accurately name the issue, this appears to be working "too well". In fact you appear to actually need to delay your script that depends on the server to start instead. The error seems to indicate the server has not started when the script to "fetch" is being run. – Neil Lunn Oct 22 '17 at 21:59
  • @NeilLunn The script does run before the server starts, it's supposed to until it finds the server, because if we use a delay we don't know how long to delay until server starts. However the fetch error happens after the server has definitely started. I think the problem is related to this: https://stackoverflow.com/questions/18867185/socket-hang-up-error-during-request Maybe I need to use pinging instead. – Martin Dawson Oct 22 '17 at 22:01

0 Answers0