Skip to content

Commit b820c7e

Browse files
jdollehasparus
authored andcommitted
feat: accept url in schema check (#6582)
1 parent 3be1a5c commit b820c7e

File tree

7 files changed

+143
-1
lines changed

7 files changed

+143
-1
lines changed

.changeset/red-needles-know.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-hive/cli': patch
3+
'hive': patch
4+
---
5+
6+
Adds optional url argument to schema checks

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ stdout--------------------------------------------:
105105
__NONE__
106106
`;
107107

108+
exports[`FEDERATION > schema:check accepts a \`--url\` argument in distributed projects 1`] = `
109+
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
110+
111+
stdout--------------------------------------------:
112+
✔ Schema registry is empty, nothing to compare your schema with.
113+
View full report:
114+
http://__URL__
115+
`;
116+
108117
exports[`FEDERATION > schema:check should notify user when registry is empty > schemaCheck 1`] = `
109118
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
110119
@@ -261,6 +270,20 @@ stdout--------------------------------------------:
261270
__NONE__
262271
`;
263272

273+
exports[`SINGLE > schema:check rejects a \`--url\` argument in single projects 1`] = `
274+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
275+
exitCode------------------------------------------:
276+
1
277+
stderr--------------------------------------------:
278+
__NONE__
279+
stdout--------------------------------------------:
280+
✖ Detected 1 error
281+
282+
- url is only supported by distributed projects
283+
284+
285+
`;
286+
264287
exports[`SINGLE > schema:check should notify user when registry is empty > schemaCheck 1`] = `
265288
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
266289
@@ -433,6 +456,15 @@ stdout--------------------------------------------:
433456
__NONE__
434457
`;
435458

459+
exports[`STITCHING > schema:check accepts a \`--url\` argument in distributed projects 1`] = `
460+
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
461+
462+
stdout--------------------------------------------:
463+
✔ Schema registry is empty, nothing to compare your schema with.
464+
View full report:
465+
http://__URL__
466+
`;
467+
436468
exports[`STITCHING > schema:check should notify user when registry is empty > schemaCheck 1`] = `
437469
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
438470

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,76 @@ describe.each([ProjectType.Stitching, ProjectType.Federation, ProjectType.Single
354354
await expect(fetchCmd).resolves.toMatchSnapshot('latest sdl');
355355
},
356356
);
357+
358+
test.skipIf(projectType !== ProjectType.Single)(
359+
'schema:check rejects a `--url` argument in single projects',
360+
async ({ expect }) => {
361+
const { createOrg } = await initSeed().createOwner();
362+
const { inviteAndJoinMember, createProject } = await createOrg();
363+
await inviteAndJoinMember();
364+
const { createTargetAccessToken } = await createProject(projectType);
365+
const { secret } = await createTargetAccessToken({});
366+
367+
await expect(
368+
schemaCheck(
369+
[
370+
'--registry.accessToken',
371+
secret,
372+
'--service',
373+
'example',
374+
'--url',
375+
'https://example.graphql-hive.com/graphql',
376+
'--author',
377+
'Kamil',
378+
'fixtures/init-schema.graphql',
379+
],
380+
{
381+
// set these environment variables to "emulate" a GitHub actions environment
382+
// We set GITHUB_EVENT_PATH to "" because on our CI it can be present and we want
383+
// consistent snapshot output behaviour.
384+
GITHUB_ACTIONS: '1',
385+
GITHUB_REPOSITORY: 'foo/foo',
386+
GITHUB_EVENT_PATH: '',
387+
},
388+
),
389+
).rejects.toMatchSnapshot();
390+
},
391+
);
392+
393+
test.skipIf(projectType === ProjectType.Single)(
394+
'schema:check accepts a `--url` argument in distributed projects',
395+
async ({ expect }) => {
396+
const { createOrg } = await initSeed().createOwner();
397+
const { inviteAndJoinMember, createProject } = await createOrg();
398+
await inviteAndJoinMember();
399+
const { createTargetAccessToken } = await createProject(projectType);
400+
const { secret } = await createTargetAccessToken({});
401+
402+
await expect(
403+
schemaCheck(
404+
[
405+
'--registry.accessToken',
406+
secret,
407+
'--service',
408+
'example',
409+
'--url',
410+
'https://example.graphql-hive.com/graphql',
411+
'--author',
412+
'Kamil',
413+
'fixtures/init-schema.graphql',
414+
],
415+
{
416+
// set these environment variables to "emulate" a GitHub actions environment
417+
// We set GITHUB_EVENT_PATH to "" because on our CI it can be present and we want
418+
// consistent snapshot output behaviour.
419+
GITHUB_ACTIONS: '1',
420+
GITHUB_REPOSITORY: 'foo/foo',
421+
GITHUB_EVENT_PATH: '',
422+
},
423+
),
424+
).resolves.toMatchSnapshot();
425+
},
426+
);
357427
},
358428
);
359429

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ export default class SchemaCheck extends Command<typeof SchemaCheck> {
161161
' This can either be a slug following the format "$organizationSlug/$projectSlug/$targetSlug" (e.g "the-guild/graphql-hive/staging")' +
162162
' or an UUID (e.g. "a0f4c605-6541-4350-8cfe-b31f21a4bf80").',
163163
}),
164+
url: Flags.string({
165+
description:
166+
'If checking a service, then you can optionally provide the service URL to see the difference in the supergraph during the check.',
167+
}),
164168
};
165169

166170
static args = {
@@ -273,6 +277,7 @@ export default class SchemaCheck extends Command<typeof SchemaCheck> {
273277
: null,
274278
contextId: flags.contextId ?? undefined,
275279
target,
280+
url: flags.url,
276281
},
277282
},
278283
});

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,10 @@ export default gql`
588588
Manually approved breaking changes will be memorized for schema checks with the same context id.
589589
"""
590590
contextId: String
591+
"""
592+
Optional url if wanting to show subgraph url changes inside checks.
593+
"""
594+
url: String
591595
}
592596
593597
input SchemaDeleteInput {

packages/services/api/src/modules/schema/providers/models/composite.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export class CompositeModel {
108108
input: {
109109
sdl: string;
110110
serviceName: string;
111+
url: string | null;
111112
};
112113
selector: {
113114
organizationId: string;
@@ -146,7 +147,9 @@ export class CompositeModel {
146147
sdl: input.sdl,
147148
service_name: input.serviceName,
148149
service_url:
149-
latest?.schemas?.find(s => s.service_name === input.serviceName)?.service_url ?? 'temp',
150+
input.url ??
151+
latest?.schemas?.find(s => s.service_name === input.serviceName)?.service_url ??
152+
'temp',
150153
action: 'PUSH',
151154
metadata: null,
152155
};

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,27 @@ export class SchemaPublisher {
341341
});
342342
}
343343

344+
// if url is provided but this is not a distributed project
345+
if (
346+
input.url != null &&
347+
!(project.type === ProjectType.FEDERATION || project.type === ProjectType.STITCHING)
348+
) {
349+
this.logger.debug('url is only supported by distributed projects (type=%s)', project.type);
350+
increaseSchemaCheckCountMetric('rejected');
351+
352+
return {
353+
__typename: 'SchemaCheckError',
354+
valid: false,
355+
changes: [],
356+
warnings: [],
357+
errors: [
358+
{
359+
message: 'url is only supported by distributed projects',
360+
},
361+
],
362+
} as const;
363+
}
364+
344365
if (
345366
(project.type === ProjectType.FEDERATION || project.type === ProjectType.STITCHING) &&
346367
input.service == null
@@ -548,6 +569,7 @@ export class SchemaPublisher {
548569
input: {
549570
sdl,
550571
serviceName: input.service,
572+
url: input.url ?? null,
551573
},
552574
selector,
553575
latest: latestVersion

0 commit comments

Comments
 (0)