Skip to content

Commit

Permalink
schemaPrinter: preserve order of types
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
IvanGoncharov committed Jan 29, 2020
1 parent 68a0818 commit 107be30
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 231 deletions.
10 changes: 5 additions & 5 deletions src/type/__tests__/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ describe('Type System: Schema', () => {
});

expect(printSchema(schema)).to.equal(dedent`
type Query {
article(id: String): Article
feed: [Article]
}
type Article {
id: String
isPublished: Boolean
Expand All @@ -117,11 +122,6 @@ describe('Type System: Schema', () => {
writeArticle: Article
}
type Query {
article(id: String): Article
feed: [Article]
}
type Subscription {
articleSubscribe(id: String): Article
}
Expand Down
60 changes: 30 additions & 30 deletions src/utilities/__tests__/extendSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,15 +666,15 @@ describe('extendSchema', () => {

expect(validateSchema(extendedSchema)).to.deep.equal([]);
expect(printSchemaChanges(schema, extendedSchema)).to.equal(dedent`
type SomeObject {
newField(arg1: String, arg2: NewInputObj!): String
}
input NewInputObj {
field1: Int
field2: [Float]
field3: String!
}
type SomeObject {
newField(arg1: String, arg2: NewInputObj!): String
}
`);
});

Expand Down Expand Up @@ -754,8 +754,7 @@ describe('extendSchema', () => {
scalar NewScalar
union NewUnion = NewObject
`;
union NewUnion = NewObject`;
const extendAST = parse(`
${newTypesSDL}
extend type SomeObject {
Expand All @@ -771,7 +770,6 @@ describe('extendSchema', () => {

expect(validateSchema(extendedSchema)).to.deep.equal([]);
expect(printSchemaChanges(schema, extendedSchema)).to.equal(dedent`
${newTypesSDL}
type SomeObject {
oldField: String
newObject: NewObject
Expand All @@ -781,6 +779,8 @@ describe('extendSchema', () => {
newEnum: NewEnum
newTree: [SomeObject]!
}
${newTypesSDL}
`);
});

Expand Down Expand Up @@ -811,12 +811,12 @@ describe('extendSchema', () => {

expect(validateSchema(extendedSchema)).to.deep.equal([]);
expect(printSchemaChanges(schema, extendedSchema)).to.equal(dedent`
interface NewInterface {
type SomeObject implements OldInterface & NewInterface {
oldField: String
newField: String
}
type SomeObject implements OldInterface & NewInterface {
oldField: String
interface NewInterface {
newField: String
}
`);
Expand Down Expand Up @@ -864,8 +864,7 @@ describe('extendSchema', () => {
type NewObject {
foo: String
}
`;
}`;
const extendAST = parse(`
${newTypesSDL}
extend type SomeObject implements NewInterface {
Expand Down Expand Up @@ -900,26 +899,27 @@ describe('extendSchema', () => {

expect(validateSchema(extendedSchema)).to.deep.equal([]);
expect(printSchemaChanges(schema, extendedSchema)).to.equal(dedent`
${newTypesSDL}
type SomeObject implements SomeInterface & NewInterface & AnotherNewInterface {
oldField: String
newField: String
anotherNewField: String
}
enum SomeEnum {
OLD_VALUE
NEW_VALUE
ANOTHER_NEW_VALUE
}
input SomeInput {
oldField: String
newField: String
anotherNewField: String
}
union SomeUnion = SomeObject | NewObject | AnotherNewObject
type SomeObject implements SomeInterface & NewInterface & AnotherNewInterface {
input SomeInput {
oldField: String
newField: String
anotherNewField: String
}
union SomeUnion = SomeObject | NewObject | AnotherNewObject
${newTypesSDL}
`);
});

Expand Down Expand Up @@ -958,12 +958,12 @@ describe('extendSchema', () => {

expect(validateSchema(extendedSchema)).to.deep.equal([]);
expect(printSchemaChanges(schema, extendedSchema)).to.equal(dedent`
interface AnotherInterface implements SomeInterface {
interface SomeInterface {
oldField: String
newField: String
}
interface SomeInterface {
interface AnotherInterface implements SomeInterface {
oldField: String
newField: String
}
Expand Down Expand Up @@ -1015,12 +1015,12 @@ describe('extendSchema', () => {
newField: String
}
interface NewInterface {
type SomeObject implements SomeInterface & AnotherInterface & NewInterface {
oldField: String
newField: String
}
type SomeObject implements SomeInterface & AnotherInterface & NewInterface {
oldField: String
interface NewInterface {
newField: String
}
`);
Expand Down Expand Up @@ -1120,16 +1120,16 @@ describe('extendSchema', () => {
expect(extendedSchema).to.not.equal(mutationSchema);
expect(printSchema(mutationSchema)).to.equal(originalPrint);
expect(printSchema(extendedSchema)).to.equal(dedent`
type Mutation {
mutationField: String
newMutationField: Int
}
type Query {
queryField: String
newQueryField: Int
}
type Mutation {
mutationField: String
newMutationField: Int
}
type Subscription {
subscriptionField: String
newSubscriptionField: Int
Expand Down
Loading

0 comments on commit 107be30

Please sign in to comment.