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

Schema conversion improvements #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/schema/transformSchema.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
13 changes: 6 additions & 7 deletions src/schema/updateSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
};