-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathjs.ts
49 lines (42 loc) · 1.26 KB
/
js.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
49
import { buildSchema, execute, parse } from "graphql";
import { resolve } from "path";
import {
ApolloIntrospection,
GraphQLIntrospection,
Introspection,
SchemaLoader,
} from "../interface";
import { query as introspectionQuery } from "../utility";
export interface IJsSchemaLoaderOptions {
schemaFile: string;
}
export const jsSchemaLoader: SchemaLoader = async (
options: IJsSchemaLoaderOptions
) => {
const schemaPath = resolve(options.schemaFile);
let schemaModule = require(schemaPath);
let schema: string;
// check if exist default in module
if (typeof schemaModule === "object") {
schemaModule = schemaModule.default;
}
// check for array of definition
if (Array.isArray(schemaModule)) {
schema = schemaModule.join("");
// check for array array wrapped in a function
} else if (typeof schemaModule === "function") {
schema = schemaModule().join("");
} else {
throw new Error(
`Unexpected schema definition on "${schemaModule}", must be an array or function`
);
}
const introspection = (await execute(
buildSchema(schema),
parse(introspectionQuery)
)) as Introspection;
return (
(introspection as ApolloIntrospection).__schema ||
(introspection as GraphQLIntrospection).data.__schema
);
};