UNPKG

4.6 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.FileSchemaProvider = void 0;
7const graphql_1 = require("graphql");
8const fs_1 = require("fs");
9const path_1 = require("path");
10const utilities_1 = require("../../utilities");
11const apollo_graphql_1 = require("apollo-graphql");
12const federation_1 = require("@apollo/federation");
13const vscode_uri_1 = __importDefault(require("vscode-uri"));
14class FileSchemaProvider {
15 constructor(config) {
16 this.config = config;
17 }
18 async resolveSchema() {
19 if (this.schema)
20 return this.schema;
21 const { path, paths } = this.config;
22 const documents = path
23 ? [this.loadFileAndGetDocument(path)]
24 : paths
25 ? paths.map(this.loadFileAndGetDocument, this)
26 : undefined;
27 if (!documents)
28 throw new Error(`Schema could not be loaded for [${path ? path : paths ? paths.join(", ") : "undefined"}]`);
29 this.schema = apollo_graphql_1.buildSchemaFromSDL(documents);
30 if (!this.schema)
31 throw new Error(`Schema could not be loaded for ${path}`);
32 return this.schema;
33 }
34 loadFileAndGetDocument(path) {
35 let result;
36 try {
37 result = fs_1.readFileSync(path, {
38 encoding: "utf-8"
39 });
40 }
41 catch (err) {
42 throw new Error(`Unable to read file ${path}. ${err.message}`);
43 }
44 const ext = path_1.extname(path);
45 if (ext === ".json") {
46 const parsed = JSON.parse(result);
47 const __schema = parsed.data
48 ? parsed.data.__schema
49 : parsed.__schema
50 ? parsed.__schema
51 : parsed;
52 const schema = graphql_1.buildClientSchema({ __schema });
53 return graphql_1.parse(graphql_1.printSchema(schema));
54 }
55 else if (ext === ".graphql" || ext === ".graphqls" || ext === ".gql") {
56 const uri = vscode_uri_1.default.file(path_1.resolve(path)).toString();
57 return graphql_1.parse(new graphql_1.Source(result, uri));
58 }
59 throw new Error("File Type not supported for schema loading. Must be a .json, .graphql, .gql, or .graphqls file");
60 }
61 onSchemaChange(_handler) {
62 throw new Error("File watching not implemented yet");
63 return () => { };
64 }
65 async resolveFederatedServiceSDL() {
66 if (this.federatedServiceSDL)
67 return this.federatedServiceSDL;
68 const { path, paths } = this.config;
69 const SDLs = path
70 ? [this.loadFileAndGetSDL(path)]
71 : paths
72 ? paths.map(this.loadFileAndGetSDL, this)
73 : undefined;
74 if (!SDLs || SDLs.filter(s => !Boolean(s)).length > 0)
75 return utilities_1.Debug.error(`SDL could not be loaded for one of more files: [${path ? path : paths ? paths.join(", ") : "undefined"}]`);
76 const federatedSchema = federation_1.buildFederatedSchema(SDLs.map(sdl => ({ typeDefs: graphql_1.parse(sdl) })));
77 const queryType = federatedSchema.getQueryType();
78 if (!queryType)
79 return utilities_1.Debug.error("No query type found for federated schema");
80 const serviceField = queryType.getFields()["_service"];
81 const serviceResults = serviceField &&
82 serviceField.resolve &&
83 serviceField.resolve(null, {}, null, {});
84 if (!serviceResults || !serviceResults.sdl)
85 return utilities_1.Debug.error("No SDL resolver or result from federated schema after building");
86 this.federatedServiceSDL = serviceResults.sdl;
87 return this.federatedServiceSDL;
88 }
89 loadFileAndGetSDL(path) {
90 let result;
91 try {
92 result = fs_1.readFileSync(path, {
93 encoding: "utf-8"
94 });
95 }
96 catch (err) {
97 return utilities_1.Debug.error(`Unable to read file ${path}. ${err.message}`);
98 }
99 const ext = path_1.extname(path);
100 if (ext === ".graphql" || ext === ".graphqls" || ext === ".gql") {
101 return result;
102 }
103 else {
104 return utilities_1.Debug.error("When using localSchemaFile to check or push a federated service, you can only use .graphql, .gql, and .graphqls files");
105 }
106 }
107}
108exports.FileSchemaProvider = FileSchemaProvider;
109//# sourceMappingURL=file.js.map
\No newline at end of file