Skip to content

support for sorting schemas #3

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

Merged
merged 2 commits into from
Jun 8, 2019
Merged
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
21 changes: 21 additions & 0 deletions src/__tests__/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,25 @@ describe('getDiff', () => {
}
});
});

describe('schema sorting', () => {
it('returns diff between two unsorted, but otherwise equal schemas, when sorting not enabled', async () => {
const result = await getDiff(
path.join(__dirname, 'fixtures/localSchemaSorted.graphql'),
path.join(__dirname, 'fixtures/localSchemaUnsorted.graphql')
);

expect(result).toBeDefined();
});

it('returns nothing between two unsorted, but otherwise equal schemas, when sorting enabled', async () => {
const result = await getDiff(
path.join(__dirname, 'fixtures/localSchemaSorted.graphql'),
path.join(__dirname, 'fixtures/localSchemaUnsorted.graphql'),
{ sortSchema: true }
);

expect(result).toBeUndefined();
});
});
});
4 changes: 4 additions & 0 deletions src/__tests__/fixtures/localSchemaSorted.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Query {
test1: String
test2: String
}
4 changes: 4 additions & 0 deletions src/__tests__/fixtures/localSchemaUnsorted.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Query {
test2: String
test1: String
}
8 changes: 7 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const cli = meow(
--header, -H Header to send to all remote schema sources
--left-schema-header Header to send to left remote schema source
--right-schema-header Header to send to right remote schema source
--sort-schema, -s Sort schemas prior to diffing

Examples
$ graphql-schema-diff https://example.com/graphql schema.graphql
Expand Down Expand Up @@ -47,6 +48,10 @@ const cli = meow(
},
'right-schema-header': {
type: 'string'
},
'sort-schema': {
type: 'boolean',
alias: 's'
}
}
}
Expand Down Expand Up @@ -96,7 +101,8 @@ getDiff(leftSchemaLocation, rightSchemaLocation, {
},
rightSchema: {
headers: parseHeaders(rightSchemaHeader)
}
},
sortSchema: cli.flags.sortSchema
})
.then(async result => {
if (result === undefined) {
Expand Down
15 changes: 13 additions & 2 deletions src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
introspectionFromSchema,
getIntrospectionQuery
} from 'graphql';
import { lexicographicSortSchema } from 'graphql/utilities';
import fs from 'fs';
import isGlob from 'is-glob';
import fetch from 'node-fetch';
Expand Down Expand Up @@ -63,7 +64,9 @@ function readLocalSchema(schemaPath: string): GraphQLSchema {
}

const schema = buildSchema(schemaString);
const introspection = introspectionFromSchema(schema, { descriptions: false })
const introspection = introspectionFromSchema(schema, {
descriptions: false
});
return buildClientSchema(introspection);
}

Expand Down Expand Up @@ -93,6 +96,7 @@ export interface DiffOptions {
headers?: Headers;
};
headers?: Headers;
sortSchema?: boolean;
}

export async function getDiff(
Expand All @@ -112,7 +116,7 @@ export async function getDiff(
...(options.rightSchema && options.rightSchema.headers)
}
};
const [leftSchema, rightSchema] = await Promise.all([
let [leftSchema, rightSchema] = await Promise.all([
getSchema(leftSchemaLocation, leftSchemaOptions),
getSchema(rightSchemaLocation, rightSchemaOptions)
]);
Expand All @@ -121,6 +125,13 @@ export async function getDiff(
throw new Error('Schemas not defined');
}

if (options.sortSchema) {
[leftSchema, rightSchema] = [
lexicographicSortSchema(leftSchema),
lexicographicSortSchema(rightSchema)
];
}

const [leftSchemaSDL, rightSchemaSDL] = [
printSchema(leftSchema),
printSchema(rightSchema)
Expand Down