Skip to content

Instantly share code, notes, and snippets.

@n1ru4l
Created April 12, 2022 07:41
Show Gist options
  • Save n1ru4l/5f9ca7218102bea86ba44bab6ea3c3c5 to your computer and use it in GitHub Desktop.
Save n1ru4l/5f9ca7218102bea86ba44bab6ea3c3c5 to your computer and use it in GitHub Desktop.
Introspect and write GraphQL schema to file-system
import * as fs from 'fs';
import * as path from 'path';
import {
getIntrospectionQuery,
buildClientSchema,
print
} 'graphql';
async function main() {
const response = awaitfetch(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