Skip to content

Commit 55b5459

Browse files
committed
Move to dependency structure for config
1 parent d687ef1 commit 55b5459

12 files changed

Lines changed: 511 additions & 275 deletions

File tree

packages/apollo-cli/src/commands/codegen/__tests__/generate.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe("successful codegen", () => {
198198
expect(mockFS.readFileSync("API.ts").toString()).toMatchSnapshot();
199199
});
200200

201-
test
201+
test
202202
.do(() => {
203203
vol.fromJSON({
204204
"schema.json": JSON.stringify(fullSchema.__schema),

packages/apollo-cli/src/commands/codegen/generate.ts

Lines changed: 96 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,11 @@ import * as path from "path";
55

66
import { TargetType, default as generate } from "../../generate";
77

8-
import {
9-
buildClientSchema,
10-
visit,
11-
extendSchema,
12-
buildASTSchema
13-
} from "graphql";
14-
15-
import { loadSchema } from "../../load-schema";
16-
178
import { engineFlags } from "../../engine-cli";
18-
import { loadQueryDocuments } from "apollo-codegen-core/lib/loading";
199

2010
import { Gaze } from "gaze";
2111

22-
import { getOperationPathsForConfig } from "../../config";
12+
import { resolveDocumentSets, ResolvedDocumentSet } from "../../config";
2313
import { loadConfigStep } from "../../load-config";
2414

2515
const waitForKey = async () => {
@@ -196,103 +186,115 @@ export default class Generate extends Command {
196186
}
197187

198188
const tasks: Listr = new Listr([
199-
loadConfigStep((msg) => this.error(msg), flags, false, true),
189+
loadConfigStep(flags),
200190
{
201-
title: "Scanning for GraphQL queries",
191+
title: "Resolving GraphQL document sets and dependencies",
202192
task: async (ctx, task) => {
203-
const paths = getOperationPathsForConfig(ctx.config);
204-
task.title = `Scanning for GraphQL queries (${paths.length} found)`;
193+
ctx.documentSets = await resolveDocumentSets(ctx.config, true);
194+
task.title = `Scanning for GraphQL queries (${
195+
ctx.documentSets.length
196+
} found)`; // this is a bogus wrong value
205197

206-
const excludedPaths = [
207-
flags.clientSchema ? path.resolve(flags.clientSchema) : undefined,
208-
ctx.config.schema ? path.resolve(ctx.config.schema) : undefined
209-
];
198+
// const excludedPaths = [
199+
// flags.clientSchema ? path.resolve(flags.clientSchema) : undefined,
200+
// ctx.config.schema ? path.resolve(ctx.config.schema) : undefined
201+
// ];
210202

211-
ctx.queryPaths = paths.filter(
212-
p => !excludedPaths.some(v => v == path.resolve(p))
213-
).map(p => path.relative(ctx.config.projectFolder, p));
214-
}
215-
},
216-
{
217-
title: "Fetching current schema",
218-
task: async ctx => {
219-
ctx.schema = await loadSchema(ctx.config).catch(this.error);
220-
}
221-
},
222-
{
223-
title: "Parsing GraphQL schema",
224-
task: async (ctx, task) => {
225-
if (ctx.schema) {
226-
ctx.schema = buildClientSchema({ __schema: ctx.schema });
227-
} else {
228-
task.skip("No server-side schema provided");
229-
}
203+
// TODO: move filtering somewhere else
204+
// ctx.queryPaths = paths.filter(
205+
// p => !excludedPaths.some(v => v == path.resolve(p))
206+
// ).map(p => path.relative(ctx.config.projectFolder, p));
230207
}
231208
},
232-
{
233-
title: "Loading client-side GraphQL schema",
234-
task: async (ctx, task) => {
235-
if (!flags.clientSchema) {
236-
task.skip("Path to client schema not provided");
237-
} else {
238-
const foundDocuments = loadQueryDocuments([
239-
path.resolve(flags.clientSchema)
240-
]);
241-
if (foundDocuments.length == 0) {
242-
this.error("Found no query documents, aborting");
243-
}
209+
// {
210+
// title: "Fetching current schema",
211+
// task: async ctx => {
212+
// ctx.schema = await loadSchema(ctx.config).catch(this.error);
213+
// }
214+
// },
215+
// {
216+
// title: "Parsing GraphQL schema",
217+
// task: async (ctx, task) => {
218+
// if (ctx.schema) {
219+
// ctx.schema = buildClientSchema({ __schema: ctx.schema });
220+
// } else {
221+
// task.skip("No server-side schema provided");
222+
// }
223+
// }
224+
// },
225+
// {
226+
// title: "Loading client-side GraphQL schema",
227+
// task: async (ctx, task) => {
228+
// if (!flags.clientSchema) {
229+
// task.skip("Path to client schema not provided");
230+
// } else {
231+
// const foundDocuments = loadQueryDocuments([
232+
// path.resolve(flags.clientSchema)
233+
// ]);
234+
// if (foundDocuments.length == 0) {
235+
// this.error("Found no query documents, aborting");
236+
// }
244237

245-
if (foundDocuments.length > 1) {
246-
this.warn(
247-
"Found more than one query document, using the first one"
248-
);
249-
}
238+
// if (foundDocuments.length > 1) {
239+
// this.warn(
240+
// "Found more than one query document, using the first one"
241+
// );
242+
// }
250243

251-
const ast = foundDocuments[0];
252-
visit(ast, {
253-
enter(node) {
254-
if (node.kind == "FieldDefinition") {
255-
(node as any).__client = true;
256-
}
257-
}
258-
});
244+
// const ast = foundDocuments[0];
245+
// visit(ast, {
246+
// enter(node) {
247+
// if (node.kind == "FieldDefinition") {
248+
// (node as any).__client = true;
249+
// }
250+
// }
251+
// });
259252

260-
if (ctx.schema) {
261-
ctx.schema = extendSchema(ctx.schema, ast);
262-
} else {
263-
ctx.schema = buildASTSchema(ast);
264-
}
265-
}
266-
}
267-
},
253+
// if (ctx.schema) {
254+
// ctx.schema = extendSchema(ctx.schema, ast);
255+
// } else {
256+
// ctx.schema = buildASTSchema(ast);
257+
// }
258+
// }
259+
// }
260+
// },
268261
{
269262
title: "Generating query files",
270263
task: async (ctx, task) => {
271264
task.title = `Generating query files with '${inferredTarget}' target`;
272-
const writtenFiles = generate(
273-
ctx.queryPaths,
274-
ctx.schema,
275-
typeof args.output === "string" ? args.output : "__generated__",
276-
flags.only,
277-
inferredTarget,
278-
flags.tagName as string,
279-
!flags.outputFlat,
280-
{
281-
passthroughCustomScalars:
282-
flags.passthroughCustomScalars || !!flags.customScalarsPrefix,
283-
customScalarsPrefix: flags.customScalarsPrefix || "",
284-
addTypename: flags.addTypename,
285-
namespace: flags.namespace,
286-
operationIdsPath: flags.operationIdsPath,
287-
generateOperationIds: !!flags.operationIdsPath,
288-
mergeInFieldsFromFragmentSpreads:
289-
flags.mergeInFieldsFromFragmentSpreads,
290-
useFlowExactObjects: flags.useFlowExactObjects,
291-
useFlowReadOnlyTypes: flags.useFlowReadOnlyTypes
292-
}
293-
);
265+
if (ctx.documentSets.length == 0) {
266+
// error
267+
} else if (ctx.documentSets.length == 1) {
268+
const set = ctx.documentSets[0] as ResolvedDocumentSet;
269+
const writtenFiles = generate(
270+
set.documentPaths.map(p =>
271+
path.relative(ctx.config.projectFolder, p)
272+
),
273+
set.schema!,
274+
typeof args.output === "string" ? args.output : "__generated__",
275+
flags.only,
276+
inferredTarget,
277+
flags.tagName as string,
278+
!flags.outputFlat,
279+
{
280+
passthroughCustomScalars:
281+
flags.passthroughCustomScalars || !!flags.customScalarsPrefix,
282+
customScalarsPrefix: flags.customScalarsPrefix || "",
283+
addTypename: flags.addTypename,
284+
namespace: flags.namespace,
285+
operationIdsPath: flags.operationIdsPath,
286+
generateOperationIds: !!flags.operationIdsPath,
287+
mergeInFieldsFromFragmentSpreads:
288+
flags.mergeInFieldsFromFragmentSpreads,
289+
useFlowExactObjects: flags.useFlowExactObjects,
290+
useFlowReadOnlyTypes: flags.useFlowReadOnlyTypes
291+
}
292+
);
294293

295-
task.title = `Generating query files with '${inferredTarget}' target - wrote ${writtenFiles} files`;
294+
task.title = `Generating query files with '${inferredTarget}' target - wrote ${writtenFiles} files`;
295+
} else {
296+
// TODO
297+
}
296298
}
297299
}
298300
]);

packages/apollo-cli/src/commands/queries/__tests__/check.test.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ jest.mock("apollo-codegen-core/lib/localfs", () => {
55
// this is because of herkou-cli-utils hacky mocking system on their console logger
66
import { stdout, mockConsole } from "heroku-cli-util";
77
import { test as setup } from "apollo-cli-test";
8-
import {
9-
print,
10-
parse
11-
} from "graphql";
8+
import { print, parse } from "graphql";
129
import { ENGINE_URI } from "../../../engine";
1310
import { VALIDATE_OPERATIONS } from "../../../operations/validateOperations";
1411

@@ -94,44 +91,62 @@ describe("successful checks", () => {
9491
});
9592

9693
test
97-
.do(() => vol.fromJSON({
98-
...files,
99-
"package.json": `
94+
.do(() =>
95+
vol.fromJSON({
96+
...files,
97+
"package.json": `
10098
{
10199
"apollo": {
102-
"engineKey": "${ENGINE_API_KEY}"
100+
"schemas": {
101+
"customSchema": {
102+
"engineKey": "${ENGINE_API_KEY}"
103+
}
104+
}
103105
}
104106
}
105107
`
106-
}))
108+
})
109+
)
107110
.nock(ENGINE_URI, engineSuccess())
108111
.stdout()
109112
.command(["queries:check"])
110113
.exit(1)
111-
.it("compares against the latest uploaded schema with engine key from config", () => {
112-
expect(stdout).toContain("FAILURE");
113-
expect(stdout).toContain("WARNING");
114-
});
114+
.it(
115+
"compares against the latest uploaded schema with engine key from default config",
116+
() => {
117+
expect(stdout).toContain("FAILURE");
118+
expect(stdout).toContain("WARNING");
119+
}
120+
);
115121

116122
test
117-
.do(() => vol.fromJSON({
118-
...files,
119-
"test/package.json": `
123+
.do(() =>
124+
vol.fromJSON({
125+
...files,
126+
"test/package.json": `
120127
{
121128
"apollo": {
122-
"engineKey": "${ENGINE_API_KEY}"
129+
"schemas": {
130+
"customSchema": {
131+
"engineKey": "${ENGINE_API_KEY}"
132+
}
133+
}
123134
}
124135
}
125136
`
126-
}))
137+
})
138+
)
127139
.nock(ENGINE_URI, engineSuccess())
128140
.stdout()
129141
.command(["queries:check", "--config=test/package.json"])
130142
.exit(1)
131-
.it("compares against the latest uploaded schema with engine key from config", () => {
132-
expect(stdout).toContain("FAILURE");
133-
expect(stdout).toContain("WARNING");
134-
});
143+
.it(
144+
"compares against the latest uploaded schema with engine key from specified config",
145+
() => {
146+
expect(stdout).toContain("FAILURE");
147+
expect(stdout).toContain("WARNING");
148+
}
149+
);
135150

136151
test
137152
.do(() => vol.fromJSON(files))

packages/apollo-cli/src/commands/queries/check.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { gitInfo } from "../../git";
1313
import { VALIDATE_OPERATIONS } from "../../operations/validateOperations";
1414
import { ChangeType } from "../../printer/ast";
1515
import { format } from "../schema/check";
16-
import { getOperationPathsForConfig } from "../../config";
17-
import { loadConfigStep } from '../../load-config';
16+
import { resolveDocumentSets } from "../../config";
17+
import { loadConfigStep } from "../../load-config";
1818

1919
export default class CheckQueries extends Command {
2020
static description =
@@ -48,13 +48,15 @@ export default class CheckQueries extends Command {
4848
const { flags } = this.parse(CheckQueries);
4949

5050
const tasks: Listr = new Listr([
51-
loadConfigStep((msg) => this.error(msg), flags, true),
51+
loadConfigStep(flags, false),
5252
{
53-
title: "Scanning for GraphQL queries",
53+
title: "Resolving GraphQL document sets",
5454
task: async (ctx, task) => {
55-
const paths = getOperationPathsForConfig(ctx.config);
56-
57-
const operations = loadQueryDocuments(paths, flags.tagName);
55+
ctx.documentSets = await resolveDocumentSets(ctx.config, false);
56+
const operations = loadQueryDocuments(
57+
ctx.documentSets[0].documentPaths,
58+
flags.tagName
59+
);
5860
task.title = `Scanning for GraphQL queries (${
5961
operations.length
6062
} found)`;
@@ -65,10 +67,16 @@ export default class CheckQueries extends Command {
6567
{
6668
title: "Checking query compatibility with schema",
6769
task: async ctx => {
70+
if (!ctx.documentSets[0].engineKey) {
71+
this.error(
72+
"No API key was specified. Set an Apollo Engine API key using the `--key` flag or the `ENGINE_API_KEY` environment variable."
73+
);
74+
}
75+
6876
const gitContext = await gitInfo();
6977

7078
const variables = {
71-
id: getIdFromKey(ctx.config.engineKey),
79+
id: getIdFromKey(ctx.documentSets[0].engineKey),
7280
// XXX hardcoded for now
7381
tag: "current",
7482
gitContext,
@@ -80,7 +88,7 @@ export default class CheckQueries extends Command {
8088
query: VALIDATE_OPERATIONS,
8189
variables,
8290
context: {
83-
headers: { ["x-api-key"]: ctx.config.engineKey },
91+
headers: { ["x-api-key"]: ctx.documentSets[0].engineKey },
8492
...(flags.engine && { uri: flags.engine })
8593
}
8694
})

0 commit comments

Comments
 (0)