Skip to content

Commit caebbe0

Browse files
fix: schema version by commit (#7155)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 8aea66b commit caebbe0

File tree

15 files changed

+77
-52
lines changed

15 files changed

+77
-52
lines changed

.changeset/late-feet-write.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-hive/cli': minor
3+
'hive': minor
4+
---
5+
6+
add schemaVersionByCommit; update docs and cli; fix webhook commit reference

integration-tests/testkit/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,13 @@ export function createCLI(tokens: { readwrite: string; readonly: string }) {
298298
]);
299299
}
300300

301-
async function fetchCmd(input: { type: 'subgraphs' | 'supergraph' | 'sdl'; actionId?: string }) {
301+
async function fetchCmd(input: { type: 'subgraphs' | 'supergraph' | 'sdl'; commit?: string }) {
302302
const cmd = schemaFetch([
303303
'--token',
304304
tokens.readwrite,
305305
'--type',
306306
input.type,
307-
...(input.actionId ? [input.actionId] : []),
307+
...(input.commit ? [input.commit] : []),
308308
]);
309309

310310
return cmd;

integration-tests/tests/api/schema/schema-version.spec.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { waitFor } from 'testkit/flow';
21
import { ProjectType } from 'testkit/gql/graphql';
32
import { graphql } from '../../../testkit/gql';
43
import { execute } from '../../../testkit/graphql';
54
import { initSeed } from '../../../testkit/seed';
65

7-
const SchemaByActionIdQuery = graphql(/* GraphQL */ `
8-
query SchemaByActionIdQuery($actionId: ID!, $targetRef: TargetReferenceInput) {
9-
schemaVersionForActionId(actionId: $actionId, target: $targetRef) {
6+
const SchemaByCommitQuery = graphql(/* GraphQL */ `
7+
query SchemaByCommitQuery($commit: String!, $targetRef: TargetReferenceInput) {
8+
schemaVersionByCommit(commit: $commit, target: $targetRef) {
109
id
1110
sdl
1211
log {
@@ -20,9 +19,9 @@ const SchemaByActionIdQuery = graphql(/* GraphQL */ `
2019
`);
2120

2221
test.concurrent(
23-
'schema version by actionId returns latest schema for the actionId',
22+
'schema version by commit returns latest schema for the commit',
2423
async ({ expect }) => {
25-
const actionId = 'tiny-test-0';
24+
const commit = 'tiny-test-0';
2625
const schema = /* GraphQL */ `
2726
type Query {
2827
ping: String
@@ -45,26 +44,26 @@ test.concurrent(
4544
await token
4645
.publishSchema({
4746
sdl: schema,
48-
commit: actionId,
47+
commit,
4948
})
5049
.then(r => r.expectNoGraphQLErrors());
5150

5251
await token
5352
.publishSchema({
5453
sdl: latestSchema,
55-
commit: actionId,
54+
commit,
5655
})
5756
.then(r => r.expectNoGraphQLErrors());
5857

5958
const result = await execute({
60-
document: SchemaByActionIdQuery,
59+
document: SchemaByCommitQuery,
6160
token: token.secret,
6261
variables: {
63-
actionId,
62+
commit,
6463
targetRef: { byId: target.id },
6564
},
6665
}).then(r => r.expectNoGraphQLErrors());
6766

68-
expect(result.schemaVersionForActionId?.sdl).toIncludeSubstringWithoutWhitespace(latestSchema);
67+
expect(result.schemaVersionByCommit?.sdl).toIncludeSubstringWithoutWhitespace(latestSchema);
6968
},
7069
);

integration-tests/tests/cli/schema.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ describe.each([ProjectType.Stitching, ProjectType.Federation, ProjectType.Single
503503
const numSchemas = schema.latestVersion?.schemas.nodes.length;
504504
const fetchCmd = cli.fetch({
505505
type: 'subgraphs',
506-
actionId: 'abc123',
506+
commit: 'abc123',
507507
});
508508
const rHeader = `service\\s+url\\s+date`;
509509
const rUrl = `http:\\/\\/\\S+(:\\d+)?|n/a`;

packages/libraries/cli/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ curl -sSL https://graphql-hive.com/install.sh | sh
4747
- [`hive operations:check FILE`](#hive-operationscheck-file)
4848
- [`hive schema:check FILE`](#hive-schemacheck-file)
4949
- [`hive schema:delete SERVICE`](#hive-schemadelete-service)
50-
- [`hive schema:fetch [ACTIONID]`](#hive-schemafetch-actionid)
50+
- [`hive schema:fetch [COMMIT]`](#hive-schemafetch-commit)
5151
- [`hive schema:publish FILE`](#hive-schemapublish-file)
5252
- [`hive update [CHANNEL]`](#hive-update-channel)
5353
- [`hive whoami`](#hive-whoami)
@@ -355,17 +355,17 @@ DESCRIPTION
355355
_See code:
356356
[src/commands/schema/delete.ts](https://github.com/graphql-hive/platform/blob/v0.50.2/src/commands/schema/delete.ts)_
357357

358-
## `hive schema:fetch [ACTIONID]`
358+
## `hive schema:fetch [COMMIT]`
359359

360360
fetch a schema, supergraph, or list of subgraphs from the Hive API
361361

362362
```
363363
USAGE
364-
$ hive schema:fetch [ACTIONID] [--debug] [--registry <value>] [--token <value>] [--registry.endpoint <value>]
364+
$ hive schema:fetch [COMMIT] [--debug] [--registry <value>] [--token <value>] [--registry.endpoint <value>]
365365
[--registry.accessToken <value>] [--type <value>] [--write <value>] [--outputFile <value>] [--target <value>]
366366
367367
ARGUMENTS
368-
ACTIONID action id (e.g. commit sha)
368+
COMMIT commit SHA (or optionally any other external identifier)
369369
370370
FLAGS
371371
--debug Whether debug output for HTTP calls and similar should be enabled.

packages/libraries/cli/src/commands/schema/fetch.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ import {
1616
import * as TargetInput from '../../helpers/target-input';
1717
import { Texture } from '../../helpers/texture/texture';
1818

19-
const SchemaVersionForActionIdQuery = graphql(/* GraphQL */ `
20-
query SchemaVersionForActionId(
21-
$actionId: ID!
19+
const SchemaVersionByCommitQuery = graphql(/* GraphQL */ `
20+
query SchemaVersionByCommit(
21+
$commit: String!
2222
$includeSDL: Boolean!
2323
$includeSupergraph: Boolean!
2424
$includeSubgraphs: Boolean!
2525
$target: TargetReferenceInput
2626
) {
27-
schemaVersionForActionId(actionId: $actionId, target: $target) {
27+
schemaVersionByCommit(commit: $commit, target: $target) {
2828
id
2929
isValid
3030
sdl @include(if: $includeSDL)
@@ -128,9 +128,9 @@ export default class SchemaFetch extends Command<typeof SchemaFetch> {
128128
};
129129

130130
static args = {
131-
actionId: Args.string({
132-
name: 'actionId' as const,
133-
description: 'action id (e.g. commit sha)',
131+
commit: Args.string({
132+
name: 'commit' as const,
133+
description: 'commit SHA, or it can be any external ID that references the schema',
134134
hidden: false,
135135
}),
136136
};
@@ -163,7 +163,7 @@ export default class SchemaFetch extends Command<typeof SchemaFetch> {
163163
throw new MissingRegistryTokenError();
164164
}
165165

166-
const { actionId } = args;
166+
const { commit } = args;
167167

168168
const sdlType = this.ensure({
169169
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -185,18 +185,18 @@ export default class SchemaFetch extends Command<typeof SchemaFetch> {
185185
}
186186

187187
let schemaVersion;
188-
if (actionId) {
188+
if (commit) {
189189
const result = await this.registryApi(endpoint, accessToken).request({
190-
operation: SchemaVersionForActionIdQuery,
190+
operation: SchemaVersionByCommitQuery,
191191
variables: {
192-
actionId,
192+
commit,
193193
includeSDL: sdlType === 'sdl',
194194
includeSupergraph: sdlType === 'supergraph',
195195
includeSubgraphs: sdlType === 'subgraphs',
196196
target,
197197
},
198198
});
199-
schemaVersion = result.schemaVersionForActionId;
199+
schemaVersion = result.schemaVersionByCommit;
200200
} else {
201201
const result = await this.registryApi(endpoint, accessToken).request({
202202
operation: LatestSchemaVersionQuery,
@@ -211,11 +211,11 @@ export default class SchemaFetch extends Command<typeof SchemaFetch> {
211211
}
212212

213213
if (schemaVersion == null) {
214-
throw new SchemaNotFoundError(actionId);
214+
throw new SchemaNotFoundError(commit);
215215
}
216216

217217
if (schemaVersion.isValid === false) {
218-
throw new InvalidSchemaError(actionId);
218+
throw new InvalidSchemaError(commit);
219219
}
220220

221221
if (schemaVersion.schemas) {
@@ -239,7 +239,7 @@ export default class SchemaFetch extends Command<typeof SchemaFetch> {
239239
const schema = schemaVersion.sdl ?? schemaVersion.supergraph;
240240

241241
if (schema == null) {
242-
throw new SchemaNotFoundError(actionId);
242+
throw new SchemaNotFoundError(commit);
243243
}
244244

245245
if (flags.write) {

packages/libraries/cli/src/commands/schema/publish.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ export default class SchemaPublish extends Command<typeof SchemaPublish> {
111111
description: 'author of the change',
112112
}),
113113
commit: Flags.string({
114-
description: 'associated commit sha',
114+
description:
115+
'The associated commit SHA, or optionally any external identifier that references the schema',
115116
}),
116117
github: Flags.boolean({
117118
description: 'Connect with GitHub Application',

packages/libraries/cli/src/helpers/errors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,21 +397,21 @@ export class InvalidTargetError extends HiveCLIError {
397397
}
398398

399399
export class SchemaNotFoundError extends HiveCLIError {
400-
constructor(actionId?: string) {
400+
constructor(commit?: string) {
401401
super(
402402
ExitCode.ERROR,
403403
errorCode(ErrorCategory.ARTIFACT_FETCH, 0),
404-
`No schema found${actionId ? ` for action id ${actionId}.` : '.'}`,
404+
`No schema found${commit ? ` for commit ${commit}.` : '.'}`,
405405
);
406406
}
407407
}
408408

409409
export class InvalidSchemaError extends HiveCLIError {
410-
constructor(actionId?: string) {
410+
constructor(commit?: string) {
411411
super(
412412
ExitCode.ERROR,
413413
errorCode(ErrorCategory.ARTIFACT_FETCH, 1),
414-
`Schema is invalid${actionId ? ` for action id ${actionId}.` : '.'}`,
414+
`Schema is invalid${commit ? ` for commit ${commit}.` : '.'}`,
415415
);
416416
}
417417
}

packages/services/api/src/modules/schema/module.graphql.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export default gql`
2727
2828
extend type Query {
2929
schemaVersionForActionId(actionId: ID!, target: TargetReferenceInput): SchemaVersion
30+
@deprecated(reason: "Use Query.schemaVersionByCommit")
31+
32+
"""
33+
Returns the latest SchemaVersion associated with a specific commit
34+
"""
35+
schemaVersionByCommit(commit: String!, target: TargetReferenceInput): SchemaVersion
3036
latestValidVersion(target: TargetReferenceInput): SchemaVersion
3137
"""
3238
Requires API Token

packages/services/api/src/modules/schema/providers/schema-manager.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,8 @@ export class SchemaManager {
977977
});
978978
}
979979

980-
async getSchemaVersionByActionId(args: {
981-
actionId: string;
980+
async getSchemaVersionByCommit(args: {
981+
commit: string;
982982
target: GraphQLSchema.TargetReferenceInput | null;
983983
}) {
984984
const selector = await this.idTranslator.resolveTargetReference({
@@ -989,10 +989,10 @@ export class SchemaManager {
989989
this.session.raise('project:describe');
990990
}
991991

992-
this.logger.debug('Fetch schema version by action id. (args=%o)', {
992+
this.logger.debug('Fetch schema version by commit. (args=%o)', {
993993
projectId: selector.projectId,
994994
targetId: selector.targetId,
995-
actionId: args.actionId,
995+
commit: args.commit,
996996
});
997997

998998
await this.session.assertPerformAction({
@@ -1004,10 +1004,10 @@ export class SchemaManager {
10041004
},
10051005
});
10061006

1007-
const record = await this.storage.getSchemaVersionByActionId({
1007+
const record = await this.storage.getSchemaVersionByCommit({
10081008
projectId: selector.projectId,
10091009
targetId: selector.targetId,
1010-
actionId: args.actionId,
1010+
commit: args.commit,
10111011
});
10121012

10131013
if (!record) {

0 commit comments

Comments
 (0)