Skip to content

Commit 00e2ace

Browse files
JakeDawkinsessaji
authored andcommitted
Add helpful message for invalid API key (apollographql#1413)
* Add check for api-key/serviceName match * Added ApolloConfig as an `apollo` type export
1 parent 1124297 commit 00e2ace

5 files changed

Lines changed: 35 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Upcoming
44

55
- `apollo`
6-
- <First `apollo` related entry goes here>
6+
- Add `ApolloConfig` type to exports from `apollo` [#1413](https://github.com/apollographql/apollo-tooling/pull/1413)
77
- `apollo-codegen-core`
88
- <First `apollo-codegen-core` related entry goes here>
99
- `apollo-codegen-flow`
@@ -19,12 +19,12 @@
1919
- `apollo-graphql`
2020
- <First `apollo-graphql` related entry goes here>
2121
- `apollo-language-server`
22-
- <First `apollo-language-server` related entry goes here>
22+
- Add error message for service lookup failure [#1413](https://github.com/apollographql/apollo-tooling/pull/1413)
2323
- `apollo-tools`
2424
- <First `apollo-tools` related entry goes here>
2525
- `vscode-apollo`
2626
- Only activate extension on apollo.config.js/ts [#1411](https://github.com/apollographql/apollo-tooling/pull/1411)
27-
- Changed the status bar title to be "Apollo" to save space.
27+
- Changed the status bar title to be "Apollo" to save space. [#1415](https://github.com/apollographql/apollo-tooling/pull/1415)
2828

2929
## `apollo@2.16.0`, `apollo-codegen-swift@0.34.0`, `apollo-language-server@1.13.0`, `apollo-tools@0.4.0`, `vscode-apollo@1.8.0`
3030

packages/apollo-language-server/src/engine/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,12 @@ export class ApolloEngineClient extends GraphQLDataSource {
274274
}
275275
});
276276

277-
if (!(data && data.service)) {
278-
throw new Error();
277+
if (!(data && data.service) || errors) {
278+
throw new Error(
279+
errors
280+
? errors.map(error => error.message).join("\n")
281+
: "No service returned. Make sure your service name and API key match"
282+
);
279283
}
280284

281285
const schemaTags: string[] = data.service.schemaTags.map(

packages/apollo-language-server/src/project/client.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export class GraphQLClientProject extends GraphQLProject {
169169
total: totalTypes
170170
},
171171
tag: this.config.tag,
172-
loaded: this.serviceID ? true : false,
172+
loaded: Boolean(this.serviceID && (this.schema || this.serviceSchema)),
173173
lastFetch: this.lastLoadDate
174174
};
175175
}
@@ -271,15 +271,19 @@ export class GraphQLClientProject extends GraphQLProject {
271271
await this.loadingHandler.handle(
272272
`Loading Engine data for ${this.displayName}`,
273273
(async () => {
274-
const {
275-
schemaTags,
276-
fieldStats
277-
} = await engineClient.loadSchemaTagsAndFieldStats(serviceID);
278-
this._onSchemaTags && this._onSchemaTags([serviceID, schemaTags]);
279-
this.fieldStats = fieldStats;
280-
this.lastLoadDate = +new Date();
281-
282-
this.generateDecorations();
274+
try {
275+
const {
276+
schemaTags,
277+
fieldStats
278+
} = await engineClient.loadSchemaTagsAndFieldStats(serviceID);
279+
this._onSchemaTags && this._onSchemaTags([serviceID, schemaTags]);
280+
this.fieldStats = fieldStats;
281+
this.lastLoadDate = +new Date();
282+
283+
this.generateDecorations();
284+
} catch (e) {
285+
console.error(e);
286+
}
283287
})()
284288
);
285289
}

packages/apollo-language-server/src/providers/schema/engine.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import gql from "graphql-tag";
44
import { GraphQLSchema, buildClientSchema } from "graphql";
55
import { ApolloEngineClient, ClientIdentity } from "../../engine";
66
import { ClientConfig, parseServiceSpecifier } from "../../config";
7+
import { getServiceFromKey } from "../../config/utils";
78
import {
89
GraphQLSchemaProvider,
910
SchemaChangeUnsubscribeHandler,
@@ -44,6 +45,15 @@ export class EngineSchemaProvider implements GraphQLSchemaProvider {
4445
}
4546

4647
const [id, tag = "current"] = parseServiceSpecifier(client.service);
48+
49+
// make sure the API key is valid for the service we're requesting a schema of.
50+
const keyServiceName = getServiceFromKey(engine.apiKey);
51+
if (id !== keyServiceName) {
52+
throw new Error(
53+
`API key service name (${keyServiceName}) does not match the service name in your config (${id}). Try changing the service name in your config to ${keyServiceName} or get a new key.`
54+
);
55+
}
56+
4757
const { data, errors } = await this.client.execute<GetSchemaByTag>({
4858
query: SCHEMA_QUERY,
4959
variables: {

packages/apollo/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export * from "./Command";
66

77
// export git utils
88
export * from "./git";
9+
10+
export { ApolloConfigFormat as ApolloConfig } from "apollo-language-server";

0 commit comments

Comments
 (0)