diff --git a/.changeset/nice-geese-learn.md b/.changeset/nice-geese-learn.md new file mode 100644 index 0000000000..115168e81b --- /dev/null +++ b/.changeset/nice-geese-learn.md @@ -0,0 +1,5 @@ +--- +'@graphql-hive/cli': minor +--- + +Add message about empty schema registry in schema:check diff --git a/integration-tests/fixtures/missing-type.graphql b/integration-tests/fixtures/missing-type.graphql new file mode 100644 index 0000000000..cb4e059583 --- /dev/null +++ b/integration-tests/fixtures/missing-type.graphql @@ -0,0 +1,3 @@ +type Query { + users: [User!]! +} diff --git a/integration-tests/tests/cli/schema.spec.ts b/integration-tests/tests/cli/schema.spec.ts index ccfb4a2ba2..f7ed7e142f 100644 --- a/integration-tests/tests/cli/schema.spec.ts +++ b/integration-tests/tests/cli/schema.spec.ts @@ -195,3 +195,97 @@ test('service url should be required in Federation', async () => { ]) ).rejects.toThrowError(/url/); }); + +test('schema:check should notify user when registry is empty', async () => { + const { access_token: owner_access_token } = await authenticate('main'); + const orgResult = await createOrganization( + { + name: 'foo', + }, + owner_access_token + ); + const org = orgResult.body.data!.createOrganization.ok!.createdOrganizationPayload.organization; + const code = org.inviteCode; + + // Join + const { access_token: member_access_token } = await authenticate('extra'); + await joinOrganization(code, member_access_token); + + const projectResult = await createProject( + { + organization: org.cleanId, + type: ProjectType.Single, + name: 'foo', + }, + owner_access_token + ); + + const project = projectResult.body.data!.createProject.ok!.createdProject; + const target = projectResult.body.data!.createProject.ok!.createdTarget; + + // Create a token with write rights + const writeTokenResult = await createToken( + { + name: 'test', + organization: org.cleanId, + project: project.cleanId, + target: target.cleanId, + organizationScopes: [], + projectScopes: [], + targetScopes: [TargetAccessScope.RegistryRead, TargetAccessScope.RegistryWrite], + }, + owner_access_token + ); + expect(writeTokenResult.body.errors).not.toBeDefined(); + const writeToken = writeTokenResult.body.data!.createToken.ok!.secret; + + await expect(schemaCheck(['--token', writeToken, 'fixtures/init-schema.graphql'])).resolves.toMatch('empty'); +}); + +test('schema:check should throw on corrupted schema', async () => { + const { access_token: owner_access_token } = await authenticate('main'); + const orgResult = await createOrganization( + { + name: 'foo', + }, + owner_access_token + ); + const org = orgResult.body.data!.createOrganization.ok!.createdOrganizationPayload.organization; + const code = org.inviteCode; + + // Join + const { access_token: member_access_token } = await authenticate('extra'); + await joinOrganization(code, member_access_token); + + const projectResult = await createProject( + { + organization: org.cleanId, + type: ProjectType.Single, + name: 'foo', + }, + owner_access_token + ); + + const project = projectResult.body.data!.createProject.ok!.createdProject; + const target = projectResult.body.data!.createProject.ok!.createdTarget; + + // Create a token with write rights + const writeTokenResult = await createToken( + { + name: 'test', + organization: org.cleanId, + project: project.cleanId, + target: target.cleanId, + organizationScopes: [], + projectScopes: [], + targetScopes: [TargetAccessScope.RegistryRead, TargetAccessScope.RegistryWrite], + }, + owner_access_token + ); + expect(writeTokenResult.body.errors).not.toBeDefined(); + const writeToken = writeTokenResult.body.data!.createToken.ok!.secret; + + const output = schemaCheck(['--token', writeToken, 'fixtures/missing-type.graphql']); + + await expect(output).rejects.toThrowError('Unknown type'); +}); diff --git a/packages/libraries/cli/src/commands/schema/check.graphql b/packages/libraries/cli/src/commands/schema/check.graphql index dc413e4404..ac008efc14 100644 --- a/packages/libraries/cli/src/commands/schema/check.graphql +++ b/packages/libraries/cli/src/commands/schema/check.graphql @@ -3,6 +3,7 @@ mutation schemaCheck($input: SchemaCheckInput!, $usesGitHubApp: Boolean!) { __typename ... on SchemaCheckSuccess @skip(if: $usesGitHubApp) { valid + initial changes { nodes { message diff --git a/packages/libraries/cli/src/commands/schema/check.ts b/packages/libraries/cli/src/commands/schema/check.ts index 689ada60d3..898e6b5cf4 100644 --- a/packages/libraries/cli/src/commands/schema/check.ts +++ b/packages/libraries/cli/src/commands/schema/check.ts @@ -87,7 +87,9 @@ export default class SchemaCheck extends Command { if (result.schemaCheck.__typename === 'SchemaCheckSuccess') { const changes = result.schemaCheck.changes; - if (!changes?.total) { + if (result.schemaCheck.initial) { + this.success('Schema registry is empty, nothing to compare your schema with.'); + } else if (!changes?.total) { this.success('No changes'); } else { renderChanges.call(this, changes);