UNPKG

5 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.EndpointSchemaProvider = void 0;
4const apollo_link_1 = require("apollo-link");
5const apollo_link_http_1 = require("apollo-link-http");
6const graphql_1 = require("graphql");
7const https_1 = require("https");
8const apollo_env_1 = require("apollo-env");
9const config_1 = require("../../config");
10const utilities_1 = require("../../utilities");
11const util_1 = require("util");
12class EndpointSchemaProvider {
13 constructor(config) {
14 this.config = config;
15 }
16 async resolveSchema() {
17 if (this.schema)
18 return this.schema;
19 const { skipSSLValidation, url, headers } = this.config;
20 const options = {
21 uri: url,
22 fetch: apollo_env_1.fetch
23 };
24 if (url.startsWith("https:") && skipSSLValidation) {
25 options.fetchOptions = {
26 agent: new https_1.Agent({ rejectUnauthorized: false })
27 };
28 }
29 const { data, errors } = (await apollo_link_1.toPromise(apollo_link_1.execute(apollo_link_http_1.createHttpLink(options), {
30 query: graphql_1.parse(graphql_1.getIntrospectionQuery()),
31 context: { headers }
32 })).catch(e => {
33 if (util_1.isString(e.message) && e.message.includes("token <")) {
34 throw new Error("Apollo tried to introspect a running GraphQL service at " +
35 url +
36 "\nIt expected a JSON schema introspection result, but got an HTML response instead." +
37 "\nYou may need to add headers to your request or adjust your endpoint url.\n" +
38 "-----------------------------\n" +
39 "For more information, please refer to: https://go.apollo.dev/t/config \n\n" +
40 "The following error occurred:\n-----------------------------\n" +
41 e.message);
42 }
43 if (url === config_1.DefaultServiceConfig.endpoint.url &&
44 util_1.isString(e.message) &&
45 e.message.includes("ECONNREFUSED")) {
46 throw new Error("Failed to connect to a running GraphQL endpoint at " +
47 url +
48 "\nThis may be because you didn't start your service.\n" +
49 "By default, when an endpoint, Apollo API key, or localSchemaFile isn't provided, Apollo tries to fetch a schema from " +
50 config_1.DefaultServiceConfig.endpoint.url +
51 "\n-----------------------------\n" +
52 "\nFor more information, please refer to: https://go.apollo.dev/t/config \n\n" +
53 "The following error occurred: \n" +
54 "-----------------------------\n" +
55 e.message);
56 }
57 if (util_1.isString(e.message) && e.message.includes("ECONNREFUSED")) {
58 throw new Error("Failed to connect to a running GraphQL endpoint at " +
59 url +
60 "\nThis may be because you didn't start your service or the endpoint URL is incorrect.");
61 }
62 throw new Error(e);
63 }));
64 if (errors && errors.length) {
65 throw new Error(errors.map(({ message }) => message).join("\n"));
66 }
67 if (!data) {
68 throw new Error("No data received from server introspection.");
69 }
70 this.schema = graphql_1.buildClientSchema(data);
71 return this.schema;
72 }
73 onSchemaChange(_handler) {
74 throw new Error("Polling of endpoint not implemented yet");
75 return () => { };
76 }
77 async resolveFederatedServiceSDL() {
78 if (this.federatedServiceSDL)
79 return this.federatedServiceSDL;
80 const { skipSSLValidation, url, headers } = this.config;
81 const options = {
82 uri: url,
83 fetch: apollo_env_1.fetch
84 };
85 if (url.startsWith("https:") && skipSSLValidation) {
86 options.fetchOptions = {
87 agent: new https_1.Agent({ rejectUnauthorized: false })
88 };
89 }
90 const getFederationInfoQuery = `
91 query getFederationInfo {
92 _service {
93 sdl
94 }
95 }
96 `;
97 const { data, errors } = (await apollo_link_1.toPromise(apollo_link_1.execute(apollo_link_http_1.createHttpLink(options), {
98 query: graphql_1.parse(getFederationInfoQuery),
99 context: { headers }
100 })));
101 if (errors && errors.length) {
102 return utilities_1.Debug.error(errors.map(({ message }) => message).join("\n"));
103 }
104 if (!data || !data._service) {
105 return utilities_1.Debug.error("No data received from server when querying for _service.");
106 }
107 this.federatedServiceSDL = data._service.sdl;
108 return data._service.sdl;
109 }
110}
111exports.EndpointSchemaProvider = EndpointSchemaProvider;
112//# sourceMappingURL=endpoint.js.map
\No newline at end of file