Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/five-stingrays-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@graphql-hive/envelop': minor
---

Add support for providing a target for usage reporting with organization access tokens.
This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`)

```ts
import { useHive } from '@graphql-hive/envelop';

const hivePlugin = useHive({
enabled: true,
token: "ORGANIZATION_ACCESS_TOKEN",
usage: {
target: "my-org/my-project/my-target"
}
})
```
19 changes: 19 additions & 0 deletions .changeset/good-rocks-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@graphql-hive/core': minor
---

Add support for providing a target for usage reporting with organization access tokens.
This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`)

```ts
import { createHive } from '@graphql-hive/core';

const hive = createHive({
enabled: true,
token: "ORGANIZATION_ACCESS_TOKEN",
usage: {
target: "my-org/my-project/my-target"
}
})
```
19 changes: 19 additions & 0 deletions .changeset/rotten-hairs-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@graphql-hive/yoga': minor
---

Add support for providing a target for usage reporting with organization access tokens.
This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`)

```ts
import { useHive } from '@graphql-hive/yoga';

const hivePlugin = useHive({
enabled: true,
token: "ORGANIZATION_ACCESS_TOKEN",
usage: {
target: "my-org/my-project/my-target"
}
})
```
19 changes: 19 additions & 0 deletions .changeset/tame-paws-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@graphql-hive/apollo': minor
---

Add support for providing a target for usage reporting with organization access tokens.
This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`)

```ts
import { useHive } from '@graphql-hive/apollo'

const hivePlugin = useHive({
enabled: true,
token: "ORGANIZATION_ACCESS_TOKEN",
usage: {
target: "my-org/my-project/my-target"
}
})
```
2 changes: 1 addition & 1 deletion deployment/services/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function deployGraphQL({
...environment.envVars,
...apiEnv,
SENTRY: sentry.enabled ? '1' : '0',
REQUEST_LOGGING: '0', // disabled
REQUEST_LOGGING: '1', // disabled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Code comment does not match actual configuration

The REQUEST_LOGGING is set to '1' which typically enables logging, but the comment says "disabled". This inconsistency might lead to confusion.


🏁 Script executed:

#!/bin/bash
# Check if there's any documentation or other instances of REQUEST_LOGGING to understand the intended behavior

rg -A 1 -B 1 "REQUEST_LOGGING" --no-filename

Length of output: 7534


Update Inline Comment for REQUEST_LOGGING

  • The inline comment on line 121 currently reads “disabled,” but as defined throughout the code and documented in the configuration details, a value of '1' means request logging is enabled.
  • Please update the comment to either correctly state “enabled” (i.e., REQUEST_LOGGING: '1', // enabled) or remove it if it’s no longer necessary to avoid confusion.

COMMERCE_ENDPOINT: serviceLocalEndpoint(commerce.service),
TOKENS_ENDPOINT: serviceLocalEndpoint(tokens.service),
WEBHOOKS_ENDPOINT: serviceLocalEndpoint(webhooks.service),
Expand Down
6 changes: 6 additions & 0 deletions packages/libraries/core/src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export interface Logger {
}

export interface HiveUsagePluginOptions {
/**
* The target to which the usage data should be reported to.
* This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
* or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`).
*/
target?: string;
/**
* Custom endpoint to collect schema usage
*
Expand Down
10 changes: 6 additions & 4 deletions packages/libraries/core/src/client/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ export function createUsage(pluginOptions: HivePluginOptions): UsageCollector {
const collector = memo(createCollector, arg => arg.schema);
const excludeSet = new Set(options.exclude ?? []);

const baseEndpoint =
selfHostingOptions?.usageEndpoint ?? options.endpoint ?? 'https://app.graphql-hive.com/usage';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for someone to enter an endpoint with a / at the end? Should we attempt cleaning up the path before appending?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I would treat this as a user error and not be to smart about trying to auto-fix it. Thinking more about it self-hosters could have their usage behind a custom proxy that requires a trailing / at the end (not a big argument tho).


const endpoint = baseEndpoint + (options?.target ? `/${options.target}` : '');

const agent = createAgent<AgentAction>(
{
...(pluginOptions.agent ?? {
maxSize: 1500,
}),
logger,
endpoint:
selfHostingOptions?.usageEndpoint ??
options.endpoint ??
'https://app.graphql-hive.com/usage',
endpoint,
token: pluginOptions.token,
enabled: pluginOptions.enabled,
debug: pluginOptions.debug,
Expand Down
46 changes: 46 additions & 0 deletions packages/libraries/core/tests/usage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,49 @@ test('retry on non-200', async () => {
[INF] [hive][usage] Disposing
`);
});

test('constructs URL with usage.target', async ({ expect }) => {
const logger = createHiveTestingLogger();
const token = 'Token';
const dUrl = Promise.withResolvers<string>();

const hive = createHive({
enabled: true,
debug: true,
agent: {
timeout: 500,
maxRetries: 0,
sendInterval: 1,
maxSize: 1,
async fetch(url) {
dUrl.resolve(url.toString());
return new Response('', {
status: 200,
});
},
logger,
},
token,
selfHosting: {
graphqlEndpoint: 'http://localhost:2/graphql',
applicationUrl: 'http://localhost:1',
usageEndpoint: 'http://localhost',
},
usage: {
target: 'the-guild/graphql-hive/staging',
},
});

await hive.collectUsage()(
{
schema,
document: op,
operationName: 'asd',
},
{},
);

const url = await dUrl.promise;
expect(url).toEqual('http://localhost/the-guild/graphql-hive/staging');
await hive.dispose();
});
Loading