-
-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathintrospectSchema.ts
29 lines (27 loc) · 932 Bytes
/
introspectSchema.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
import { GraphQLSchema, parse } from 'graphql';
import { introspectionQuery, buildClientSchema } from 'graphql';
import { ApolloLink, execute, makePromise } from 'apollo-link';
import { Fetcher, fetcherToLink } from './makeRemoteExecutableSchema';
const parsedIntrospectionQuery = parse(introspectionQuery);
export default async function introspectSchema(
link: ApolloLink | Fetcher,
context?: { [key: string]: any },
): Promise<GraphQLSchema> {
if (!(link as ApolloLink).request) {
link = fetcherToLink(link as Fetcher);
}
const introspectionResult = await makePromise(
execute(link as ApolloLink, {
query: parsedIntrospectionQuery,
context,
}),
);
if (introspectionResult.errors || !introspectionResult.data.__schema) {
throw introspectionResult.errors;
} else {
const schema = buildClientSchema(introspectionResult.data as {
__schema: any;
});
return schema;
}
}