Created
June 26, 2018 18:36
-
-
Save joshstrange/3130d1edf0b414b997f75219defc9da6 to your computer and use it in GitHub Desktop.
Fetch graphql schema and save to file
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
#!/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