Skip to content

Commit

Permalink
Better typings for Hive plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Mar 14, 2024
1 parent 3d59e68 commit ff33394
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 32 deletions.
6 changes: 6 additions & 0 deletions .changeset/yellow-tools-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@graphql-mesh/types": patch
"@graphql-mesh/plugin-hive": patch
---

Update types in Hive plugin
44 changes: 36 additions & 8 deletions packages/legacy/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2001,20 +2001,15 @@
"title": "HivePlugin",
"properties": {
"enabled": {
"description": "If this expression is truthy, mocking would be enabled\nYou can use environment variables expression, for example: `process.env.MOCKING_ENABLED != null` (Any of: Boolean, String)",
"anyOf": [
{
"type": "object",
"additionalProperties": true
"type": "boolean"
},
{
"type": "string"
},
{
"type": "array",
"additionalItems": true
}
],
"description": "If this expression is truthy, mocking would be enabled\nYou can use environment variables expression, for example: `process.env.MOCKING_ENABLED != null`"
]
},
"token": {
"type": "string",
Expand Down Expand Up @@ -2044,6 +2039,24 @@
"type": "object",
"title": "HiveAgentOptions",
"properties": {
"name": {
"type": "string"
},
"logger": {
"anyOf": [
{
"type": "object",
"additionalProperties": true
},
{
"type": "string"
},
{
"type": "array",
"additionalItems": true
}
]
},
"timeout": {
"type": "integer",
"description": "30s by default"
Expand Down Expand Up @@ -2098,6 +2111,21 @@
"processVariables": {
"type": "boolean",
"description": "(Experimental) Enables collecting Input fields usage based on the variables passed to the operation.\nDefault: false"
},
"sampler": {
"anyOf": [
{
"type": "object",
"additionalProperties": true
},
{
"type": "string"
},
{
"type": "array",
"additionalItems": true
}
]
}
}
},
Expand Down
7 changes: 5 additions & 2 deletions packages/legacy/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1786,9 +1786,9 @@ export interface MaskedErrorsPluginConfig {
export interface HivePlugin {
/**
* If this expression is truthy, mocking would be enabled
* You can use environment variables expression, for example: `process.env.MOCKING_ENABLED != null`
* You can use environment variables expression, for example: `process.env.MOCKING_ENABLED != null` (Any of: Boolean, String)
*/
enabled?: any;
enabled?: boolean | string;
/**
* Access Token
*/
Expand All @@ -1802,6 +1802,8 @@ export interface HivePlugin {
* Agent Options
*/
export interface HiveAgentOptions {
name?: string;
logger?: any;
/**
* 30s by default
*/
Expand Down Expand Up @@ -1856,6 +1858,7 @@ export interface HiveUsageOptions {
* Default: false
*/
processVariables?: boolean;
sampler?: any;
}
/**
* Extract client info from GraphQL Context
Expand Down
46 changes: 28 additions & 18 deletions packages/plugins/hive/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { createHive, HivePluginOptions, useYogaHive } from '@graphql-hive/client';
import { process } from '@graphql-mesh/cross-helpers';
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
import { MeshPlugin, MeshPluginOptions, YamlConfig } from '@graphql-mesh/types';
import { registerTerminateHandler } from '@graphql-mesh/utils';
import { Logger, MeshPlugin, YamlConfig } from '@graphql-mesh/types';
import { PubSub, registerTerminateHandler } from '@graphql-mesh/utils';

export default function useMeshHive(
pluginOptions: MeshPluginOptions<YamlConfig.HivePlugin>,
pluginOptions: YamlConfig.HivePlugin & {
logger?: Logger;
pubsub?: PubSub;
},
// eslint-disable-next-line @typescript-eslint/ban-types
): MeshPlugin<{}> {
const enabled =
pluginOptions != null && 'enabled' in pluginOptions
? // eslint-disable-next-line no-new-func
new Function(`return ${pluginOptions.enabled}`)()
? typeof pluginOptions.enabled === 'string'
? // eslint-disable-next-line no-new-func
new Function(`return ${pluginOptions.enabled}`)()
: pluginOptions.enabled
: true;

if (!enabled) {
Expand All @@ -35,18 +40,22 @@ export default function useMeshHive(
processVariables: pluginOptions.usage.processVariables,
};
if (pluginOptions.usage?.clientInfo) {
usage.clientInfo = function (context) {
return {
name: stringInterpolator.parse(pluginOptions.usage.clientInfo.name, {
context,
env: process.env,
}),
version: stringInterpolator.parse(pluginOptions.usage.clientInfo.version, {
context,
env: process.env,
}),
if (typeof pluginOptions.usage.clientInfo === 'function') {
usage.clientInfo = pluginOptions.usage.clientInfo;
} else {
usage.clientInfo = function (context) {
return {
name: stringInterpolator.parse(pluginOptions.usage.clientInfo.name, {
context,
env: process.env,
}),
version: stringInterpolator.parse(pluginOptions.usage.clientInfo.version, {
context,
env: process.env,
}),
};
};
};
}
}
}
let reporting: HivePluginOptions['reporting'];
Expand All @@ -65,12 +74,13 @@ export default function useMeshHive(
let agent: HivePluginOptions['agent'];
if (pluginOptions.agent) {
agent = {
name: pluginOptions.agent.name,
timeout: pluginOptions.agent.timeout,
maxRetries: pluginOptions.agent.maxRetries,
minTimeout: pluginOptions.agent.minTimeout,
sendInterval: pluginOptions.agent.sendInterval,
maxSize: pluginOptions.agent.maxSize,
logger: pluginOptions.logger,
logger: pluginOptions.agent?.logger || pluginOptions.logger,
};
}
let selfHosting: HivePluginOptions['selfHosting'];
Expand Down Expand Up @@ -99,7 +109,7 @@ export default function useMeshHive(
function onTerminate() {
return hiveClient
.dispose()
.catch(e => pluginOptions.logger.error(`Hive client failed to dispose`, e));
.catch(e => pluginOptions.logger?.error(`Hive client failed to dispose`, e));
}
const id: number = pluginOptions.pubsub.subscribe('destroy', () =>
onTerminate().finally(() => pluginOptions.pubsub.unsubscribe(id)),
Expand Down
5 changes: 4 additions & 1 deletion packages/plugins/hive/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type HivePlugin @md {
If this expression is truthy, mocking would be enabled
You can use environment variables expression, for example: `process.env.MOCKING_ENABLED != null`
"""
enabled: Any
enabled: BooleanOrString
"""
Access Token
"""
Expand All @@ -32,6 +32,8 @@ type HivePlugin @md {
}

type HiveAgentOptions {
name: String
logger: Any
"""
30s by default
"""
Expand Down Expand Up @@ -87,6 +89,7 @@ type HiveUsageOptions {
Default: false
"""
processVariables: Boolean
sampler: Any
}

type HiveClientInfo {
Expand Down
9 changes: 7 additions & 2 deletions website/src/generated-markdown/HivePlugin.generated.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

* `enabled` (type: `Any`) - If this expression is truthy, mocking would be enabled
You can use environment variables expression, for example: `process.env.MOCKING_ENABLED != null`
* `enabled` - - If this expression is truthy, mocking would be enabled
You can use environment variables expression, for example: `process.env.MOCKING_ENABLED != null` One of:
* `Boolean`
* `String`
* `token` (type: `String`, required) - Access Token
* `agent` (type: `Object`) - Agent Options:
* `name` (type: `String`)
* `logger` (type: `Any`)
* `timeout` (type: `Int`) - 30s by default
* `maxRetries` (type: `Int`) - 5 by default
* `minTimeout` (type: `Int`) - 200 by default
Expand All @@ -27,6 +31,7 @@ Default: no ttl
Default: 1.0
* `processVariables` (type: `Boolean`) - (Experimental) Enables collecting Input fields usage based on the variables passed to the operation.
Default: false
* `sampler` (type: `Any`)
* `reporting` (type: `Object`) - Schema reporting:
* `author` (type: `String`, required) - Author of current version of the schema
* `commit` (type: `String`, required) - Commit SHA hash (or any identifier) related to the schema version
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5162,7 +5162,6 @@ __metadata:
"@graphql-hive/client": "npm:^0.28.0"
"@graphql-mesh/string-interpolation": "npm:0.5.3"
"@graphql-mesh/utils": "npm:^0.97.3"
"@types/http-cache-semantics": "npm:4.0.4"
peerDependencies:
"@graphql-mesh/cross-helpers": ^0.4.1
"@graphql-mesh/types": ^0.97.3
Expand Down

0 comments on commit ff33394

Please sign in to comment.