Last active
June 24, 2024 13:34
-
-
Save n1ru4l/5f9ca7218102bea86ba44bab6ea3c3c5 to your computer and use it in GitHub Desktop.
Introspect and write GraphQL schema to file-system
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
import * as fs from 'fs'; | |
import * as path from 'path'; | |
import { | |
getIntrospectionQuery, | |
buildClientSchema, | |
} 'graphql'; | |
async function main() { | |
const response = await fetch(process.env.GRAPHQL_API_URL, { | |
method: 'post', | |
headers: { | |
Accept: 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ query: getIntrospectionQuery() }), | |
}) | |
const body = await response.json() | |
const schema = buildClientSchema(body.data); | |
const sdlString = print(schema); | |
fs.writeFileSync(path.join(__dirname, 'schema.graphql'), sdlString); | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment