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

Did not get HTTP 200 back from the endpoint #14

Open
aminconnectngo opened this issue Apr 21, 2019 · 6 comments
Open

Did not get HTTP 200 back from the endpoint #14

aminconnectngo opened this issue Apr 21, 2019 · 6 comments

Comments

@aminconnectngo
Copy link

I already installed graphql-docs (globally) and now when I run below command
graphql-docs-gen MY-GRAPHQL-ENDPOINT doc.html
It returns with Did not get

HTTP 200 back from the endpoint error message.

Any idea how to fix it?

@onx2
Copy link

onx2 commented May 17, 2019

I ran the following and received the same error.
$ graphql-docs-gen https://fakerql.com/graphql documentation.html

@aminconnectngo @rodriguise Give this tool a try: https://www.npmjs.com/package/@graphidocs/docs

$ graphidocs -e https://fakerql.com/graphql -o ./doc/schema

I was able to produce the docs from that fake graphql api using it.

@1hsun
Copy link

1hsun commented Jun 10, 2019

After parsing the errors, file '/lib/introspectionQuery.txt' is having field named 'onOperation', 'onFragment' and 'onField', which might not contained in your query/schema field.
In my case, It will works when you eliminated them.

@amin-wan-kenobi
Copy link

@onx2
Thank you. It works great. Also when you choose a schema in the documentation, it gives you the selected schema is required by what other schemas. Very good indeed.

@onx2
Copy link

onx2 commented Jun 26, 2019

For anyone interested, I ended up with a custom build rather than using a tool.

Using node-fetch and ts-node I fetch the introspection query response, then use graphQL utils to build the schema.

ts-node and node-fetch are run on the command line using yarn or npm (in my case).

So something like this in your scripts section of package.json.
"fetch:schema": "ts-node ./fetch-schema"

This file is run on the CLI and will fetch the introspection query, then save the json response to a schema.json file in your directory.

// root -> fetch-schema.ts
'use strict';

// Don't worry about this section... I used the TS compiler to translate es6 into this because
// I couldn't get es6 to work with ts-node. It's verbose... I know :(
let fs = _interopRequireWildcard(require('fs'));
let _graphql = require('graphql')
let _nodeFetch = _interopRequireDefault(require('node-fetch'));

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : {
    default: obj
  };
}

function _interopRequireWildcard(obj) {
  if (obj && obj.__esModule) {
    return obj;
  } else {
    const newObj = {};
    if (obj !== null) {
      for (const key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          const desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {};
          if (desc.get || desc.set) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }
    }
    newObj.default = obj;

    return newObj;
  }
}

// This is the part you need to care about :)
(async () => {
  console.log('\x1b[36m', 'Fetching schema...');

  // ******** IMPORTANT ********
  // Update `url-for-your-graphql-api/graphql` with your actual graphql endpoint
  const response = await (_nodeFetch.default)('url-for-your-graphql-api/graphql', {
    body: JSON.stringify({
      query: _graphql.introspectionQuery
    }),
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    method: 'POST'
  });

  const schema = await response.json();

  if (schema.error) {
    console.log('\x1b[31m', `
      Failed to save most recent schema.json: ${schema.error}.\n
    `);
  } else {
    console.log('\x1b[36m', 'Writing schema file...');

    // ******** IMPORTANT ********
    // Update path/to/schema.json to the correct path you want to save to
    fs.writeFile('path/to/schema.json', JSON.stringify(schema), undefined, (err) => {
      // throws an error, you could also catch it here
      if (err) throw err;

      // success case, the file was saved
      console.log('\x1b[32m', 'Successfully saved schema.json!\n');
    });
  }
})();

Some TypeScript (or JS I guess if you can import json files) file that handles building the schema
Uses the buildClientSchema utility from graphql.


// ******** IMPORTANT ********
// Change `path/to/schema.json` to the path defined in your `fetch-schema.ts` file.
import { data as introspection } from 'path/to/schema.json';
import { buildClientSchema } from 'graphql';

const schema = buildClientSchema(introspection);

console.log(schema);

/**
  * Functions I used to extract data needed for building out the docs
  * 
  * const mutations = schema.getMutationType()
  * const queries = schema.getQueryType()
  * const subscriptions = schema.getSubscriptionType()
  * const typeMap = schema.getTypeMap()
  */

package.json
I think the bare requirements would look something like:

"devDependencies": {
    "node-fetch": "^2.6.0",
    "ts-loader": "^6.0.2",
    "ts-node": "^8.2.0",
    "typescript": "^3.5.1",
  },
  "dependencies": {
    "graphql": "^14.3.1",
    "whatwg-fetch": "^3.0.0"
  }

I'm using TypeScript, so to import .json files I had to set "resolveJsonModule": true in my tsconfig.json.

From here you can utilize all of the built in graphql functions as well as lodash to manage the types, mutations, queries, subscriptions, etc... and build your own UI. I chose to work with React and used Lodash for nice array / object handling.

Hope this helps!

@GraemeAllanBryce
Copy link

I have this issue too but struggle to understand where @1hsun found the errors.

Was the fix to this library or to the graphQL schema?

Welcome any help as to how I should proceed as the formatting of this library looks superior :-)

@jimtje
Copy link

jimtje commented Dec 12, 2019

The problem was that around early 2018 there was a breaking change in the introspection query. I've submitted a pull request that fixes the issue but I'm not sure if the library is still maintained otherwise. Does work as intended now though.

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

6 participants