Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kyledetella/c671ca6335fbfd9e6aa3db97db0c212f to your computer and use it in GitHub Desktop.
Save kyledetella/c671ca6335fbfd9e6aa3db97db0c212f to your computer and use it in GitHub Desktop.
Get SDL from GraphQL introspection query
import {
buildClientSchema,
buildSchema,
getIntrospectionQuery,
GraphQLSchema,
printSchema,
} from "graphql";
import fetch from "node-fetch";
const GRAPHQL_API_URL = "";
const GRAPHQL_API_AUTH_TOKEN = "";
(async () => {
const resp = await fetch(GRAPHQL_API_URL, {
method: "POST",
headers: {
Authorization: `Basic ${GRAPHQL_API_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ query: getIntrospectionQuery() }),
});
const { data } = await resp.json();
// Convert to SDL
console.log(printSchema(buildClientSchema(data)));
// Get GraphQLSchema type
console.log(buildSchema(data));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment