Skip to content

Commit b0f2bcb

Browse files
n1ru4lcoderabbitai[bot]
authored andcommitted
feat(sdk): specify the usage target endpoint for organization access tokens (#6574)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent a7c3cc9 commit b0f2bcb

File tree

8 files changed

+135
-5
lines changed

8 files changed

+135
-5
lines changed

.changeset/five-stingrays-search.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@graphql-hive/envelop': minor
3+
---
4+
5+
Add support for providing a target for usage reporting with organization access tokens.
6+
This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
7+
or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`)
8+
9+
```ts
10+
import { useHive } from '@graphql-hive/envelop';
11+
12+
const hivePlugin = useHive({
13+
enabled: true,
14+
token: "ORGANIZATION_ACCESS_TOKEN",
15+
usage: {
16+
target: "my-org/my-project/my-target"
17+
}
18+
})
19+
```

.changeset/good-rocks-appear.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@graphql-hive/core': minor
3+
---
4+
5+
Add support for providing a target for usage reporting with organization access tokens.
6+
This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
7+
or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`)
8+
9+
```ts
10+
import { createHive } from '@graphql-hive/core';
11+
12+
const hive = createHive({
13+
enabled: true,
14+
token: "ORGANIZATION_ACCESS_TOKEN",
15+
usage: {
16+
target: "my-org/my-project/my-target"
17+
}
18+
})
19+
```

.changeset/rotten-hairs-pay.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@graphql-hive/yoga': minor
3+
---
4+
5+
Add support for providing a target for usage reporting with organization access tokens.
6+
This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
7+
or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`)
8+
9+
```ts
10+
import { useHive } from '@graphql-hive/yoga';
11+
12+
const hivePlugin = useHive({
13+
enabled: true,
14+
token: "ORGANIZATION_ACCESS_TOKEN",
15+
usage: {
16+
target: "my-org/my-project/my-target"
17+
}
18+
})
19+
```

.changeset/tame-paws-float.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@graphql-hive/apollo': minor
3+
---
4+
5+
Add support for providing a target for usage reporting with organization access tokens.
6+
This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
7+
or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`)
8+
9+
```ts
10+
import { useHive } from '@graphql-hive/apollo'
11+
12+
const hivePlugin = useHive({
13+
enabled: true,
14+
token: "ORGANIZATION_ACCESS_TOKEN",
15+
usage: {
16+
target: "my-org/my-project/my-target"
17+
}
18+
})
19+
```

deployment/services/graphql.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export function deployGraphQL({
118118
...environment.envVars,
119119
...apiEnv,
120120
SENTRY: sentry.enabled ? '1' : '0',
121-
REQUEST_LOGGING: '0', // disabled
121+
REQUEST_LOGGING: '1', // disabled
122122
COMMERCE_ENDPOINT: serviceLocalEndpoint(commerce.service),
123123
TOKENS_ENDPOINT: serviceLocalEndpoint(tokens.service),
124124
WEBHOOKS_ENDPOINT: serviceLocalEndpoint(webhooks.service),

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ export interface Logger {
7373
}
7474

7575
export interface HiveUsagePluginOptions {
76+
/**
77+
* The target to which the usage data should be reported to.
78+
* This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
79+
* or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`).
80+
*/
81+
target?: string;
7682
/**
7783
* Custom endpoint to collect schema usage
7884
*

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,18 @@ export function createUsage(pluginOptions: HivePluginOptions): UsageCollector {
7575
const collector = memo(createCollector, arg => arg.schema);
7676
const excludeSet = new Set(options.exclude ?? []);
7777

78+
const baseEndpoint =
79+
selfHostingOptions?.usageEndpoint ?? options.endpoint ?? 'https://app.graphql-hive.com/usage';
80+
81+
const endpoint = baseEndpoint + (options?.target ? `/${options.target}` : '');
82+
7883
const agent = createAgent<AgentAction>(
7984
{
8085
...(pluginOptions.agent ?? {
8186
maxSize: 1500,
8287
}),
8388
logger,
84-
endpoint:
85-
selfHostingOptions?.usageEndpoint ??
86-
options.endpoint ??
87-
'https://app.graphql-hive.com/usage',
89+
endpoint,
8890
token: pluginOptions.token,
8991
enabled: pluginOptions.enabled,
9092
debug: pluginOptions.debug,

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,49 @@ test('retry on non-200', async () => {
747747
[INF] [hive][usage] Disposing
748748
`);
749749
});
750+
751+
test('constructs URL with usage.target', async ({ expect }) => {
752+
const logger = createHiveTestingLogger();
753+
const token = 'Token';
754+
const dUrl = Promise.withResolvers<string>();
755+
756+
const hive = createHive({
757+
enabled: true,
758+
debug: true,
759+
agent: {
760+
timeout: 500,
761+
maxRetries: 0,
762+
sendInterval: 1,
763+
maxSize: 1,
764+
async fetch(url) {
765+
dUrl.resolve(url.toString());
766+
return new Response('', {
767+
status: 200,
768+
});
769+
},
770+
logger,
771+
},
772+
token,
773+
selfHosting: {
774+
graphqlEndpoint: 'http://localhost:2/graphql',
775+
applicationUrl: 'http://localhost:1',
776+
usageEndpoint: 'http://localhost',
777+
},
778+
usage: {
779+
target: 'the-guild/graphql-hive/staging',
780+
},
781+
});
782+
783+
await hive.collectUsage()(
784+
{
785+
schema,
786+
document: op,
787+
operationName: 'asd',
788+
},
789+
{},
790+
);
791+
792+
const url = await dUrl.promise;
793+
expect(url).toEqual('http://localhost/the-guild/graphql-hive/staging');
794+
await hive.dispose();
795+
});

0 commit comments

Comments
 (0)