diff --git a/app/dociql/fetch-schema.js b/app/dociql/fetch-schema.js index f5246e51..59fe6b5d 100644 --- a/app/dociql/fetch-schema.js +++ b/app/dociql/fetch-schema.js @@ -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()