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