Skip to content

Commit 62a1093

Browse files
committed
✨ Add header options to CLI
1 parent c98cb1a commit 62a1093

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ $ graphql-schema-diff --help
2828
--ignore-breaking-changes Do not exit with error on breaking changes
2929
--create-html-output Creates an HTML file containing the diff
3030
--html-output-directory Directory where the HTML file should be stored (Default: './schemaDiff')
31+
--header, -H Header to send to all remote schema sources
32+
--left-schema-header Header to send to left remote schema source
33+
--right-schema-header Header to send to right remote schema source
3134

3235
Examples
3336
$ graphql-schema-diff https://example.com/graphql schema.graphql
37+
$ graphql-schema-diff https://example.com/graphql schema.graphql -H 'Authorization: Bearer 123'
3438

3539
```
3640

src/cli.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import meow from 'meow';
44
import chalk from 'chalk';
55
import { createHtmlOutput } from './html';
6-
import { getDiff } from '.';
6+
import { getDiff, Headers } from './diff';
77

88
const cli = meow(
99
`
@@ -15,9 +15,13 @@ const cli = meow(
1515
--ignore-breaking-changes Do not exit with error on breaking changes
1616
--create-html-output Creates an HTML file containing the diff
1717
--html-output-directory Directory where the HTML file should be stored (Default: './schemaDiff')
18+
--header, -H Header to send to all remote schema sources
19+
--left-schema-header Header to send to left remote schema source
20+
--right-schema-header Header to send to right remote schema source
1821
1922
Examples
2023
$ graphql-schema-diff https://example.com/graphql schema.graphql
24+
$ graphql-schema-diff https://example.com/graphql schema.graphql -H 'Authorization: Bearer 123'
2125
`,
2226
{
2327
flags: {
@@ -33,18 +37,63 @@ const cli = meow(
3337
'html-output-directory': {
3438
type: 'string',
3539
default: 'schemaDiff'
40+
},
41+
header: {
42+
type: 'string',
43+
alias: 'H'
44+
},
45+
'left-schema-header': {
46+
type: 'string'
47+
},
48+
'right-schema-header': {
49+
type: 'string'
3650
}
3751
}
3852
}
3953
);
4054

41-
const [leftSchemaLocation, rightSchemaLocation] = cli.input;
55+
function parseHeaders(headerInput?: string | string[]): Headers | undefined {
56+
let headers: string[][];
57+
const parseHeader = (header: string): string[] =>
58+
header.split(':').map((val: string) => val.trim());
59+
60+
if (!headerInput) return;
61+
62+
if (Array.isArray(headerInput)) {
63+
headers = headerInput.map(parseHeader);
64+
} else {
65+
headers = [parseHeader(headerInput)];
66+
}
67+
68+
return headers
69+
.filter(([key]) => key != null && key !== '')
70+
.reduce((result, [key, value]) => ({ ...result, [key && key]: value }), {});
71+
}
72+
73+
const [leftSchemaLocation, rightSchemaLocation]: string[] = cli.input;
74+
const {
75+
header,
76+
leftSchemaHeader,
77+
rightSchemaHeader
78+
}: {
79+
header?: string | string[];
80+
leftSchemaHeader?: string | string[];
81+
rightSchemaHeader?: string | string[];
82+
} = cli.flags;
4283

4384
if (!leftSchemaLocation || !rightSchemaLocation) {
4485
throw new Error('Schema locations missing!');
4586
}
4687

47-
getDiff(leftSchemaLocation, rightSchemaLocation)
88+
getDiff(leftSchemaLocation, rightSchemaLocation, {
89+
headers: parseHeaders(header),
90+
leftSchema: {
91+
headers: parseHeaders(leftSchemaHeader)
92+
},
93+
rightSchema: {
94+
headers: parseHeaders(rightSchemaHeader)
95+
}
96+
})
4897
.then(async result => {
4998
if (result === undefined) {
5099
console.log(chalk.green('✔ No changes'));

src/diff.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import fetch from 'node-fetch';
1616
import disparity from 'disparity';
1717
import { fileLoader, mergeTypes } from 'merge-graphql-schemas';
1818

19-
interface Headers {
19+
export interface Headers {
2020
[key: string]: string;
2121
}
2222

@@ -111,7 +111,10 @@ export async function getDiff(
111111
throw new Error('Schemas not defined');
112112
}
113113

114-
const [leftSchemaSDL, rightSchemaSDL] = [printSchema(leftSchema), printSchema(rightSchema)];
114+
const [leftSchemaSDL, rightSchemaSDL] = [
115+
printSchema(leftSchema),
116+
printSchema(rightSchema)
117+
];
115118

116119
if (leftSchemaSDL === rightSchemaSDL) {
117120
return;

0 commit comments

Comments
 (0)