Skip to content

Commit 5130fc1

Browse files
authored
feat(sdk): better handling of token info and usage configuration (#6637)
1 parent c1d9c05 commit 5130fc1

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

.changeset/tiny-glasses-talk.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@graphql-hive/core': minor
3+
'@graphql-hive/apollo': minor
4+
'@graphql-hive/envelop': minor
5+
'@graphql-hive/yoga': minor
6+
---
7+
8+
Add error logging for invalid combinations of the `target` and `token` configuration.
9+
10+
- Please make sure to provide the `target` option for usage reporting when using a token that starts with `hvo1/`.
11+
- Please make sure to **not** provide a `target` option for usage reporting when a token does **not** start with `hvo1/`

packages/libraries/core/src/client/client.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,16 @@ export function createHive(options: HivePluginOptions): HiveClient {
5353
await Promise.all([schemaReporter.dispose(), usage.dispose()]);
5454
}
5555

56+
const isOrganizationAccessToken = options.token?.startsWith('hvo1/') === true;
57+
5658
// enabledOnly when `printTokenInfo` is `true` or `debug` is true and `printTokenInfo` is not `false`
57-
const printTokenInfo = enabled
58-
? options.printTokenInfo === true || (!!options.debug && options.printTokenInfo !== false)
59-
: false;
59+
const printTokenInfo =
60+
enabled &&
61+
/** Access tokens using the `hvo1/` prefix cannot print any token information. */
62+
isOrganizationAccessToken === false
63+
? options.printTokenInfo === true || (!!options.debug && options.printTokenInfo !== false)
64+
: false;
65+
6066
const infoLogger = createHiveLogger(logger, '[info]');
6167

6268
const info = printTokenInfo

packages/libraries/core/src/client/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ export type HivePluginOptions = OptionalWhenFalse<
234234
/**
235235
* Print info about the token.
236236
* Disabled by default (enabled by default only in debug mode)
237+
*
238+
* **Note:** The new access tokens do not support printing the token info. For every access token starting with `hvo1/`
239+
* no information will be printed.
240+
* @deprecated This option will be removed in the future.
237241
*/
238242
printTokenInfo?: boolean;
239243
/**

packages/libraries/core/src/client/usage.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,18 @@ function isAbortAction(result: Parameters<CollectUsageCallback>[1]): result is A
5151
return 'action' in result && result.action === 'abort';
5252
}
5353

54+
const noopUsageCollector: UsageCollector = {
55+
collect() {
56+
return async () => {};
57+
},
58+
collectRequest() {},
59+
async dispose() {},
60+
collectSubscription() {},
61+
};
62+
5463
export function createUsage(pluginOptions: HivePluginOptions): UsageCollector {
5564
if (!pluginOptions.usage || pluginOptions.enabled === false) {
56-
return {
57-
collect() {
58-
return async () => {};
59-
},
60-
collectRequest() {},
61-
async dispose() {},
62-
collectSubscription() {},
63-
};
65+
return noopUsageCollector;
6466
}
6567

6668
let reportSize = 0;
@@ -75,6 +77,23 @@ export function createUsage(pluginOptions: HivePluginOptions): UsageCollector {
7577
const collector = memo(createCollector, arg => arg.schema);
7678
const excludeSet = new Set(options.exclude ?? []);
7779

80+
/** Access tokens using the `hvo1/` require a target. */
81+
if (!options.target && pluginOptions.token.startsWith('hvo1/')) {
82+
logger.error(
83+
"Using an organization access token (starting with 'hvo1/') requires providing the 'target' option." +
84+
'\nUsage reporting is disabled.',
85+
);
86+
return noopUsageCollector;
87+
}
88+
89+
if (options.target && !pluginOptions.token.startsWith('hvo1/')) {
90+
logger.error(
91+
"Using the 'target' option requires using an organization access token (starting with 'hvo1/')." +
92+
'\nUsage reporting is disabled.',
93+
);
94+
return noopUsageCollector;
95+
}
96+
7897
const baseEndpoint =
7998
selfHostingOptions?.usageEndpoint ?? options.endpoint ?? 'https://app.graphql-hive.com/usage';
8099

packages/libraries/core/tests/usage.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ test('retry on non-200', async () => {
750750

751751
test('constructs URL with usage.target', async ({ expect }) => {
752752
const logger = createHiveTestingLogger();
753-
const token = 'Token';
753+
const token = 'hvo1/brrrrt';
754754
const dUrl = Promise.withResolvers<string>();
755755

756756
const hive = createHive({

0 commit comments

Comments
 (0)