Created
May 7, 2019 19:02
-
-
Save chandan-reddy-k/b522714a35868405c0930e13401e3b18 to your computer and use it in GitHub Desktop.
Query and fetch 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 fetch = require('node-fetch'); | |
const fs = require('fs'); | |
fetch(`https://api.example.com/graphql`, { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ | |
variables: {}, | |
query: ` | |
{ | |
__schema { | |
types { | |
kind | |
name | |
possibleTypes { | |
name | |
} | |
} | |
} | |
} | |
`, | |
}), | |
}) | |
.then(result => result.json()) | |
.then(result => { | |
// here we're filtering out any type information unrelated to unions or interfaces | |
const filteredData = result.data.__schema.types.filter( | |
type => type.possibleTypes !== null, | |
); | |
result.data.__schema.types = filteredData; | |
fs.writeFileSync('./src/fragmentTypes.json', JSON.stringify(result.data), err => { | |
if (err) { | |
console.error('Error writing fragmentTypes file', err); | |
} else { | |
console.log('Fragment types successfully extracted!'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment