Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/strange-shrimps-battle.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 4 additions & 2 deletions integration-tests/testkit/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>) {
const outout = await execaCommand(`${binPath} ${cmd}`, {
shell: true,
env: {
OCLIF_CLI_CUSTOM_PATH: cliDir,
NODE_OPTIONS: '--no-deprecation',
...env,
},
});

Expand All @@ -44,11 +45,12 @@ export async function schemaPublish(args: string[]) {
);
}

export async function schemaCheck(args: string[]) {
export async function schemaCheck(args: string[], env?: Record<string, string>) {
const registryAddress = await getServiceHost('server', 8082);

return await exec(
['schema:check', `--registry.endpoint`, `http://${registryAddress}/graphql`, ...args].join(' '),
env,
);
}

Expand Down
44 changes: 44 additions & 0 deletions integration-tests/tests/cli/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
`);
});
11 changes: 5 additions & 6 deletions packages/libraries/cli/src/commands/schema/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -60,7 +60,7 @@ const schemaCheckMutation = graphql(/* GraphQL */ `
webUrl
}
}
... on SchemaCheckError @skip(if: $usesGitHubApp) {
... on SchemaCheckError {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the API returns SchemaCheckError (which it does when you use the --github flag without a --service flag on a federation project), we should still get these fields and print the error to the user.

valid
changes {
nodes {
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -274,7 +274,6 @@ export default class SchemaCheck extends Command<typeof SchemaCheck> {
contextId: flags.contextId ?? undefined,
target,
},
usesGitHubApp,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading