Skip to content

Commit 107be30

Browse files
committed
schemaPrinter: preserve order of types
Fixes graphql#2362 It's a step toward having reversable `buildSchema`: ``` sdl === printSchema(buildSchema(sdl)) ``` At the same time if you need fully predictable SDL output (e.g. to diff schemas) you can achieve this using `llexicographicSortSchema`: ``` printSchema(lexicographicSortSchema(schema)) ``` But if some reason you can't use `lexicographicSortSchema` please open an issue and describe your use case in more details.
1 parent 68a0818 commit 107be30

File tree

5 files changed

+228
-231
lines changed

5 files changed

+228
-231
lines changed

src/type/__tests__/schema-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ describe('Type System: Schema', () => {
9292
});
9393

9494
expect(printSchema(schema)).to.equal(dedent`
95+
type Query {
96+
article(id: String): Article
97+
feed: [Article]
98+
}
99+
95100
type Article {
96101
id: String
97102
isPublished: Boolean
@@ -117,11 +122,6 @@ describe('Type System: Schema', () => {
117122
writeArticle: Article
118123
}
119124
120-
type Query {
121-
article(id: String): Article
122-
feed: [Article]
123-
}
124-
125125
type Subscription {
126126
articleSubscribe(id: String): Article
127127
}

src/utilities/__tests__/extendSchema-test.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -666,15 +666,15 @@ describe('extendSchema', () => {
666666

667667
expect(validateSchema(extendedSchema)).to.deep.equal([]);
668668
expect(printSchemaChanges(schema, extendedSchema)).to.equal(dedent`
669+
type SomeObject {
670+
newField(arg1: String, arg2: NewInputObj!): String
671+
}
672+
669673
input NewInputObj {
670674
field1: Int
671675
field2: [Float]
672676
field3: String!
673677
}
674-
675-
type SomeObject {
676-
newField(arg1: String, arg2: NewInputObj!): String
677-
}
678678
`);
679679
});
680680

@@ -754,8 +754,7 @@ describe('extendSchema', () => {
754754
755755
scalar NewScalar
756756
757-
union NewUnion = NewObject
758-
`;
757+
union NewUnion = NewObject`;
759758
const extendAST = parse(`
760759
${newTypesSDL}
761760
extend type SomeObject {
@@ -771,7 +770,6 @@ describe('extendSchema', () => {
771770

772771
expect(validateSchema(extendedSchema)).to.deep.equal([]);
773772
expect(printSchemaChanges(schema, extendedSchema)).to.equal(dedent`
774-
${newTypesSDL}
775773
type SomeObject {
776774
oldField: String
777775
newObject: NewObject
@@ -781,6 +779,8 @@ describe('extendSchema', () => {
781779
newEnum: NewEnum
782780
newTree: [SomeObject]!
783781
}
782+
783+
${newTypesSDL}
784784
`);
785785
});
786786

@@ -811,12 +811,12 @@ describe('extendSchema', () => {
811811

812812
expect(validateSchema(extendedSchema)).to.deep.equal([]);
813813
expect(printSchemaChanges(schema, extendedSchema)).to.equal(dedent`
814-
interface NewInterface {
814+
type SomeObject implements OldInterface & NewInterface {
815+
oldField: String
815816
newField: String
816817
}
817818
818-
type SomeObject implements OldInterface & NewInterface {
819-
oldField: String
819+
interface NewInterface {
820820
newField: String
821821
}
822822
`);
@@ -864,8 +864,7 @@ describe('extendSchema', () => {
864864
865865
type NewObject {
866866
foo: String
867-
}
868-
`;
867+
}`;
869868
const extendAST = parse(`
870869
${newTypesSDL}
871870
extend type SomeObject implements NewInterface {
@@ -900,26 +899,27 @@ describe('extendSchema', () => {
900899

901900
expect(validateSchema(extendedSchema)).to.deep.equal([]);
902901
expect(printSchemaChanges(schema, extendedSchema)).to.equal(dedent`
903-
${newTypesSDL}
902+
type SomeObject implements SomeInterface & NewInterface & AnotherNewInterface {
903+
oldField: String
904+
newField: String
905+
anotherNewField: String
906+
}
907+
904908
enum SomeEnum {
905909
OLD_VALUE
906910
NEW_VALUE
907911
ANOTHER_NEW_VALUE
908912
}
909913
910-
input SomeInput {
911-
oldField: String
912-
newField: String
913-
anotherNewField: String
914-
}
914+
union SomeUnion = SomeObject | NewObject | AnotherNewObject
915915
916-
type SomeObject implements SomeInterface & NewInterface & AnotherNewInterface {
916+
input SomeInput {
917917
oldField: String
918918
newField: String
919919
anotherNewField: String
920920
}
921921
922-
union SomeUnion = SomeObject | NewObject | AnotherNewObject
922+
${newTypesSDL}
923923
`);
924924
});
925925

@@ -958,12 +958,12 @@ describe('extendSchema', () => {
958958

959959
expect(validateSchema(extendedSchema)).to.deep.equal([]);
960960
expect(printSchemaChanges(schema, extendedSchema)).to.equal(dedent`
961-
interface AnotherInterface implements SomeInterface {
961+
interface SomeInterface {
962962
oldField: String
963963
newField: String
964964
}
965965
966-
interface SomeInterface {
966+
interface AnotherInterface implements SomeInterface {
967967
oldField: String
968968
newField: String
969969
}
@@ -1015,12 +1015,12 @@ describe('extendSchema', () => {
10151015
newField: String
10161016
}
10171017
1018-
interface NewInterface {
1018+
type SomeObject implements SomeInterface & AnotherInterface & NewInterface {
1019+
oldField: String
10191020
newField: String
10201021
}
10211022
1022-
type SomeObject implements SomeInterface & AnotherInterface & NewInterface {
1023-
oldField: String
1023+
interface NewInterface {
10241024
newField: String
10251025
}
10261026
`);
@@ -1120,16 +1120,16 @@ describe('extendSchema', () => {
11201120
expect(extendedSchema).to.not.equal(mutationSchema);
11211121
expect(printSchema(mutationSchema)).to.equal(originalPrint);
11221122
expect(printSchema(extendedSchema)).to.equal(dedent`
1123-
type Mutation {
1124-
mutationField: String
1125-
newMutationField: Int
1126-
}
1127-
11281123
type Query {
11291124
queryField: String
11301125
newQueryField: Int
11311126
}
11321127
1128+
type Mutation {
1129+
mutationField: String
1130+
newMutationField: Int
1131+
}
1132+
11331133
type Subscription {
11341134
subscriptionField: String
11351135
newSubscriptionField: Int

0 commit comments

Comments
 (0)