Last active
June 15, 2024 10:11
-
-
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
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 --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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage