Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use try..catch or promise error won't be catched #7

Open
rofrol opened this issue Apr 1, 2018 · 0 comments
Open

Use try..catch or promise error won't be catched #7

rofrol opened this issue Apr 1, 2018 · 0 comments

Comments

@rofrol
Copy link

rofrol commented Apr 1, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant