Make the token optional when client is disabled (#710)

This commit is contained in:
Kamil Kisiela 2022-11-29 15:29:16 +01:00 committed by GitHub
parent 310d738e19
commit d0357ee93a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 40 deletions

View file

@ -0,0 +1,5 @@
---
'@graphql-hive/client': patch
---
Make token optional when Hive is disabled

View file

@ -7,6 +7,9 @@ export interface HiveClient {
info(): Promise<void>;
reportSchema: SchemaReporter['report'];
collectUsage(args: ExecutionArgs): CollectUsageCallback;
/**
* @deprecated https://github.com/kamilkisiela/graphql-hive/issues/659
*/
operationsStore: OperationsStore;
dispose(): Promise<void>;
}
@ -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<AgentOptions, 'endpoint' | 'token' | 'enabled' | 'debug'>;
/**
* 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<T, KCond extends keyof T, KExcluded extends keyof T> =
// untouched by default or when true
| T
// when false, make KExcluded optional
| (Omit<T, KExcluded> & { [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<AgentOptions, 'endpoint' | 'token' | 'enabled' | 'debug'>;
/**
* 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<T> = null | undefined | T;

View file

@ -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