-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathintrospection.ts
43 lines (39 loc) · 1.13 KB
/
introspection.ts
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
import { buildClientSchema, getIntrospectionQuery, printSchema } from "graphql";
import { readFile } from "node:fs/promises";
/**
* Introspect a GraphQL endpoint and return the schema as the GraphQL SDL
* @param endpoint - The endpoint to introspect
* @returns The schema
*/
export async function introspectEndpoint(
endpoint: string,
headers?: Record<string, string>,
) {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
...headers,
},
body: JSON.stringify({
query: getIntrospectionQuery(),
}),
});
if (!response.ok) {
throw new Error(`GraphQL request failed: ${response.statusText}`);
}
const responseJson = await response.json();
// Transform to a schema object
const schema = buildClientSchema(responseJson.data);
// Print the schema SDL
return printSchema(schema);
}
/**
* Introspect a local GraphQL schema file and return the schema as the GraphQL SDL
* @param path - The path to the local schema file
* @returns The schema
*/
export async function introspectLocalSchema(path: string) {
const schema = await readFile(path, "utf8");
return schema;
}