fetch-graphql-schema
fetch-graphql-schema copied to clipboard
Use try..catch or promise error won't be catched
for example this returns error:
(node:2919) UnhandledPromiseRejectionWarning: ReferenceError: query is not defined
at file:///Users/romanfrolow/personal_projects/fetch-graphql-schema-custom/index.mjs:22:13
at <anonymous>
(node:2919) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2919) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
for code:
import fs from 'fs';
import path from 'path';
import fetch from 'node-fetch';
import { buildClientSchema, printSchema } from 'graphql/utilities';
import expose from './expose';
const {__dirname} = expose;
const custom = process.argv[2] === '--custom';
(async () => {
try {
const { introspectionQuery: query } = await import(custom ? './introspection-query' : 'graphql/utilities');
} catch (error) {
console.error(error);
}
fetch('http://127.0.0.1:8080/graphql2', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(custom
? { query, variables: { includeDeprecated: true } }
: { query }
),
})
.catch(error => console.error('BAD', error))
.then(result => {
if (result.errors) {
console.error(
'ERROR introspecting schema: ',
JSON.stringify(result.errors, null, 2)
);
} else {
result.json()
.catch(error => console.error('BAD', error))
.then(json => {
fs.writeFileSync(
path.join(__dirname, './schema.json'),
JSON.stringify(json, null, 2)
);
fs.writeFileSync(
path.join(__dirname, './schema.graphql'),
printSchema(buildClientSchema(json.data))
);
});
}
});
})()
instead of
{ FetchError: invalid json response body at http://127.0.0.1:8080/graphql2 reason: Unexpected end of JSON input
at /Users/romanfrolow/personal_projects/fetch-graphql-schema-custom/node_modules/node-fetch/lib/index.js:254:32
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:118:7)
message: 'invalid json response body at http://127.0.0.1:8080/graphql2 reason: Unexpected end of JSON input',
type: 'invalid-json' }
with the version using try..catch https://github.com/rofrol/fetch-graphql-schema-custom