-
Notifications
You must be signed in to change notification settings - Fork 144
feat(cli): add JSON Schema for hive.json config file #8154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
peterphitran
wants to merge
3
commits into
graphql-hive:main
Choose a base branch
from
peterphitran:feat/hive-json-schema
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --- | ||
| '@graphql-hive/cli': minor | ||
| --- | ||
|
|
||
| Add a JSON Schema for the `hive.json` configuration file. | ||
|
|
||
| The CLI now ships a `hive-config.schema.json` describing the supported `hive.json` shapes (the modern | ||
| `registry`/`cdn` format, the deprecated flat format, and namespaced/space configurations). Reference | ||
| it from your config to get autocompletion, inline documentation, and validation in editors that | ||
| support JSON Schema: | ||
|
|
||
| ```json | ||
| { | ||
| "$schema": "https://raw.githubusercontent.com/graphql-hive/console/main/packages/libraries/cli/hive-config.schema.json", | ||
| "registry": { | ||
| "endpoint": "<yourRegistryURL>", | ||
| "accessToken": "<yourtoken>" | ||
| } | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "$id": "https://raw.githubusercontent.com/graphql-hive/console/main/packages/libraries/cli/hive-config.schema.json", | ||
|
peterphitran marked this conversation as resolved.
|
||
| "title": "GraphQL Hive CLI configuration (hive.json)", | ||
| "description": "Configuration file for the GraphQL Hive CLI (`hive`). Configuration is resolved with the priority: CLI arguments > environment variables > hive.json. See https://the-guild.dev/graphql/hive/docs/api-reference/cli for details.", | ||
| "$comment": "Generated from ConfigModel and LegacyConfigModel in src/helpers/config.ts by scripts/generate-config-schema.ts. Do not edit by hand; run `pnpm --filter @graphql-hive/cli generate:schema`.", | ||
| "anyOf": [ | ||
| { | ||
| "$ref": "#/definitions/modernConfig" | ||
| }, | ||
| { | ||
| "$ref": "#/definitions/legacyConfig" | ||
| }, | ||
| { | ||
| "$ref": "#/definitions/namespacedConfig" | ||
| } | ||
| ], | ||
| "definitions": { | ||
| "modernConfig": { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "description": "Hive CLI configuration.", | ||
| "properties": { | ||
| "$schema": { | ||
| "type": "string", | ||
| "description": "URL of the JSON Schema used to validate this file." | ||
| }, | ||
| "registry": { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "description": "Schema registry connection settings.", | ||
| "properties": { | ||
| "endpoint": { | ||
| "type": "string", | ||
| "format": "uri", | ||
| "description": "GraphQL endpoint of the Hive schema registry. Defaults to https://app.graphql-hive.com/graphql. Set this when self-hosting Hive. Can also be provided via the HIVE_REGISTRY environment variable or the --registry.endpoint argument." | ||
| }, | ||
| "accessToken": { | ||
| "type": "string", | ||
| "description": "Registry access token used to authenticate registry commands such as schema:publish and schema:check. Can also be provided via the HIVE_TOKEN environment variable or the --registry.accessToken argument." | ||
| } | ||
| } | ||
| }, | ||
| "cdn": { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "description": "High-availability CDN settings used by artifact:fetch.", | ||
| "properties": { | ||
| "endpoint": { | ||
| "type": "string", | ||
| "format": "uri", | ||
| "description": "High-availability CDN endpoint for fetching artifacts. Can also be provided via the HIVE_CDN_ENDPOINT environment variable or the --cdn.endpoint argument." | ||
| }, | ||
| "accessToken": { | ||
| "type": "string", | ||
| "description": "CDN access token used by artifact:fetch. Can also be provided via the --cdn.accessToken argument." | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "legacyConfig": { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "description": "Deprecated flat configuration format. Use the `registry` object (modernConfig) instead.", | ||
| "properties": { | ||
| "$schema": { | ||
| "type": "string", | ||
| "description": "URL of the JSON Schema used to validate this file." | ||
| }, | ||
| "registry": { | ||
| "type": "string", | ||
| "description": "Deprecated. Registry endpoint URL. Use `registry.endpoint` instead." | ||
| }, | ||
| "token": { | ||
| "type": "string", | ||
| "description": "Deprecated. Registry access token. Use `registry.accessToken` instead." | ||
| } | ||
| } | ||
| }, | ||
| "namespacedConfig": { | ||
| "type": "object", | ||
| "description": "Multiple named configurations ('spaces'). The active space is selected with the HIVE_SPACE environment variable, falling back to the `default` space. Each value is a Hive CLI configuration.", | ||
| "properties": { | ||
| "$schema": { | ||
| "type": "string", | ||
| "description": "URL of the JSON Schema used to validate this file." | ||
| } | ||
| }, | ||
| "additionalProperties": { | ||
| "anyOf": [ | ||
| { | ||
| "$ref": "#/definitions/modernConfig" | ||
| }, | ||
| { | ||
| "$ref": "#/definitions/legacyConfig" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/libraries/cli/scripts/generate-config-schema.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { writeFileSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { z } from 'zod'; | ||
| import { ConfigModel, LegacyConfigModel } from '../src/helpers/config'; | ||
|
|
||
| // The `$id`/`$schema` URL users reference from their `hive.json`. Must match the repository slug | ||
| // (graphql-hive/console) so the schema resolves over raw.githubusercontent.com. | ||
| const SCHEMA_ID = | ||
| 'https://raw.githubusercontent.com/graphql-hive/console/main/packages/libraries/cli/hive-config.schema.json'; | ||
|
|
||
| const SCHEMA_PATH = join(__dirname, '..', 'hive-config.schema.json'); | ||
|
|
||
| type JsonSchema = Record<string, unknown>; | ||
|
|
||
| const schemaProperty: JsonSchema = { | ||
| type: 'string', | ||
| description: 'URL of the JSON Schema used to validate this file.', | ||
| }; | ||
|
|
||
| // Minimal Zod -> JSON Schema converter covering the constructs the config models use | ||
| // (objects, strings, `.url()`, `.optional()`, `.describe()`). It throws on anything else so that | ||
| // unsupported additions fail loudly during `generate:schema` instead of silently producing an | ||
| // incorrect schema. | ||
| function toJsonSchema(schema: z.ZodTypeAny): JsonSchema { | ||
| if (schema instanceof z.ZodOptional) { | ||
| return toJsonSchema(schema.unwrap() as z.ZodTypeAny); | ||
| } | ||
|
|
||
| if (schema instanceof z.ZodObject) { | ||
| const properties: Record<string, JsonSchema> = {}; | ||
| const required: string[] = []; | ||
| const shape = schema.shape as Record<string, z.ZodTypeAny>; | ||
|
|
||
| for (const [key, value] of Object.entries(shape)) { | ||
| properties[key] = toJsonSchema(value); | ||
| if (!(value instanceof z.ZodOptional)) { | ||
| required.push(key); | ||
| } | ||
| } | ||
|
|
||
| const output: JsonSchema = { type: 'object', additionalProperties: false }; | ||
| if (schema.description) { | ||
| output.description = schema.description; | ||
| } | ||
| output.properties = properties; | ||
| if (required.length > 0) { | ||
| output.required = required; | ||
| } | ||
| return output; | ||
| } | ||
|
|
||
| if (schema instanceof z.ZodString) { | ||
| const output: JsonSchema = { type: 'string' }; | ||
| if (schema._def.checks.some(check => check.kind === 'url')) { | ||
| output.format = 'uri'; | ||
| } | ||
| if (schema.description) { | ||
| output.description = schema.description; | ||
| } | ||
| return output; | ||
| } | ||
|
|
||
| throw new Error( | ||
| `Unsupported Zod type in the hive-config schema generator: ${schema.constructor.name}. Extend toJsonSchema() in scripts/generate-config-schema.ts to handle it.`, | ||
| ); | ||
| } | ||
|
|
||
| function configDefinition(model: z.ZodTypeAny, description: string): JsonSchema { | ||
| const base = toJsonSchema(model); | ||
| const properties = (base.properties ?? {}) as Record<string, JsonSchema>; | ||
| return { | ||
| type: 'object', | ||
| additionalProperties: false, | ||
| description, | ||
| properties: { $schema: schemaProperty, ...properties }, | ||
| }; | ||
| } | ||
|
|
||
| export function buildConfigSchema(): JsonSchema { | ||
| const modernConfig = configDefinition(ConfigModel, 'Hive CLI configuration.'); | ||
| const legacyConfig = configDefinition( | ||
| LegacyConfigModel, | ||
| 'Deprecated flat configuration format. Use the `registry` object (modernConfig) instead.', | ||
| ); | ||
| const namespacedConfig: JsonSchema = { | ||
| type: 'object', | ||
| description: | ||
| "Multiple named configurations ('spaces'). The active space is selected with the HIVE_SPACE environment variable, falling back to the `default` space. Each value is a Hive CLI configuration.", | ||
| properties: { $schema: schemaProperty }, | ||
| additionalProperties: { | ||
| anyOf: [{ $ref: '#/definitions/modernConfig' }, { $ref: '#/definitions/legacyConfig' }], | ||
| }, | ||
| }; | ||
|
|
||
| return { | ||
| $schema: 'http://json-schema.org/draft-07/schema#', | ||
| $id: SCHEMA_ID, | ||
| title: 'GraphQL Hive CLI configuration (hive.json)', | ||
| description: | ||
| 'Configuration file for the GraphQL Hive CLI (`hive`). Configuration is resolved with the priority: CLI arguments > environment variables > hive.json. See https://the-guild.dev/graphql/hive/docs/api-reference/cli for details.', | ||
| $comment: | ||
| 'Generated from ConfigModel and LegacyConfigModel in src/helpers/config.ts by scripts/generate-config-schema.ts. Do not edit by hand; run `pnpm --filter @graphql-hive/cli generate:schema`.', | ||
| anyOf: [ | ||
| { $ref: '#/definitions/modernConfig' }, | ||
| { $ref: '#/definitions/legacyConfig' }, | ||
| { $ref: '#/definitions/namespacedConfig' }, | ||
| ], | ||
| definitions: { modernConfig, legacyConfig, namespacedConfig }, | ||
| }; | ||
| } | ||
|
|
||
| export function serializeConfigSchema(): string { | ||
| return `${JSON.stringify(buildConfigSchema(), null, 2)}\n`; | ||
| } | ||
|
|
||
| if (process.argv[1]?.endsWith('generate-config-schema.ts')) { | ||
| writeFileSync(SCHEMA_PATH, serializeConfigSchema()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.