Skip to content

Instantly share code, notes, and snippets.

@joshstrange
Created June 26, 2018 18:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshstrange/3130d1edf0b414b997f75219defc9da6 to your computer and use it in GitHub Desktop.
Save joshstrange/3130d1edf0b414b997f75219defc9da6 to your computer and use it in GitHub Desktop.
Fetch graphql schema and save to file
#!/usr/bin/env node
const os = require('os');
const request = require('request-promise-native');
const fs = require('mz/fs');
let hostname = os.hostname();
let baseUrl = 'YOUR_BASE_URL';
(async () => {
let jwt = await getJwt();
console.log("JWT Fetched");
console.log("Getting Schema");
let response = await request.post(baseUrl + '/graphql', {
headers: {
'Content-Type': 'application/json',
'YOUR_HEADER+NAME': jwt
},
body: JSON.stringify({
query: `
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}`
})
});
await fs.writeFile('graphql.schema.json', JSON.stringify(JSON.parse(response), null, "\t"));
console.log("Schema Updated!");
})();
async function getJwt() {
let username = 'YOUR_USERNAME';
let password = 'YOUR_PASSWPRD';
let response = await request.post(baseUrl + '/auth', {
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: username,
password: password
})
});
return JSON.parse(response).authenticationToken;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment