diff --git a/src/schema/transformSchema.js b/src/schema/transformSchema.js index 3675ddf..3b0fe43 100644 --- a/src/schema/transformSchema.js +++ b/src/schema/transformSchema.js @@ -1,13 +1,19 @@ import introspectionQuery from './introspectionQuery'; export default async function transformSchema(rootSchema, graphql) { - const initialResult = await graphql(rootSchema, introspectionQuery); - - if (initialResult.errors) { - throw new Error(`unable to parse schema: ${initialResult.errors}`); + if (rootSchema && 'data' in rootSchema && '__schema' in rootSchema.data) { + // The rootSchema is already an introspected schema. Just mimimize. + return makeMinimalSchema(rootSchema.data.__schema); + } else if (rootSchema) { + const introspectedSchema = await graphql(rootSchema.default || rootSchema, introspectionQuery); + if (introspectedSchema.errors) { + throw new Error(`unable to parse schema: ${introspectedSchema.errors}`); + } + return makeMinimalSchema(introspectedSchema.data.__schema); + } else { + throw new Error('Provide a schema to transformSchema function'); } - return makeMinimalSchema(initialResult.data.__schema); } const isObject = val => val && typeof val === 'object'; diff --git a/src/schema/updateSchema.js b/src/schema/updateSchema.js index 3d99113..2ab613a 100755 --- a/src/schema/updateSchema.js +++ b/src/schema/updateSchema.js @@ -29,10 +29,13 @@ export default async function updateSchema() { } } +// Read a schema as GraphQLSchema JS (file) or JSON with data.__schema (URL or file) const getSchema = async inputArg => { if (urlRegex.test(inputArg)) { const body = JSON.stringify({query: introspectionQuery}); - const res = await fetch(inputArg, {method: 'POST', body}); + var headers = new Headers(); + headers.append("Content-Type", "application/json"); + const res = await fetch(inputArg, {method: 'POST', headers, body}); const {status, statusText} = res; let resJSON; if (status >= 200 && status < 300) { @@ -45,14 +48,10 @@ const getSchema = async inputArg => { if (resJSON.errors) { console.log(`The graphQL endpoint returned the following errors: ${JSON.stringify(resJSON.errors)}`); } - return resJSON.data; + return resJSON; } const relativeInputPath = path.join(process.cwd(), inputArg); let rootSchema; - try { - rootSchema = require(relativeInputPath).default; - } catch (e) { - console.log('Error requiring schema', e); - } + rootSchema = require(relativeInputPath); return rootSchema; };