const https = require('http');
const fs = require('fs');
const introspectionQuery = `
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
}
}
}
}
}
}
}
}
`;
const postData = JSON.stringify({
query: introspectionQuery
});
const options = {
hostname: 'localhost',
port: 4000,
path: '/graphql',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': '9587c9820b109977a43a9302c23d051d98eff56050581eab63784b0b7f08152d',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const result = JSON.parse(data);
if (result.errors) {
console.error('GraphQL Errors:', result.errors);
process.exit(1);
}
fs.writeFileSync('schema.json', JSON.stringify(result, null, 2));
console.log('Schema saved to schema.json');
} catch (e) {
console.error('Error parsing response:', e);
process.exit(1);
}
});
});
req.on('error', (e) => {
console.error('Request error:', e);
process.exit(1);
});
req.write(postData);
req.end();