Created
June 4, 2018 05:43
-
-
Save aizatto/1d882626d782e57b5c45a55603214b18 to your computer and use it in GitHub Desktop.
different ways to fetch the graphql schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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