diff --git a/.changeset/strange-shrimps-battle.md b/.changeset/strange-shrimps-battle.md new file mode 100644 index 0000000000..8df78046ac --- /dev/null +++ b/.changeset/strange-shrimps-battle.md @@ -0,0 +1,6 @@ +--- +'@graphql-hive/cli': patch +--- + +Show correct error message when attempting a schema check on a federation project without the +`--service` paramater. diff --git a/integration-tests/testkit/cli.ts b/integration-tests/testkit/cli.ts index 6328eb129f..1158078de7 100644 --- a/integration-tests/testkit/cli.ts +++ b/integration-tests/testkit/cli.ts @@ -19,12 +19,13 @@ async function generateTmpFile(content: string, extension: string) { return filepath; } -async function exec(cmd: string) { +async function exec(cmd: string, env?: Record) { const outout = await execaCommand(`${binPath} ${cmd}`, { shell: true, env: { OCLIF_CLI_CUSTOM_PATH: cliDir, NODE_OPTIONS: '--no-deprecation', + ...env, }, }); @@ -44,11 +45,12 @@ export async function schemaPublish(args: string[]) { ); } -export async function schemaCheck(args: string[]) { +export async function schemaCheck(args: string[], env?: Record) { const registryAddress = await getServiceHost('server', 8082); return await exec( ['schema:check', `--registry.endpoint`, `http://${registryAddress}/graphql`, ...args].join(' '), + env, ); } diff --git a/integration-tests/tests/cli/schema.spec.ts b/integration-tests/tests/cli/schema.spec.ts index 64640ad977..84dcb9add6 100644 --- a/integration-tests/tests/cli/schema.spec.ts +++ b/integration-tests/tests/cli/schema.spec.ts @@ -453,3 +453,47 @@ test.concurrent( `); }, ); + +test('schema:check gives correct error message for missing `--service` name flag in federation project', async ({ + expect, +}) => { + const { createOrg } = await initSeed().createOwner(); + const { inviteAndJoinMember, createProject } = await createOrg(); + await inviteAndJoinMember(); + const { createTargetAccessToken } = await createProject(ProjectType.Federation); + const { secret } = await createTargetAccessToken({}); + + await expect( + schemaCheck( + [ + '--registry.accessToken', + secret, + '--github', + '--author', + 'Kamil', + 'fixtures/init-schema.graphql', + ], + { + // set these environment variables to "emulate" a GitHub actions environment + // We set GITHUB_EVENT_PATH to "" because on our CI it can be present and we want + // consistent snapshot output behaviour. + GITHUB_ACTIONS: '1', + GITHUB_REPOSITORY: 'foo/foo', + GITHUB_EVENT_PATH: '', + }, + ), + ).rejects.toMatchInlineSnapshot(` + :::::::::::::::: CLI FAILURE OUTPUT ::::::::::::::: + exitCode------------------------------------------: + 1 + stderr--------------------------------------------: + › Warning: Could not resolve pull request number. Are you running this + › command on a 'pull_request' event? + › See https://__URL__ + › b-workflow-for-ci + stdout--------------------------------------------: + ✖ Detected 1 error + + - Missing service name + `); +}); diff --git a/packages/libraries/cli/src/commands/schema/check.ts b/packages/libraries/cli/src/commands/schema/check.ts index 3ce982d35a..0adca1eb1b 100644 --- a/packages/libraries/cli/src/commands/schema/check.ts +++ b/packages/libraries/cli/src/commands/schema/check.ts @@ -26,10 +26,10 @@ import { import * as TargetInput from '../../helpers/target-input'; const schemaCheckMutation = graphql(/* GraphQL */ ` - mutation schemaCheck($input: SchemaCheckInput!, $usesGitHubApp: Boolean!) { + mutation schemaCheck($input: SchemaCheckInput!) { schemaCheck(input: $input) { __typename - ... on SchemaCheckSuccess @skip(if: $usesGitHubApp) { + ... on SchemaCheckSuccess { valid initial warnings { @@ -60,7 +60,7 @@ const schemaCheckMutation = graphql(/* GraphQL */ ` webUrl } } - ... on SchemaCheckError @skip(if: $usesGitHubApp) { + ... on SchemaCheckError { valid changes { nodes { @@ -90,10 +90,10 @@ const schemaCheckMutation = graphql(/* GraphQL */ ` webUrl } } - ... on GitHubSchemaCheckSuccess @include(if: $usesGitHubApp) { + ... on GitHubSchemaCheckSuccess { message } - ... on GitHubSchemaCheckError @include(if: $usesGitHubApp) { + ... on GitHubSchemaCheckError { message } } @@ -274,7 +274,6 @@ export default class SchemaCheck extends Command { contextId: flags.contextId ?? undefined, target, }, - usesGitHubApp, }, }); diff --git a/packages/services/api/src/modules/schema/providers/schema-publisher.ts b/packages/services/api/src/modules/schema/providers/schema-publisher.ts index 2dca30ce7a..23b660ab8e 100644 --- a/packages/services/api/src/modules/schema/providers/schema-publisher.ts +++ b/packages/services/api/src/modules/schema/providers/schema-publisher.ts @@ -346,6 +346,7 @@ export class SchemaPublisher { ) { this.logger.debug('No service name provided (type=%s)', project.type); increaseSchemaCheckCountMetric('rejected'); + return { __typename: 'SchemaCheckError', valid: false,