UNPKG

6.34 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path_1 = require("path");
4const localfs_1 = require("apollo-codegen-core/lib/localfs");
5const fg = require("glob");
6const minimatch = require("minimatch");
7const graphql_1 = require("graphql");
8const load_schema_1 = require("./load-schema");
9const loading_1 = require("apollo-codegen-core/lib/loading");
10function loadEndpointConfig(obj, shouldDefaultURL) {
11 let preSubscriptions;
12 if (typeof obj === "string") {
13 preSubscriptions = {
14 url: obj
15 };
16 }
17 else {
18 preSubscriptions =
19 obj ||
20 (shouldDefaultURL ? { url: "http://localhost:4000/graphql" } : undefined);
21 }
22 if (preSubscriptions &&
23 !preSubscriptions.subscriptions &&
24 preSubscriptions.url) {
25 preSubscriptions.subscriptions = preSubscriptions.url.replace("http", "ws");
26 }
27 return preSubscriptions;
28}
29function loadSchemaConfig(obj, defaultEndpoint) {
30 return {
31 schema: obj.schema,
32 endpoint: loadEndpointConfig(obj.endpoint, !obj.engineKey && defaultEndpoint),
33 engineKey: obj.engineKey,
34 clientSide: obj.clientSide,
35 extends: obj.extends
36 };
37}
38function loadDocumentSet(obj) {
39 return {
40 schema: obj.schema,
41 includes: typeof obj.includes === "string"
42 ? [obj.includes]
43 : obj.includes
44 ? obj.includes
45 : ["**"],
46 excludes: typeof obj.excludes === "string"
47 ? [obj.excludes]
48 : obj.excludes
49 ? obj.excludes
50 : ["node_modules/**"]
51 };
52}
53function loadConfig(obj, configFile, configDir, defaultEndpoint, defaultSchema) {
54 const schemasObj = (obj.schemas || {});
55 Object.keys(schemasObj).forEach(key => {
56 schemasObj[key] = loadSchemaConfig(schemasObj[key], defaultEndpoint);
57 });
58 if (Object.keys(schemasObj).length == 0 && defaultSchema) {
59 schemasObj["default"] = loadSchemaConfig({}, defaultEndpoint);
60 }
61 return {
62 configFile,
63 projectFolder: configDir,
64 schemas: schemasObj,
65 name: path_1.basename(configDir),
66 queries: (obj.queries
67 ? Array.isArray(obj.queries)
68 ? obj.queries
69 : [obj.queries]
70 : Object.keys(schemasObj).length == 1
71 ? [{ schema: Object.keys(schemasObj)[0] }]
72 : []).map(d => loadDocumentSet(d)),
73 engineEndpoint: obj.engineEndpoint
74 };
75}
76exports.loadConfig = loadConfig;
77function loadConfigFromFile(file, defaultEndpoint, defaultSchema) {
78 if (file.endsWith(".js")) {
79 const filepath = path_1.resolve(file);
80 delete require.cache[require.resolve(filepath)];
81 return loadConfig(require(filepath), filepath, path_1.dirname(filepath), defaultEndpoint, defaultSchema);
82 }
83 else if (file.endsWith("package.json")) {
84 const apolloKey = JSON.parse(localfs_1.fs.readFileSync(file).toString()).apollo;
85 if (apolloKey) {
86 return loadConfig(apolloKey, file, path_1.dirname(file), defaultEndpoint, defaultSchema);
87 }
88 else {
89 return loadConfig({}, file, path_1.dirname(file), defaultEndpoint, defaultSchema);
90 }
91 }
92 else {
93 throw new Error("Unsupported config file format");
94 }
95}
96exports.loadConfigFromFile = loadConfigFromFile;
97function findAndLoadConfig(dir, defaultEndpoint, defaultSchema) {
98 if (localfs_1.fs.existsSync(path_1.join(dir, "apollo.config.js"))) {
99 return loadConfigFromFile(path_1.join(dir, "apollo.config.js"), defaultEndpoint, defaultSchema);
100 }
101 else if (localfs_1.fs.existsSync(path_1.join(dir, "package.json"))) {
102 return loadConfigFromFile(path_1.join(dir, "package.json"), defaultEndpoint, defaultSchema);
103 }
104 else {
105 return loadConfig({}, dir, dir, defaultEndpoint, defaultSchema);
106 }
107}
108exports.findAndLoadConfig = findAndLoadConfig;
109async function resolveSchema(name, config) {
110 const referredSchema = (config.schemas || {})[name];
111 const loadAsAST = () => {
112 const ast = loading_1.loadQueryDocuments([referredSchema.schema])[0];
113 if (referredSchema.clientSide) {
114 graphql_1.visit(ast, {
115 enter(node) {
116 if (node.kind == "FieldDefinition") {
117 node.__client = true;
118 }
119 }
120 });
121 }
122 return ast;
123 };
124 return referredSchema.extends
125 ? graphql_1.extendSchema((await resolveSchema(referredSchema.extends, config)), loadAsAST())
126 : referredSchema.clientSide
127 ? graphql_1.buildASTSchema(loadAsAST())
128 : await load_schema_1.loadSchema(referredSchema, config).then(introspectionSchema => {
129 if (!introspectionSchema)
130 return;
131 return graphql_1.buildClientSchema({ __schema: introspectionSchema });
132 });
133}
134exports.resolveSchema = resolveSchema;
135async function resolveDocumentSets(config, needSchema) {
136 return await Promise.all((config.queries || []).map(async (doc) => {
137 const referredSchema = doc.schema
138 ? (config.schemas || {})[doc.schema]
139 : undefined;
140 const schemaPaths = [];
141 let currentSchema = (config.schemas || {})[doc.schema];
142 while (currentSchema) {
143 if (currentSchema.schema) {
144 schemaPaths.push(currentSchema.schema);
145 }
146 currentSchema = (config.schemas || {})[currentSchema.extends];
147 }
148 return {
149 schema: needSchema && doc.schema
150 ? await resolveSchema(doc.schema, config)
151 : undefined,
152 endpoint: referredSchema ? referredSchema.endpoint : undefined,
153 engineKey: referredSchema ? referredSchema.engineKey : undefined,
154 documentPaths: doc.includes
155 .flatMap(i => localfs_1.withGlobalFS(() => fg.sync(i, { cwd: config.projectFolder, absolute: true })))
156 .filter(f => ![...doc.excludes, ...schemaPaths].some(e => minimatch(path_1.relative(config.projectFolder, f), e))),
157 originalSet: doc
158 };
159 }));
160}
161exports.resolveDocumentSets = resolveDocumentSets;
162//# sourceMappingURL=config.js.map
\No newline at end of file