forked from apollographql/apollo-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
281 lines (255 loc) · 7.03 KB
/
config.ts
File metadata and controls
281 lines (255 loc) · 7.03 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { basename, dirname, join, relative, resolve } from "path";
import { fs, withGlobalFS } from "apollo-codegen-core/lib/localfs";
import * as fg from "glob";
import * as minimatch from "minimatch";
import { GraphQLSchema, extendSchema, visit, buildASTSchema } from "graphql";
import { loadSchema } from "./load-schema";
import { loadQueryDocuments } from "apollo-codegen-core/lib/loading";
export interface EndpointConfig {
url?: string; // main HTTP endpoint
subscriptions?: string; // WS endpoint for subscriptions
headers?: Object; // headers to send when performing operations
skipSSLValidation?: boolean; // bypass the SSL validation on a HTTPS request
}
export interface SchemaDependency {
schema?: string;
endpoint?: EndpointConfig;
engineKey?: string;
extends?: string;
clientSide?: boolean;
}
export interface DocumentSet {
schema?: string;
includes: string[];
excludes: string[];
}
export interface ApolloConfig {
configFile: string;
projectFolder: string;
name?: string;
schemas?: { [name: string]: SchemaDependency }; // path to JSON introspection, if not provided endpoint will be used
queries?: DocumentSet[];
engineEndpoint?: string;
}
function loadEndpointConfig(
obj: any,
shouldDefaultURL: boolean
): EndpointConfig | undefined {
let preSubscriptions: EndpointConfig | undefined;
if (typeof obj === "string") {
preSubscriptions = {
url: obj
};
} else {
preSubscriptions =
(obj as EndpointConfig | undefined) ||
(shouldDefaultURL ? { url: "http://localhost:4000/graphql" } : undefined);
}
if (
preSubscriptions &&
!preSubscriptions.subscriptions &&
preSubscriptions.url
) {
preSubscriptions.subscriptions = preSubscriptions.url!.replace(
"http",
"ws"
);
}
return preSubscriptions;
}
function loadSchemaConfig(
obj: any,
defaultEndpoint: boolean
): SchemaDependency {
return {
schema: obj.schema,
endpoint: loadEndpointConfig(
obj.endpoint,
!obj.engineKey && defaultEndpoint
),
engineKey: obj.engineKey,
clientSide: obj.clientSide,
extends: obj.extends
};
}
function loadDocumentSet(obj: any): DocumentSet {
return {
schema: obj.schema,
includes:
typeof obj.includes === "string"
? [obj.includes as string]
: obj.includes
? (obj.includes as string[])
: ["**"],
excludes:
typeof obj.excludes === "string"
? [obj.excludes as string]
: obj.excludes
? (obj.excludes as string[])
: ["node_modules/**"]
};
}
export function loadConfig(
obj: any,
configFile: string,
configDir: string,
defaultEndpoint: boolean,
defaultSchema: boolean
): ApolloConfig {
const schemasObj = (obj.schemas || {}) as { [name: string]: any };
Object.keys(schemasObj).forEach(key => {
schemasObj[key] = loadSchemaConfig(schemasObj[key], defaultEndpoint);
});
if (Object.keys(schemasObj).length == 0 && defaultSchema) {
schemasObj["default"] = loadSchemaConfig({}, defaultEndpoint);
}
return {
configFile,
projectFolder: configDir,
schemas: schemasObj,
name: basename(configDir),
queries: (obj.queries
? Array.isArray(obj.queries)
? (obj.queries as any[])
: [obj.queries]
: Object.keys(schemasObj).length == 1
? [{ schema: Object.keys(schemasObj)[0] }]
: []
).map(d => loadDocumentSet(d)),
engineEndpoint: obj.engineEndpoint
};
}
export function loadConfigFromFile(
file: string,
defaultEndpoint: boolean,
defaultSchema: boolean
): ApolloConfig {
if (file.endsWith(".js")) {
const filepath = resolve(file);
delete require.cache[require.resolve(filepath)];
return loadConfig(
require(filepath),
filepath,
dirname(filepath),
defaultEndpoint,
defaultSchema
);
} else if (file.endsWith("package.json")) {
const apolloKey = JSON.parse(fs.readFileSync(file).toString()).apollo;
if (apolloKey) {
return loadConfig(
apolloKey,
file,
dirname(file),
defaultEndpoint,
defaultSchema
);
} else {
return loadConfig(
{},
file,
dirname(file),
defaultEndpoint,
defaultSchema
);
}
} else {
throw new Error("Unsupported config file format");
}
}
export function findAndLoadConfig(
dir: string,
defaultEndpoint: boolean,
defaultSchema: boolean
): ApolloConfig {
if (fs.existsSync(join(dir, "apollo.config.js"))) {
return loadConfigFromFile(
join(dir, "apollo.config.js"),
defaultEndpoint,
defaultSchema
);
} else if (fs.existsSync(join(dir, "package.json"))) {
return loadConfigFromFile(
join(dir, "package.json"),
defaultEndpoint,
defaultSchema
);
} else {
return loadConfig({}, dir, dir, defaultEndpoint, defaultSchema);
}
}
export interface ResolvedDocumentSet {
schema?: GraphQLSchema;
endpoint?: EndpointConfig;
engineKey?: string;
documentPaths: string[];
originalSet: DocumentSet;
}
export async function resolveSchema(
name: string,
config: ApolloConfig
): Promise<GraphQLSchema | undefined> {
const referredSchema = (config.schemas || {})[name];
const loadAsAST = () => {
const ast = loadQueryDocuments([referredSchema.schema!])[0];
if (referredSchema.clientSide) {
visit(ast, {
enter(node) {
if (node.kind == "FieldDefinition") {
(node as any).__client = true;
}
}
});
}
return ast;
};
return referredSchema.extends
? extendSchema(
(await resolveSchema(referredSchema.extends, config))!,
loadAsAST()
)
: referredSchema.clientSide
? buildASTSchema(loadAsAST())
: await loadSchema(referredSchema, config);
}
export async function resolveDocumentSets(
config: ApolloConfig,
needSchema: boolean
): Promise<ResolvedDocumentSet[]> {
return await Promise.all(
(config.queries || []).map(async doc => {
const referredSchema = doc.schema
? (config.schemas || {})[doc.schema]
: undefined;
const schemaPaths: string[] = [];
let currentSchema = (config.schemas || {})[doc.schema!];
while (currentSchema) {
if (currentSchema.schema) {
schemaPaths.push(currentSchema.schema);
}
currentSchema = (config.schemas || {})[currentSchema.extends!];
}
return {
schema:
needSchema && doc.schema
? await resolveSchema(doc.schema, config)
: undefined,
endpoint: referredSchema ? referredSchema.endpoint : undefined,
engineKey: referredSchema ? referredSchema.engineKey : undefined,
documentPaths: doc.includes
.flatMap(i =>
withGlobalFS(() =>
fg.sync(i, { cwd: config.projectFolder, absolute: true })
)
)
.filter(
f =>
![...doc.excludes, ...schemaPaths].some(e =>
minimatch(relative(config.projectFolder, f), e)
)
),
originalSet: doc
};
})
);
}