Skip to content

Commit

Permalink
Add support for generating docs from a GraphQL SDL file (#40)
Browse files Browse the repository at this point in the history
* added support for generating docs from a GraphQL IDL schema file

* added helpful error message around parsing a schema file
  • Loading branch information
demsullivan committed Sep 14, 2021
1 parent 07eab0c commit 6be7bf0
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion app/dociql/fetch-schema.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
const { getIntrospectionQuery, buildClientSchema } = require('graphql')
const { graphqlSync, buildSchema, getIntrospectionQuery, buildClientSchema } = require('graphql')
const request = require("sync-request")
const fs = require('fs');

const converter = require('graphql-2-json-schema');

module.exports = function (graphUrl, authHeader) {
if (graphUrl.includes("file://")) {
return fetchSchemaFromFile(graphUrl);
} else {
return fetchSchemaFromUrl(graphUrl, authHeader);
}


}

function fetchSchemaFromFile(graphUrl) {
const filePath = graphUrl.replace("file://", "");
const fileContent = fs.readFileSync(filePath, "utf-8");
let graphQLSchema;

try {
graphQLSchema = buildSchema(fileContent);
} catch (e) {
console.error(`Encountered an error parsing the schema file. Is ${filePath} in GraphQL SDL format?`);
throw e;
}

const introspection = graphqlSync(graphQLSchema, getIntrospectionQuery()).data;

const jsonSchema = converter.fromIntrospectionQuery(introspection);

return {
jsonSchema,
graphQLSchema
}
}

function fetchSchemaFromUrl(graphUrl, authHeader) {
const requestBody = {
operationName: "IntrospectionQuery",
query: getIntrospectionQuery()
Expand Down

0 comments on commit 6be7bf0

Please sign in to comment.