UNPKG

1.77 kBJavaScriptView Raw
1import { getIntrospectionQuery, buildClientSchema, parse, } from 'graphql';
2import { ValueOrPromise } from 'value-or-promise';
3import { isAsyncIterable, AggregateError, createGraphQLError, inspect, } from '@graphql-tools/utils';
4function getSchemaFromIntrospection(introspectionResult, options) {
5 var _a;
6 if ((_a = introspectionResult === null || introspectionResult === void 0 ? void 0 : introspectionResult.data) === null || _a === void 0 ? void 0 : _a.__schema) {
7 return buildClientSchema(introspectionResult.data, options);
8 }
9 if (introspectionResult === null || introspectionResult === void 0 ? void 0 : introspectionResult.errors) {
10 const graphqlErrors = introspectionResult.errors.map(error => createGraphQLError(error.message, error));
11 if (introspectionResult.errors.length === 1) {
12 throw graphqlErrors[0];
13 }
14 else {
15 throw new AggregateError(graphqlErrors, 'Could not obtain introspection result');
16 }
17 }
18 throw createGraphQLError(`Could not obtain introspection result, received the following as response; \n ${inspect(introspectionResult)}`);
19}
20export function introspectSchema(executor, context, options) {
21 const parsedIntrospectionQuery = parse(getIntrospectionQuery(options), options);
22 return new ValueOrPromise(() => executor({
23 document: parsedIntrospectionQuery,
24 context,
25 }))
26 .then(introspection => {
27 if (isAsyncIterable(introspection)) {
28 const iterator = introspection[Symbol.asyncIterator]();
29 return iterator.next().then(({ value }) => value);
30 }
31 return introspection;
32 })
33 .then(introspection => getSchemaFromIntrospection(introspection, options))
34 .resolve();
35}