-
Notifications
You must be signed in to change notification settings - Fork 470
/
Copy pathintrospection.ts
55 lines (50 loc) · 1.83 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
44
45
46
47
48
49
50
51
52
53
54
55
// IntrospectionSchemaProvider (http => IntrospectionResult => schema)
import { NotificationHandler } from "vscode-languageserver";
import { execute as linkExecute, toPromise } from "apollo-link";
import { createHttpLink, HttpLink } from "apollo-link-http";
import {
GraphQLSchema,
buildClientSchema,
getIntrospectionQuery,
ExecutionResult,
IntrospectionQuery,
parse
} from "graphql";
import { Agent } from "http";
import { fetch } from "apollo-env";
import { RemoteServiceConfig } from "../../config";
import { GraphQLSchemaProvider, SchemaChangeUnsubscribeHandler } from "./base";
export class IntrospectionSchemaProvider implements GraphQLSchemaProvider {
private schema?: GraphQLSchema;
constructor(private config: Exclude<RemoteServiceConfig, "name">) {}
async resolveSchema() {
if (this.schema) return this.schema;
const { skipSSLValidation, url, headers } = this.config;
const options: HttpLink.Options = {
uri: url,
fetch,
...(skipSSLValidation && { fetchOptions: { agent: new Agent() } })
};
const { data, errors } = (await toPromise(
linkExecute(createHttpLink(options), {
query: parse(getIntrospectionQuery()),
context: { headers }
})
)) as ExecutionResult<IntrospectionQuery>;
if (errors && errors.length) {
// XXX better error handling of GraphQL errors
throw new Error(errors.map(({ message }: Error) => message).join("\n"));
}
if (!data) {
throw new Error("No data received from server introspection.");
}
this.schema = buildClientSchema(data);
return this.schema;
}
onSchemaChange(
_handler: NotificationHandler<GraphQLSchema>
): SchemaChangeUnsubscribeHandler {
throw new Error("Polling of endpoint not implemented yet");
return () => {};
}
}