Skip to content

Instantly share code, notes, and snippets.

@aizatto
Created June 4, 2018 05:43
Show Gist options
  • Save aizatto/1d882626d782e57b5c45a55603214b18 to your computer and use it in GitHub Desktop.
Save aizatto/1d882626d782e57b5c45a55603214b18 to your computer and use it in GitHub Desktop.
different ways to fetch the graphql schema
const fs = require('fs');
const {
buildClientSchema,
introspectionQuery,
printSchema,
} = require('graphql/utilities');
const path = require('path');
const schemaPath = path.join(__dirname, '../../data/schema');
const Schema = require('./../graphql/Schema.js');
fs.writeFileSync(
`${schemaPath}.graphql`,
printSchema(Schema, { commentDescriptions: true })
);
process.exit(0);
const fetch = require('node-fetch');
const fs = require('fs');
const {
buildClientSchema,
introspectionQuery,
printSchema,
} = require('graphql/utilities');
const path = require('path');
const schemaPath = path.join(__dirname, '../../data/schema');
const SERVER = 'http://localhost:4000/graphql';
// Save JSON of full schema introspection for Babel Relay Plugin to use
fetch(`${SERVER}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: introspectionQuery,
}),
})
.then(res => res.json())
.then(schemaJSON => {
fs.writeFileSync(`${schemaPath}.json`, JSON.stringify(schemaJSON, null, 2));
// Save user readable type system shorthand of schema
const graphQLSchema = buildClientSchema(schemaJSON.data);
fs.writeFileSync(`${schemaPath}.graphql`, printSchema(graphQLSchema));
})
.catch(error => {
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment