Skip to content

Commit 8048bcf

Browse files
gdw2fabsrc
authored andcommitted
Add support for sorting schemas (#3)
* support for sorting schemas * using lexicographicSortSchema
1 parent 60765c0 commit 8048bcf

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

src/__tests__/diff.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,25 @@ describe('getDiff', () => {
221221
}
222222
});
223223
});
224+
225+
describe('schema sorting', () => {
226+
it('returns diff between two unsorted, but otherwise equal schemas, when sorting not enabled', async () => {
227+
const result = await getDiff(
228+
path.join(__dirname, 'fixtures/localSchemaSorted.graphql'),
229+
path.join(__dirname, 'fixtures/localSchemaUnsorted.graphql')
230+
);
231+
232+
expect(result).toBeDefined();
233+
});
234+
235+
it('returns nothing between two unsorted, but otherwise equal schemas, when sorting enabled', async () => {
236+
const result = await getDiff(
237+
path.join(__dirname, 'fixtures/localSchemaSorted.graphql'),
238+
path.join(__dirname, 'fixtures/localSchemaUnsorted.graphql'),
239+
{ sortSchema: true }
240+
);
241+
242+
expect(result).toBeUndefined();
243+
});
244+
});
224245
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type Query {
2+
test1: String
3+
test2: String
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type Query {
2+
test2: String
3+
test1: String
4+
}

src/cli.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const cli = meow(
1818
--header, -H Header to send to all remote schema sources
1919
--left-schema-header Header to send to left remote schema source
2020
--right-schema-header Header to send to right remote schema source
21+
--sort-schema, -s Sort schemas prior to diffing
2122
2223
Examples
2324
$ graphql-schema-diff https://example.com/graphql schema.graphql
@@ -47,6 +48,10 @@ const cli = meow(
4748
},
4849
'right-schema-header': {
4950
type: 'string'
51+
},
52+
'sort-schema': {
53+
type: 'boolean',
54+
alias: 's'
5055
}
5156
}
5257
}
@@ -96,7 +101,8 @@ getDiff(leftSchemaLocation, rightSchemaLocation, {
96101
},
97102
rightSchema: {
98103
headers: parseHeaders(rightSchemaHeader)
99-
}
104+
},
105+
sortSchema: cli.flags.sortSchema
100106
})
101107
.then(async result => {
102108
if (result === undefined) {

src/diff.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
introspectionFromSchema,
1111
getIntrospectionQuery
1212
} from 'graphql';
13+
import { lexicographicSortSchema } from 'graphql/utilities';
1314
import fs from 'fs';
1415
import isGlob from 'is-glob';
1516
import fetch from 'node-fetch';
@@ -63,7 +64,9 @@ function readLocalSchema(schemaPath: string): GraphQLSchema {
6364
}
6465

6566
const schema = buildSchema(schemaString);
66-
const introspection = introspectionFromSchema(schema, { descriptions: false })
67+
const introspection = introspectionFromSchema(schema, {
68+
descriptions: false
69+
});
6770
return buildClientSchema(introspection);
6871
}
6972

@@ -93,6 +96,7 @@ export interface DiffOptions {
9396
headers?: Headers;
9497
};
9598
headers?: Headers;
99+
sortSchema?: boolean;
96100
}
97101

98102
export async function getDiff(
@@ -112,7 +116,7 @@ export async function getDiff(
112116
...(options.rightSchema && options.rightSchema.headers)
113117
}
114118
};
115-
const [leftSchema, rightSchema] = await Promise.all([
119+
let [leftSchema, rightSchema] = await Promise.all([
116120
getSchema(leftSchemaLocation, leftSchemaOptions),
117121
getSchema(rightSchemaLocation, rightSchemaOptions)
118122
]);
@@ -121,6 +125,13 @@ export async function getDiff(
121125
throw new Error('Schemas not defined');
122126
}
123127

128+
if (options.sortSchema) {
129+
[leftSchema, rightSchema] = [
130+
lexicographicSortSchema(leftSchema),
131+
lexicographicSortSchema(rightSchema)
132+
];
133+
}
134+
124135
const [leftSchemaSDL, rightSchemaSDL] = [
125136
printSchema(leftSchema),
126137
printSchema(rightSchema)

0 commit comments

Comments
 (0)