forked from mhallin/graphql_ppx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendIntrospectionQuery.js
141 lines (135 loc) · 3.09 KB
/
sendIntrospectionQuery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env node
var argv = require("yargs")
.usage('Usage: $0 <url> [--headers "key:value"]')
.command("url", "URL of the GraphQL endpoint", { alias: "url" })
.required(1, "URL is required")
.option("H", {
alias: "headers",
describe: "Additional Headers to send with introspection query",
type: "array",
coerce: arg => {
let additionalHeaders = {};
for (const header of arg) {
const separator = header.indexOf(":");
const name = header.substring(0, separator).trim();
const value = header.substring(separator + 1).trim();
if (!(name && value)) {
throw new Error('Headers should be specified as "Name: Value"');
}
additionalHeaders[name] = value;
}
return additionalHeaders;
}
})
.help("?")
.alias("?", "help")
.example("$0 https://example.com/graphql", "Get GraphQL Schema")
.example(`$0 https://example.com/graphql --headers "Authorisation: <token>"`, "Get GraphQL Schema with Authorisation header").argv;
var request = require("request");
var fs = require("fs");
var 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 requestOptions = {
json: true,
body: { query: introspectionQuery },
headers: { "user-agent": "node.js", ...argv.headers }
};
request.post(argv._[0], requestOptions, function(error, response, body) {
if (error) {
console.error("Could not send introspection query: ", error);
process.exit(1);
}
if (response.statusCode !== 200) {
console.error("Non-ok status code from API: ", response.statusCode, response.statusMessage);
process.exit(1);
}
var result = JSON.stringify(body, null, 2);
fs.writeFileSync("graphql_schema.json", result, { encoding: "utf-8" });
});