Skip to content

Instantly share code, notes, and snippets.

@HaNdTriX
Last active June 15, 2024 10:11
Show Gist options
  • Save HaNdTriX/8c68899fdbbdb2ac23ceb7eb70ffa28a to your computer and use it in GitHub Desktop.
Save HaNdTriX/8c68899fdbbdb2ac23ceb7eb70ffa28a to your computer and use it in GitHub Desktop.
Fetch a graphql schema from that server that need an Bearer Authorization Token
// #!/usr/bin/env node --experimental-json-modules
import readline from 'readline/promises'
import fs from 'fs/promises'
const GRAPHQL_ENDPOINT = process.argv[2]
if (!GRAPHQL_ENDPOINT) {
console.error('🚨 Please provide a GraphQL endpoint as an argument.')
process.exit(1)
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const token = await rl.question('🔓 Please paste your ACCESS_TOKEN?\n> ')
const response = await fetch(GRAPHQL_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token.replace('Bearer ', '').trim()}`
},
// Graphql introspection query
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
}
}
}
}
}
}
}
}
`
})
})
if (response.ok) {
const schema = await response.json()
await fs.writeFile('./schema.json', JSON.stringify(schema, null, 2))
console.log('✅ Schema updated successfully')
} else {
console.error('🚨 Error', response.status, response.statusText)
}
process.exit(response.ok ? 0 : 1)
@HaNdTriX
Copy link
Author

Usage

 node update-graphql-schema.mjs <GRAPHQL_ENDPOINT>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment