Skip to content

Commit 27881f6

Browse files
committed
fix: put schema at the top (fixes #6)
1 parent 6757068 commit 27881f6

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/utilities/formatSdl.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ const sortSchema = (key, value, options: OptionsType) => {
2323
return [
2424
key,
2525
value.slice().sort((a, b) => {
26+
if (a.kind === 'SchemaDefinition') {
27+
return -1;
28+
}
29+
30+
if (b.kind === 'SchemaDefinition') {
31+
return 1;
32+
}
33+
2634
return a.name.value.localeCompare(b.name.value);
2735
}),
2836
];

test/format-graphql/utilities/formatSdl.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@ type Foo {
2626
t.is(formatSdl(input), expectedOutput);
2727
});
2828

29+
test('puts schema at the top)', (t) => {
30+
const input = `
31+
type Foo {
32+
id: ID!
33+
}
34+
35+
schema {
36+
query: Foo
37+
}
38+
`;
39+
40+
const expectedOutput = `schema {
41+
query: Foo
42+
}
43+
44+
type Foo {
45+
id: ID!
46+
}
47+
`;
48+
49+
t.is(formatSdl(input), expectedOutput);
50+
});
51+
2952
test('sorts fields', (t) => {
3053
const input = `
3154
type Foo {

0 commit comments

Comments
 (0)