Skip to content

Commit 202d3f5

Browse files
committed
Fix failing unit tests for headers
1 parent c9483bb commit 202d3f5

10 files changed

Lines changed: 127 additions & 95 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ export default class Generate extends Command {
220220
loadSchemaStep(
221221
pullFromEngine,
222222
apiKey,
223+
flags.engine,
223224
"Loading GraphQL schema",
224225
async ctx => {
225226
if (flags.schema) {

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

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import { Command, flags } from "@oclif/command";
33
import { table, styledJSON } from "heroku-cli-util";
44
import * as Listr from "listr";
55
import { toPromise, execute } from "apollo-link";
6-
import {
7-
print,
8-
GraphQLError
9-
} from "graphql";
6+
import { print, GraphQLError } from "graphql";
107

118
import * as fg from "glob";
129
import { withGlobalFS } from "apollo-codegen-core/lib/localfs";
@@ -19,6 +16,11 @@ import { gitInfo } from "../../git";
1916
import { VALIDATE_OPERATIONS } from "../../operations/validateOperations";
2017
import { ChangeType } from "../../printer/ast";
2118
import { format } from "../schema/check";
19+
import {
20+
ApolloConfig,
21+
loadConfigFromFile,
22+
findAndLoadConfig
23+
} from "../../config";
2224

2325
export default class CheckQueries extends Command {
2426
static description =
@@ -29,6 +31,9 @@ export default class CheckQueries extends Command {
2931
char: "h",
3032
description: "Show command help"
3133
}),
34+
config: flags.string({
35+
description: "Path to your Apollo config file"
36+
}),
3237
queries: flags.string({
3338
description:
3439
"Path to your GraphQL queries, can include search tokens like **",
@@ -49,19 +54,37 @@ export default class CheckQueries extends Command {
4954
async run() {
5055
const { flags } = this.parse(CheckQueries);
5156

52-
const apiKey = flags.key;
53-
if (!apiKey) {
54-
this.error(
55-
"No API key was specified. Set an Apollo Engine API key using the `--key` flag or the `ENGINE_API_KEY` environment variable."
56-
);
57-
return;
58-
}
5957
const tasks: Listr = new Listr([
58+
{
59+
title: "Loading Apollo config",
60+
task: async ctx => {
61+
if (flags.config) {
62+
ctx.config = loadConfigFromFile(flags.config) || {};
63+
} else {
64+
ctx.config = findAndLoadConfig(__dirname) || {};
65+
}
66+
67+
ctx.config = {
68+
...ctx.config,
69+
operations: flags.queries
70+
? flags.queries.split("\n")
71+
: ctx.config.operations,
72+
engineKey: flags.key || ctx.config.engineKey
73+
};
74+
75+
if (!ctx.config.engineKey) {
76+
this.error(
77+
"No API key was specified. Set an Apollo Engine API key using the `--key` flag or the `ENGINE_API_KEY` environment variable."
78+
);
79+
return;
80+
}
81+
}
82+
},
6083
{
6184
title: "Scanning for GraphQL queries",
6285
task: async (ctx, task) => {
6386
const paths = withGlobalFS(() => {
64-
return (flags.queries ? flags.queries.split("\n") : []).flatMap(p =>
87+
return (ctx.config as ApolloConfig).operations.flatMap(p =>
6588
fg.sync(p)
6689
);
6790
});
@@ -76,11 +99,11 @@ export default class CheckQueries extends Command {
7699
},
77100
{
78101
title: "Checking query compatibility with schema",
79-
task: async (ctx) => {
102+
task: async ctx => {
80103
const gitContext = await gitInfo();
81104

82105
const variables = {
83-
id: getIdFromKey(apiKey),
106+
id: getIdFromKey(ctx.config.engineKey),
84107
// XXX hardcoded for now
85108
tag: "current",
86109
gitContext,
@@ -92,7 +115,7 @@ export default class CheckQueries extends Command {
92115
query: VALIDATE_OPERATIONS,
93116
variables,
94117
context: {
95-
headers: { ["x-api-key"]: apiKey },
118+
headers: { ["x-api-key"]: ctx.config.engineKey },
96119
...(flags.engine && { uri: flags.engine })
97120
}
98121
})

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@ export default class SchemaCheck extends Command {
6363
{
6464
title: "Fetching local schema",
6565
task: async ctx => {
66+
const headers = header
67+
.filter(x => Boolean(x))
68+
.map(x => JSON.parse(x))
69+
.reduce((a, b) => Object.assign(a, b), {});
6670
ctx.schema = await fetchSchema({
6771
url: flags.endpoint,
68-
headers: header.filter(x => Boolean(x)).map(x => JSON.parse(x))
72+
headers
6973
});
7074
}
7175
},

packages/apollo-cli/src/commands/schema/download.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export default class SchemaDownload extends Command {
5656
loadSchemaStep(
5757
pullFromEngine,
5858
apiKey,
59+
flags.engine,
5960
"Fetching local schema",
6061
async ctx => {
6162
ctx.schema = await fetchSchema({

packages/apollo-cli/src/commands/schema/publish.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ export default class SchemaPublish extends Command {
5555
{
5656
title: "Fetching current schema",
5757
task: async ctx => {
58+
const headers = header
59+
.filter(x => !!x)
60+
.map(x => JSON.parse(x))
61+
.reduce((a, b) => Object.assign(a, b), {});
5862
ctx.schema = await fetchSchema({
5963
url: flags.endpoint,
60-
headers: header.filter(x => !!x).map(x => JSON.parse(x))
64+
headers
6165
}).catch(this.error);
6266
}
6367
},

packages/apollo-cli/src/config.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface ApolloConfig {
1212
projectName?: string;
1313
schema?: string; // path to JSON introspection, if not provided endpoint will be used
1414
endpoint?: EndpointConfig; // GraphQL endpoint URL, used to run queries and grab schema
15-
operations?: string[]; // glob path(s) to GraphQL operations (default: '**/*.graphql')
15+
operations: string[]; // glob path(s) to GraphQL operations (default: '**/*.graphql')
1616
excludedOperations?: string[]; // glob path(s) to GraphQL operation paths to ignore (default: 'node_modules/**')
1717
engineKey?: string; // Apollo Engine key
1818
}
@@ -62,20 +62,28 @@ export function loadConfig(obj: any, configFilePath: string): ApolloConfig {
6262
};
6363
}
6464

65-
export function findAndLoadConfig(dir: string): ApolloConfig | undefined {
66-
if (fs.existsSync(join(dir, "apollo.config.js"))) {
67-
const configFile = join(dir, "apollo.config.js");
68-
delete require.cache[require.resolve(configFile)];
69-
return loadConfig(require(configFile), configFile);
70-
} else if (fs.existsSync(join(dir, "package.json"))) {
71-
const configFile = join(dir, "package.json");
72-
const apolloKey = JSON.parse(readFileSync(configFile).toString()).apollo;
65+
export function loadConfigFromFile(file: string): ApolloConfig | undefined {
66+
if (file.endsWith(".js")) {
67+
delete require.cache[require.resolve(file)];
68+
return loadConfig(require(file), file);
69+
} else if (file.endsWith("package.json")) {
70+
const apolloKey = JSON.parse(readFileSync(file).toString()).apollo;
7371
if (apolloKey) {
74-
return loadConfig(apolloKey, configFile);
72+
return loadConfig(apolloKey, file);
7573
} else {
7674
return undefined;
7775
}
7876
} else {
7977
return undefined;
8078
}
8179
}
80+
81+
export function findAndLoadConfig(dir: string): ApolloConfig | undefined {
82+
if (fs.existsSync(join(dir, "apollo.config.js"))) {
83+
return loadConfigFromFile(join(dir, "apollo.config.js"));
84+
} else if (fs.existsSync(join(dir, "package.json"))) {
85+
return loadConfigFromFile(join(dir, "package.json"));
86+
} else {
87+
return undefined;
88+
}
89+
}

packages/apollo-cli/src/fetch-schema.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ export async function fromFile(file: string) {
5252
}
5353
}
5454

55-
export interface FetchParams {
56-
endpoint: string | undefined;
57-
header?: Object[];
58-
}
59-
6055
export const fetchSchema = async ({ url, headers }: EndpointConfig) => {
6156
if (!url) throw new Error("No endpoint provided when fetching schema");
6257
if (fs.existsSync(url)) return fromFile(url);
@@ -74,7 +69,10 @@ export const fetchSchema = async ({ url, headers }: EndpointConfig) => {
7469
});
7570
};
7671

77-
export async function fetchSchemaFromEngine(apiKey: string) {
72+
export async function fetchSchemaFromEngine(
73+
apiKey: string,
74+
customEngine: string | undefined
75+
) {
7876
const variables = {
7977
id: getIdFromKey(apiKey as string),
8078
tag: "current"
@@ -85,7 +83,8 @@ export async function fetchSchemaFromEngine(apiKey: string) {
8583
query: SCHEMA_QUERY,
8684
variables,
8785
context: {
88-
headers: { ["x-api-key"]: apiKey }
86+
headers: { ["x-api-key"]: apiKey },
87+
...(customEngine && { uri: customEngine })
8988
}
9089
})
9190
);

packages/apollo-cli/src/load-schema.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export async function loadSchema(config: ApolloConfig) {
77
} else if (config.endpoint) {
88
return await fetchSchema(config.endpoint);
99
} else if (config.engineKey) {
10-
return await fetchSchemaFromEngine(config.engineKey);
10+
return await fetchSchemaFromEngine(config.engineKey, undefined);
1111
} else {
1212
throw new Error("No methods of getting the schema found");
1313
}
@@ -16,6 +16,7 @@ export async function loadSchema(config: ApolloConfig) {
1616
export function loadSchemaStep(
1717
pullFromEngine: boolean,
1818
apiKey: string | undefined,
19+
customEngine: string | undefined,
1920
notEngineTitle: string,
2021
notEngine: (ctx: any) => Promise<void>
2122
) {
@@ -25,7 +26,10 @@ export function loadSchemaStep(
2526
: notEngineTitle,
2627
task: async (ctx: any) => {
2728
if (pullFromEngine) {
28-
ctx.schema = await fetchSchemaFromEngine(apiKey as string);
29+
ctx.schema = await fetchSchemaFromEngine(
30+
apiKey as string,
31+
customEngine
32+
);
2933
} else {
3034
await notEngine(ctx);
3135
}

packages/apollo-codegen-core/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
"@babel/generator": "7.0.0-beta.38",
3939
"@babel/types": "7.0.0-beta.38",
4040
"common-tags": "^1.5.1",
41-
"core-js": "^2.5.3",
42-
"graphql-config": "^2.0.1"
41+
"core-js": "^2.5.3"
4342
},
4443
"peerDependencies": {
4544
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0"

0 commit comments

Comments
 (0)