/
introspect.ts
48 lines (44 loc) · 1.81 KB
/
introspect.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
44
45
46
47
48
import {
GraphQLSchema,
DocumentNode,
getIntrospectionQuery,
buildClientSchema,
parse,
IntrospectionQuery,
} from 'graphql';
import { ExecutionResult } from '@graphql-tools/utils';
import { AsyncExecutor, SyncExecutor } from '@graphql-tools/delegate';
import AggregateError from '@ardatan/aggregate-error';
function getSchemaFromIntrospection(introspectionResult: ExecutionResult<IntrospectionQuery>): GraphQLSchema {
if (introspectionResult?.data?.__schema) {
return buildClientSchema(introspectionResult.data);
} else if (introspectionResult?.errors?.length) {
if (introspectionResult.errors.length > 1) {
const combinedError = new AggregateError(introspectionResult.errors);
throw combinedError;
}
const error = introspectionResult.errors[0];
throw error.originalError || error;
} else {
throw new Error('Could not obtain introspection result, received: ' + JSON.stringify(introspectionResult));
}
}
export async function introspectSchema(executor: AsyncExecutor, context?: Record<string, any>): Promise<GraphQLSchema> {
const parsedIntrospectionQuery: DocumentNode = parse(getIntrospectionQuery());
const introspectionResult = await executor<IntrospectionQuery>({
document: parsedIntrospectionQuery,
context,
});
return getSchemaFromIntrospection(introspectionResult);
}
export function introspectSchemaSync(executor: SyncExecutor, context?: Record<string, any>): GraphQLSchema {
const parsedIntrospectionQuery: DocumentNode = parse(getIntrospectionQuery());
const introspectionResult = executor<IntrospectionQuery>({
document: parsedIntrospectionQuery,
context,
});
if ('then' in introspectionResult) {
throw new Error(`Executor cannot return promise value in introspectSchemaSync!`);
}
return getSchemaFromIntrospection(introspectionResult);
}