Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement visual diff #1746

Closed
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
3 changes: 3 additions & 0 deletions packages/commands/diff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"@graphql-inspector/core": "0.0.0-PLACEHOLDER",
"@graphql-inspector/commands": "0.0.0-PLACEHOLDER",
"@graphql-inspector/logger": "0.0.0-PLACEHOLDER",
"express": "4.17.1",
"express-graphql": "0.11.0",
"graphql-tools": "6.2.4-alpha-34c1194a.0",

Choose a reason for hiding this comment

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

@kamilkisiela This line could be removed when this bug is fixed.

@ardatan cc.

Copy link
Owner

Choose a reason for hiding this comment

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

@FluorescentHallucinogen looks good but let's wait for the fix @ardatan

"tslib": "^2.0.0"
},
"scripts": {
Expand Down
54 changes: 50 additions & 4 deletions packages/commands/diff/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ import {
} from '@graphql-inspector/core';
import {existsSync} from 'fs';
import {GraphQLSchema} from 'graphql';
import {mergeSchemas} from '@graphql-tools/merge';
import express from 'express';
import {graphqlHTTP} from 'express-graphql';
import path from 'path';

export {CommandFactory};

export function handler(input: {
oldSchema: GraphQLSchema;
newSchema: GraphQLSchema;
onComplete?: string;
rules?: Array<string | number>;
visual?: boolean;
port?: number;
onComplete?: string;
}) {
const onComplete = input.onComplete
? resolveCompletionHandler(input.onComplete)
Expand Down Expand Up @@ -80,7 +86,28 @@ export function handler(input: {
reportNonBreakingChanges(nonBreakingChanges);
}

onComplete({breakingChanges, dangerousChanges, nonBreakingChanges});
onComplete(
{breakingChanges, dangerousChanges, nonBreakingChanges},
input.visual,
);

if (input.visual) {
const schema = mergeSchemas({
schemas: [input.oldSchema, input.newSchema],
onFieldTypeConflict: (_, right) => right,
});

const app = express();
app.set('json spaces', 2);
app.use('/graphql', graphqlHTTP({schema}));
app.get('/changes', (_, res) => res.status(200).json(changes));
app.use('/voyager', express.static(path.join(__dirname, 'static')));
app.listen(input.port);

Logger.log(`\nJSON Diff:\thttp://localhost:${input.port}/changes`);

Logger.log(`Visual Diff:\thttp://localhost:${input.port}/voyager`);
}
}

export default createCommand<
Expand All @@ -89,6 +116,8 @@ export default createCommand<
oldSchema: string;
newSchema: string;
rule?: Array<string | number>;
visual?: boolean;
port?: number;
onComplete?: string;
} & GlobalArgs
>((api) => {
Expand All @@ -114,6 +143,16 @@ export default createCommand<
describe: 'Add rules',
array: true,
},
visual: {
describe: 'Enable visual diff',
type: 'boolean',
},
port: {
alias: 'p',
describe: 'Port',
type: 'number',
default: 4000,
},
onComplete: {
describe: 'Handle Completion',
type: 'string',
Expand Down Expand Up @@ -148,6 +187,8 @@ export default createCommand<
oldSchema,
newSchema,
rules: args.rule,
visual: args.visual,
port: args.port,
onComplete: args.onComplete,
});
} catch (error) {
Expand Down Expand Up @@ -225,7 +266,10 @@ function resolveCompletionHandler(name: string): CompletionHandler | never {
return mod?.default || mod;
}

function failOnBreakingChanges({breakingChanges}: CompletionArgs) {
function failOnBreakingChanges(
{breakingChanges}: CompletionArgs,
proceed?: boolean,
) {
const breakingCount = breakingChanges.length;

if (breakingCount) {
Expand All @@ -234,7 +278,9 @@ function failOnBreakingChanges({breakingChanges}: CompletionArgs) {
breakingCount > 1 ? 's' : ''
}`,
);
process.exit(1);
if (!proceed) {
process.exit(1);
}
} else {
Logger.success('No breaking changes detected');
}
Expand Down
128 changes: 128 additions & 0 deletions packages/commands/diff/src/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!DOCTYPE html>

Choose a reason for hiding this comment

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

@kamilkisiela Need your help. How to include (copy) this file into build?

Copy link
Owner

Choose a reason for hiding this comment

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

We would have to adjust bob-the-bundler but I'll do it after merging this PR.

<html>
<head>
<meta
name="viewport"
content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui"
/>
<style>
body {
margin: 0;
}

#voyager {
height: 100vh;
}

.edge.red > path:not(.hover-path),
.edge.red > polygon {
stroke: #ff0000 !important;
}

.field.red > polygon {
fill: rgba(255, 0, 0, 0.55);
}

.edge.green > path:not(.hover-path),
.edge.green > polygon {
stroke: #00ff00 !important;
}

.field.green > polygon {
fill: rgba(0, 255, 0, 0.55);
}

.edge.yellow > path:not(.hover-path),
.edge.yellow > polygon {
stroke: #ffff00 !important;
}

.field.yellow > polygon {
fill: rgba(255, 255, 0, 0.55);
}
</style>
<link
rel="stylesheet"
href="https://unpkg.com/graphql-voyager/dist/voyager.css"
/>
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/graphql-voyager/dist/voyager.min.js"></script>
</head>
<body>
<main id="voyager">Loading...</main>
<script>
window.addEventListener('load', () => {
GraphQLVoyager.init(document.getElementById('voyager'), {
introspection: (introspectionQuery) =>
fetch(`/graphql?query=${introspectionQuery}`).then((res) =>
res.ok ? res.json() : res.text(),
),
});
setInterval(() => {
fetch('/changes')
.then(function (res) {
return res.json();
})
.then((changes) => {
const redElements = document.getElementsByClassName('red');
while (redElements.length) {
redElements[0].classList.remove('red');
}

const greenElements = document.getElementsByClassName('green');
while (greenElements.length) {
greenElements[0].classList.remove('green');
}

const yellowElements = document.getElementsByClassName('yellow');
while (yellowElements.length) {
yellowElements[0].classList.remove('yellow');
}

changes.forEach((change) => {
const selector = `[id="FIELD::${change.path.replace(
'.',
'::',
)}"]`;

let color;

switch (change.type) {
case 'FIELD_ADDED':
color = 'green';
break;
case 'FIELD_REMOVED':
color = 'red';
break;
case 'FIELD_TYPE_CHANGED':
color = 'yellow';
break;
}

const element = document.querySelector(selector);

if (element) {
element.classList.add(color);

const titleElements = element.getElementsByTagName('title');

while (titleElements.length) {
titleElements[0].remove();
}

const node = document.createElementNS(
'http://www.w3.org/2000/svg',
'title',
);
const textnode = document.createTextNode(change.message);
node.appendChild(textnode);
element.prepend(node);
}
});
});
}, 1000);
});
</script>
</body>
</html>