diff --git a/.changeset/five-kangaroos-divide.md b/.changeset/five-kangaroos-divide.md new file mode 100644 index 000000000..5f9a4869b --- /dev/null +++ b/.changeset/five-kangaroos-divide.md @@ -0,0 +1,5 @@ +--- +'@graphql-hive/client': patch +--- + +Make token optional when Hive is disabled diff --git a/packages/libraries/client/src/internal/types.ts b/packages/libraries/client/src/internal/types.ts index 9ef86f831..33217a6bc 100644 --- a/packages/libraries/client/src/internal/types.ts +++ b/packages/libraries/client/src/internal/types.ts @@ -7,6 +7,9 @@ export interface HiveClient { info(): Promise; reportSchema: SchemaReporter['report']; collectUsage(args: ExecutionArgs): CollectUsageCallback; + /** + * @deprecated https://github.com/kamilkisiela/graphql-hive/issues/659 + */ operationsStore: OperationsStore; dispose(): Promise; } @@ -134,45 +137,55 @@ export interface HiveSelfHostingOptions { usageEndpoint?: string; } -export interface HivePluginOptions { - /** - * Enable/Disable Hive - * - * Default: true - */ - enabled?: boolean; - /** - * Debugging mode - * - * Default: false - */ - debug?: boolean; - /** - * Access Token - */ - token: string; - /** - * Use when self-hosting GraphQL Hive - */ - selfHosting?: HiveSelfHostingOptions; - agent?: Omit; - /** - * Collects schema usage based on operations - * - * Disabled by default - */ - usage?: HiveUsagePluginOptions | boolean; - /** - * Schema reporting - * - * Disabled by default - */ - reporting?: HiveReportingPluginOptions | false; - /** - * Operations Store - */ - operationsStore?: HiveOperationsStorePluginOptions; -} +type OptionalWhenFalse = + // untouched by default or when true + | T + // when false, make KExcluded optional + | (Omit & { [P in KCond]: false } & { [P in KExcluded]?: T[KExcluded] }); + +export type HivePluginOptions = OptionalWhenFalse< + { + /** + * Enable/Disable Hive + * + * Default: true + */ + enabled?: boolean; + /** + * Debugging mode + * + * Default: false + */ + debug?: boolean; + /** + * Access Token + */ + token: string; + /** + * Use when self-hosting GraphQL Hive + */ + selfHosting?: HiveSelfHostingOptions; + agent?: Omit; + /** + * Collects schema usage based on operations + * + * Disabled by default + */ + usage?: HiveUsagePluginOptions | boolean; + /** + * Schema reporting + * + * Disabled by default + */ + reporting?: HiveReportingPluginOptions | false; + /** + * Operations Store + */ + operationsStore?: HiveOperationsStorePluginOptions; + }, + 'enabled', + 'token' +>; export type Maybe = null | undefined | T; diff --git a/packages/libraries/client/tests/enabled.spec.ts b/packages/libraries/client/tests/enabled.spec.ts index 24bf1e3c7..87df8b082 100644 --- a/packages/libraries/client/tests/enabled.spec.ts +++ b/packages/libraries/client/tests/enabled.spec.ts @@ -36,7 +36,29 @@ test("should not log that it's not enabled", async () => { agent: { logger, }, - token: '', + }); + + const result = await hive + .info() + .then(() => 'OK') + .catch(() => 'ERROR'); + + expect(logger.info).not.toBeCalled(); + expect(result).toBe('OK'); +}); + +test('should not throw exception about missing token when disabled', async () => { + const logger = { + error: jest.fn(), + info: jest.fn(), + }; + + const hive = createHive({ + enabled: false, + debug: false, + agent: { + logger, + }, }); const result = await hive