Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-schema-utilities",
"version": "1.1.3",
"version": "1.1.4",
"description": "Merge GraphQL Schema files and validate queries against the merged Schema",
"repository": {
"type": "git",
Expand Down
8 changes: 7 additions & 1 deletion src/cli-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ program
'Use a glob that that contains your graphql operation files to test against the merged schema file.', '')
.option('-d, --includeDirectives',
'By default will NOT merge the directives, unless you added this flag.', false)
.option('-q, --quiet',
'Run in quite mode, suppressing all logs except errors.', false)
.parse(process.argv);

if (!program.schema) {
Expand All @@ -49,7 +51,11 @@ if (!program.schema) {
? '\n subscription: Subscription' : ''} \n}\n\n`;
data = typeDefs + data;
}
process.stdout.write(data);

if (!program.quiet) {
process.stdout.write(data);
}

if (program.output) {
ensureDirectoryExistence(program.output);
fs.writeFile(program.output, data, (err) => {
Expand Down
19 changes: 10 additions & 9 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
makeExecutableSchema,
} from 'graphql-tools';
import * as validator from './index';
import { consoleLogger } from './logger';
import { ValidationRule } from './types';
import * as utils from './utilities';
import { validateSchemaWithSourceMap } from './validate-schema';
Expand All @@ -24,11 +25,11 @@ import { validateSchemaWithSourceMap } from './validate-schema';
*/
export function mergeGQLSchemas(schemaPattern: string): Promise<GraphQLSchema> {
return new Promise((resolve, reject) => {
console.log(`\nLoading schema from ${schemaPattern}`);
consoleLogger.log(`\nLoading schema from ${schemaPattern}`);
utils.readGlob(schemaPattern).then((files) => {
if (!files.length) {
const noFilesMatching = `No matching files were found with Glob: ${schemaPattern}.`;
console.error(noFilesMatching);
consoleLogger.error(noFilesMatching);
reject(new Error(noFilesMatching));
}
Promise.all(
Expand All @@ -37,7 +38,7 @@ export function mergeGQLSchemas(schemaPattern: string): Promise<GraphQLSchema> {
.readFile(file)
.then((subSchema: string) => subSchema)
.catch((error) => {
console.error(
consoleLogger.error(
`An error occurred while trying to read your graphql schemas in ${schemaPattern}. \n`,
error,
);
Expand Down Expand Up @@ -71,25 +72,25 @@ export function validateOperations(
rules?: ReadonlyArray<ValidationRule>,
): Promise<void> {
return new Promise<void>((resolve, reject) => {
console.log(
consoleLogger.log(
`\nValidating queries for ${queriesPattern} using loaded schema`,
);

function outputErrors(errs) {
console.log('\nErrors found:');
consoleLogger.log('\nErrors found:');
errs.forEach((err) => {
console.log(`\nFile: ${err.file}`);
consoleLogger.log(`\nFile: ${err.file}`);
err.errors.forEach((errStr) => {
console.log(`\t${errStr}`);
consoleLogger.log(`\t${errStr}`);
});
});
console.log('\n');
consoleLogger.log('\n');
}

validator
.validateQueryFiles(queriesPattern, validSchema, rules)
.then(() => {
console.log('All queries are valid\n');
consoleLogger.log('All queries are valid\n');
resolve();
})
.catch((errs) => {
Expand Down
10 changes: 8 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import chalk from 'chalk';

import * as program from 'commander';

export interface ILogger {
error(...args);
warn(...args);
Expand All @@ -12,11 +14,15 @@ export class ConsoleLogger implements ILogger {
}

public log(message: string, ...args) {
console.log(message, args.length ? args : '');
if (!program.quiet) {
console.log(message, args.length ? args : '');
}
}

public warn(warnMessage: string, ...args) {
console.warn(chalk.yellow(warnMessage), args.length ? args : '');
if (!program.quiet) {
console.warn(chalk.yellow(warnMessage), args.length ? args : '');
}
}
}

Expand Down