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
17 changes: 15 additions & 2 deletions cli/src/commands/grpc-service/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { access, constants, lstat, mkdir, readFile, writeFile } from 'node:fs/promises';
import { compileGraphQLToMapping, compileGraphQLToProto, ProtoLock } from '@wundergraph/protographic';
import {
compileGraphQLToMapping,
compileGraphQLToProto,
ProtoLock,
validateGraphQLSDL,
} from '@wundergraph/protographic';
import { Command, program } from 'commander';
import { camelCase, upperFirst } from 'lodash-es';
import Spinner, { type Ora } from 'ora';
import { resolve } from 'pathe';
import { BaseCommandOptions } from '../../../core/types/types.js';
import { renderResultTree } from '../../router/commands/plugin/helper.js';
import { renderResultTree, renderValidationResults } from '../../router/commands/plugin/helper.js';

type CLIOptions = {
input: string;
Expand Down Expand Up @@ -121,6 +126,14 @@ async function generateProtoAndMapping({
spinner.text = 'Generating mapping and proto files...';

const lockData = await fetchLockData(lockFile);

// Validate the GraphQL schema and render results
spinner.text = 'Validating GraphQL schema...';
const validationResult = validateGraphQLSDL(schema);
renderValidationResults(validationResult, schemaFile);

// Continue with generation if validation passed (no errors)
spinner.text = 'Generating mapping and proto files...';
const mapping = compileGraphQLToMapping(schema, serviceName);
const proto = compileGraphQLToProto(schema, {
serviceName,
Expand Down
68 changes: 68 additions & 0 deletions cli/src/commands/router/commands/plugin/helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ValidationResult } from '@wundergraph/protographic';
import Spinner from 'ora';
import pc from 'picocolors';

Expand Down Expand Up @@ -64,3 +65,70 @@ export function renderResultTree(

console.log(output);
}

/**
* Renders validation warnings and errors in a consistent format
* @param validationResult The validation result containing errors and warnings
* @param schemaFile The path to the schema file being validated
* @throws Error if there are validation errors
*/
export function renderValidationResults(validationResult: ValidationResult, schemaFile: string): void {
const hasErrors = validationResult.errors.length > 0;
const hasWarnings = validationResult.warnings.length > 0;

if (!hasErrors && !hasWarnings) {
return; // No issues to report
}

// Render warnings first (non-blocking)
if (hasWarnings) {
const warningSymbol = pc.yellow('[!]');
console.log(`\n${warningSymbol} ${pc.bold('Schema validation warnings:')}`);
console.log(` ${pc.dim('│')}`);
console.log(` ${pc.dim('├──────── file')}: ${schemaFile}`);
console.log(` ${pc.dim('├──── warnings')}: ${pc.yellow(validationResult.warnings.length.toString())}`);
console.log(` ${pc.dim('│')}`);

for (const [index, warning] of validationResult.warnings.slice(0, 10).entries()) {
// take at max 10
const isLast = index === validationResult.warnings.length - 1 && !hasErrors;
const connector = isLast ? '└─' : '├─';
console.log(` ${pc.dim(connector)} ${pc.yellow('warn')}: ${warning.replace('[Warning] ', '')}`);
}

if (validationResult.warnings.length > 10) {
console.log(` ${pc.dim('└─')} ${pc.dim('...and more warnings...')}`);
}

if (!hasErrors) {
console.log(` ${pc.dim('│')}`);
console.log(` ${pc.dim('└─')} ${pc.dim('Continuing with generation despite warnings...')}\n`);
}
}

// Render errors (blocking)
if (hasErrors) {
const errorSymbol = pc.red('[✕]');
console.log(`\n${errorSymbol} ${pc.bold('Schema validation errors:')}`);
console.log(` ${pc.dim('│')}`);
console.log(` ${pc.dim('├──────── file')}: ${schemaFile}`);
console.log(` ${pc.dim('├────── errors')}: ${pc.red(validationResult.errors.length.toString())}`);
console.log(` ${pc.dim('│')}`);

for (const [index, error] of validationResult.errors.slice(0, 10).entries()) {
// take at max 10
const isLast = index === validationResult.errors.length - 1;
const connector = isLast ? '└─' : '├─';
console.log(` ${pc.dim(connector)} ${pc.red('error')}: ${error.replace('[Error] ', '')}`);
}

if (validationResult.errors.length > 10) {
console.log(` ${pc.dim('└─')} ${pc.dim('...and more errors...')}`);
}

console.log(` ${pc.dim('│')}`);
console.log(` ${pc.dim('└─')} ${pc.dim('Generation stopped due to validation errors.')}\n`);

throw new Error(`Schema validation failed with ${validationResult.errors.length} error(s)`);
}
}
16 changes: 14 additions & 2 deletions cli/src/commands/router/commands/plugin/toolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import { existsSync } from 'node:fs';
import { basename, join, resolve } from 'pathe';
import pc from 'picocolors';
import { execa } from 'execa';
import { compileGraphQLToMapping, compileGraphQLToProto, ProtoLock } from '@wundergraph/protographic';
import {
compileGraphQLToMapping,
compileGraphQLToProto,
ProtoLock,
validateGraphQLSDL,
} from '@wundergraph/protographic';
import prompts from 'prompts';
import semver from 'semver';
import { camelCase, upperFirst } from 'lodash-es';
import { dataDir } from '../../../../core/config.js';
import { renderValidationResults } from './helper.js';

// Define platform-architecture combinations
export const HOST_PLATFORM = `${os.platform()}-${getOSArch()}`;
Expand Down Expand Up @@ -359,7 +365,8 @@ export async function generateProtoAndMapping(pluginDir: string, goModulePath: s
await mkdir(generatedDir, { recursive: true });

spinner.text = 'Reading schema...';
const schema = await readFile(resolve(srcDir, 'schema.graphql'), 'utf8');
const schemaFile = resolve(srcDir, 'schema.graphql');
const schema = await readFile(schemaFile, 'utf8');
const lockFile = resolve(generatedDir, 'service.proto.lock.json');

let lockData: ProtoLock | undefined;
Expand All @@ -374,6 +381,11 @@ export async function generateProtoAndMapping(pluginDir: string, goModulePath: s

const serviceName = upperFirst(camelCase(pluginName)) + 'Service';

// Validate the GraphQL schema and render results
spinner.text = 'Validating GraphQL schema...';
const validationResult = validateGraphQLSDL(schema);
renderValidationResults(validationResult, schemaFile);

spinner.text = 'Generating mapping and proto files...';

const mapping = compileGraphQLToMapping(schema, serviceName);
Expand Down
18 changes: 18 additions & 0 deletions cli/test/fixtures/schema-with-nullable-list-items.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type Query {
projects: [Project]!
}

type Project {
id: ID!
name: String!
description: String
startDate: String # ISO date
endDate: String # ISO date
status: ProjectStatus!
tags: [String]!
}

enum ProjectStatus {
ACTIVE
INACTIVE
}
13 changes: 13 additions & 0 deletions cli/test/fixtures/schema-with-validation-errors.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Query {
user: User!
}

type Nested {
name: String!
}

# This will generate an error due to nested key directive
type User @key(fields: "id nested { name }") {
id: ID!
nested: Nested!
}
16 changes: 16 additions & 0 deletions cli/test/fixtures/schema-with-warnings-and-errors.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type Query {
# This will generate warnings about nullable list items
items: [String]
users: [User]
user: User!
}

type Nested {
name: String!
}

# This will generate an error due to nested key directive
type User @key(fields: "id nested { name }") {
id: ID!
nested: Nested!
}
132 changes: 129 additions & 3 deletions cli/test/grpc-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { rmSync, mkdirSync, existsSync, writeFileSync, rmdirSync } from 'node:fs';
import { join } from 'node:path';
import { join, resolve } from 'node:path';
import { tmpdir } from 'node:os';
import { fileURLToPath } from 'node:url';
import { Command } from 'commander';
import { describe, test, expect } from 'vitest';
import { createPromiseClient, createRouterTransport } from '@connectrpc/connect';
import { PlatformService } from '@wundergraph/cosmo-connect/dist/platform/v1/platform_connect';
import { dirname } from 'pathe';
import GenerateCommand from '../src/commands/grpc-service/commands/generate.js';
import GRPCCommands from '../src/commands/grpc-service/index.js';
import { Client } from '../src/core/client/client.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

export const mockPlatformTransport = () =>
createRouterTransport(({ service }) => {
service(PlatformService, {});
Expand All @@ -30,12 +34,14 @@ describe('gRPC Generate Command', () => {
rmdirSync(tmpDir, { recursive: true });
});

const schemaPath = resolve(__dirname, 'fixtures', 'full-schema.graphql');

await program.parseAsync(
[
'generate',
'testservice',
'-i',
'test/fixtures/full-schema.graphql',
schemaPath,
'-o',
tmpDir,
],
Expand Down Expand Up @@ -65,12 +71,14 @@ describe('gRPC Generate Command', () => {
rmSync(nonExistentDir, { recursive: true, force: true });
}

const schemaPath = resolve(__dirname, 'fixtures', 'full-schema.graphql');

await program.parseAsync(
[
'generate',
'testservice',
'-i',
'test/fixtures/full-schema.graphql',
schemaPath,
'-o',
nonExistentDir,
],
Expand Down Expand Up @@ -162,4 +170,122 @@ describe('gRPC Generate Command', () => {
}
)).rejects.toThrow('process.exit unexpectedly called with "1"');
});

test('should generate all files with warnings', async (testContext) => {
const client: Client = {
platform: createPromiseClient(PlatformService, mockPlatformTransport()),
};

const program = new Command();
program.addCommand(GenerateCommand({ client }));

const tmpDir = join(tmpdir(), `grpc-test-${Date.now()}`);
mkdirSync(tmpDir, { recursive: true });

testContext.onTestFinished(() => {
rmdirSync(tmpDir, { recursive: true });
});

const schemaPath = resolve(__dirname, 'fixtures', 'schema-with-nullable-list-items.graphql');

// Should complete successfully despite warnings
await program.parseAsync(
[
'generate',
'testservice',
'-i',
schemaPath,
'-o',
tmpDir,
],
{
from: 'user',
}
);

// Verify the output files exist (generation should continue with warnings)
expect(existsSync(join(tmpDir, 'mapping.json'))).toBe(true);
expect(existsSync(join(tmpDir, 'service.proto'))).toBe(true);
expect(existsSync(join(tmpDir, 'service.proto.lock.json'))).toBe(true);
});

test('should fail when schema has validation errors', async (testContext) => {
const client: Client = {
platform: createPromiseClient(PlatformService, mockPlatformTransport()),
};

const program = new Command();
program.addCommand(GenerateCommand({ client }));

const tmpDir = join(tmpdir(), `grpc-test-${Date.now()}`);
mkdirSync(tmpDir, { recursive: true });

testContext.onTestFinished(() => {
rmdirSync(tmpDir, { recursive: true });
});

const schemaPath = resolve(__dirname, 'fixtures', 'schema-with-validation-errors.graphql');

// Should fail due to validation errors
await expect(
program.parseAsync(
[
'generate',
'testservice',
'-i',
schemaPath,
'-o',
tmpDir,
],
{
from: 'user',
}
)
).rejects.toThrow('Schema validation failed');

// Verify no output files were created (generation should stop on errors)
expect(existsSync(join(tmpDir, 'mapping.json'))).toBe(false);
expect(existsSync(join(tmpDir, 'service.proto'))).toBe(false);
expect(existsSync(join(tmpDir, 'service.proto.lock.json'))).toBe(false);
});

test('should display warnings and stop on errors', async (testContext) => {
const client: Client = {
platform: createPromiseClient(PlatformService, mockPlatformTransport()),
};

const program = new Command();
program.addCommand(GenerateCommand({ client }));

const tmpDir = join(tmpdir(), `grpc-test-${Date.now()}`);
mkdirSync(tmpDir, { recursive: true });

testContext.onTestFinished(() => {
rmdirSync(tmpDir, { recursive: true });
});

const schemaPath = resolve(__dirname, 'fixtures', 'schema-with-warnings-and-errors.graphql');

// Should fail due to validation errors (despite having warnings)
await expect(
program.parseAsync(
[
'generate',
'testservice',
'-i',
schemaPath,
'-o',
tmpDir,
],
{
from: 'user',
}
)
).rejects.toThrow('Schema validation failed');

// Verify no output files were created (generation should stop on errors)
expect(existsSync(join(tmpDir, 'mapping.json'))).toBe(false);
expect(existsSync(join(tmpDir, 'service.proto'))).toBe(false);
expect(existsSync(join(tmpDir, 'service.proto.lock.json'))).toBe(false);
});
});
Loading