Skip to content

Commit

Permalink
All at once with GraphQL Config
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Oct 22, 2019
1 parent 44d776e commit d157075
Show file tree
Hide file tree
Showing 22 changed files with 872 additions and 230 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Use Node.js ${{ matrix.node_version }}
uses: actions/setup-node@master
with:
version: ${{ matrix.node_version }}
node-version: ${{ matrix.node_version }}

- name: Install
run: |
Expand Down
13 changes: 13 additions & 0 deletions example/.graphqlrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
schema: ./schemas/schema.graphql
documents: ./documents/*.graphql
extensions:
inspector:
commands:
diff:
schema: ./schemas/new.graphql
my-diff:
command: diff
schema: ./schemas/schema.js
coverage: {}
validate: {}
similar: {}
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "example",
"scripts": {
"graphql-inspector": "node ../packages/cli/dist/index.js",
"all": "yarn graphql-inspector run",
"ui": "yarn graphql-inspector ui",
"diff": "yarn graphql-inspector diff ./schemas/schema.graphql ./schemas/new.graphql",
"diff:master": "yarn graphql-inspector diff git:master:example/schemas/schema.graphql ./schemas/schema.graphql",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/__tests__/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('diff', () => {
spyEmit.mock.calls.find(hasMessage('Detected the following changes')),
).not.toBeDefined();

expect(spyProcessExit).toHaveBeenCalledWith(0);
expect(spyProcessExit).not.toHaveBeenCalledWith(1);
});

test('should load different schema from graphql file', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/__tests__/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ describe('validate', () => {
spyEmit.mock.calls.find(hasMessage('All documents are valid')),
).not.toBeDefined();

expect(spyProcessExit).toHaveBeenCalledWith(0);
expect(spyProcessExit).toHaveBeenCalledWith(1);
});
});
5 changes: 5 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@
"commander": "3.0.2",
"express": "4.17.1",
"figures": "3.1.0",
"graphql-config": "3.0.0-alpha.13",
"@graphql-toolkit/core": "0.6.1-alpha-cee1e98.4+cee1e98",
"@graphql-toolkit/code-file-loader": "0.6.1-alpha-cee1e98.4+cee1e98",
"indent-string": "4.0.0",
"is-valid-path": "0.1.1",
"listr": "0.14.3",
"log-symbols": "3.0.0",
"open": "7.0.0"
},
Expand All @@ -54,6 +58,7 @@
"@types/graphql": "14.5.0",
"@types/indent-string": "4.0.1",
"@types/jest": "24.0.19",
"@types/listr": "0.14.2",
"@types/log-symbols": "3.0.0",
"@types/node": "12.11.5",
"graphql": "14.5.8",
Expand Down
90 changes: 57 additions & 33 deletions packages/cli/src/commands/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SchemaCoverage,
} from '@graphql-inspector/core';
import {loadSchema, loadDocuments} from '@graphql-inspector/load';
import {GraphQLSchema, Source} from 'graphql';

import {ensureAbsolute} from '../utils/fs';
import {Renderer, ConsoleRenderer} from '../render';
Expand All @@ -14,6 +15,55 @@ export function outputJSON(coverage: SchemaCoverage): string {
return JSON.stringify(coverage, null, 2);
}

export async function runCoverage({
documents,
schema,
renderer,
writePath,
silent,
}: {
documents: Source[];
schema: GraphQLSchema;
renderer: Renderer;
writePath?: string;
silent?: boolean;
}) {
const shouldWrite = typeof writePath !== 'undefined';

const coverage = calculateCoverage(schema, documents);

if (!silent) {
renderer.coverage(coverage);
}

if (shouldWrite) {
if (typeof writePath !== 'string' || !isValidPath(writePath)) {
throw new Error(`--write is not valid file path: ${writePath}`);
}

const absPath = ensureAbsolute(writePath);
const ext = extname(absPath)
.replace('.', '')
.toLocaleLowerCase();

let output: string | undefined = undefined;

if (ext === 'json') {
output = outputJSON(coverage);
}

if (output) {
writeFileSync(absPath, output, {
encoding: 'utf-8',
});

renderer.success('Available at', absPath, '\n');
} else {
throw new Error(`Extension ${ext} is not supported`);
}
}
}

export async function coverage(
documentsPointer: string,
schemaPointer: string,
Expand All @@ -28,47 +78,21 @@ export async function coverage(
const renderer = options.renderer || new ConsoleRenderer();
const silent = options.silent === true;
const writePath = options.write;
const shouldWrite = typeof writePath !== 'undefined';

try {
const schema = await loadSchema(schemaPointer, {
headers: options.headers,
});
const documents = await loadDocuments(documentsPointer);
const coverage = calculateCoverage(schema, documents);

if (!silent) {
renderer.coverage(coverage);
}

if (shouldWrite) {
if (typeof writePath !== 'string' || !isValidPath(writePath)) {
throw new Error(`--write is not valid file path: ${writePath}`);
}

const absPath = ensureAbsolute(writePath);
const ext = extname(absPath)
.replace('.', '')
.toLocaleLowerCase();

let output: string | undefined = undefined;

if (ext === 'json') {
output = outputJSON(coverage);
}

if (output) {
writeFileSync(absPath, output, {
encoding: 'utf-8',
});

renderer.success('Available at', absPath, '\n');
} else {
throw new Error(`Extension ${ext} is not supported`);
}
}
await runCoverage({
schema,
documents,
renderer,
silent,
writePath,
});
} catch (e) {
console.log(e);
renderer.error(e);
process.exit(1);
}
Expand Down
67 changes: 40 additions & 27 deletions packages/cli/src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@graphql-inspector/core';
import {loadSchema} from '@graphql-inspector/load';
import {existsSync} from 'fs';
import {GraphQLSchema} from 'graphql';

import {renderChange, Renderer, ConsoleRenderer} from '../render';
import {ensureAbsolute} from '../utils/fs';
Expand All @@ -24,37 +25,22 @@ function resolveRule(name: string): Rule | undefined {
return DiffRule[name as keyof typeof DiffRule];
}

export async function diff(
oldSchemaPointer: string,
newSchemaPointer: string,
options: {
token?: string;
renderer?: Renderer;
require?: string[];
rule?: Array<string>;
headers?: Record<string, string>;
},
) {
const renderer = (options && options.renderer) || new ConsoleRenderer();

export async function runDiff(options: {
oldSchema: GraphQLSchema;
newSchema: GraphQLSchema;
renderer: Renderer;
rule?: Array<string>;
}) {
const {oldSchema, newSchema, renderer} = options;
try {
const oldSchema = await loadSchema(oldSchemaPointer, {
token: options.token,
headers: options.headers,
});
const newSchema = await loadSchema(newSchemaPointer, {
token: options.token,
headers: options.headers,
});
const rules = options.rule
? options.rule
.map(
(name): Rule => {
const rule = resolveRule(name);

if (!rule) {
renderer.error(`\Rule '${name}' does not exist!\n`);
return process.exit(1);
throw new Error(`\Rule '${name}' does not exist!\n`);
}

return rule;
Expand All @@ -66,6 +52,7 @@ export async function diff(

if (!changes.length) {
renderer.success('No changes detected');
return;
} else {
renderer.emit(
`\nDetected the following changes (${changes.length}) between schemas:\n`,
Expand All @@ -80,20 +67,46 @@ export async function diff(
c => c.criticality.level === CriticalityLevel.Breaking,
).length;

renderer.error(
throw new Error(
`Detected ${breakingCount} breaking change${
breakingCount > 1 ? 's' : ''
}\n`,
);
process.exit(1);
} else {
renderer.success('No breaking changes detected\n');
return;
}
}
} catch (e) {
throw e;
}
}

export async function diff(
oldSchemaPointer: string,
newSchemaPointer: string,
options: {
token?: string;
renderer?: Renderer;
require?: string[];
rule?: Array<string>;
headers?: Record<string, string>;
},
) {
const renderer = (options && options.renderer) || new ConsoleRenderer();

try {
const oldSchema = await loadSchema(oldSchemaPointer, {
token: options.token,
headers: options.headers,
});
const newSchema = await loadSchema(newSchemaPointer, {
token: options.token,
headers: options.headers,
});
await runDiff({oldSchema, newSchema, renderer, rule: options.rule});
} catch (e) {
renderer.error(e.message || e);
process.exit(1);
}

process.exit(0);
}
Loading

0 comments on commit d157075

Please sign in to comment.