Skip to content

Commit

Permalink
feat(SchemaComposer): method toSDL() now by default sorts types by …
Browse files Browse the repository at this point in the history
…kinds (RootObjects, Scalars, Enums, Union, Interface, Object, Input) and after that in alphabetic order. This change of default sorting greatly improves the readability of SDL files for humans and keeps stable sort order for machines. If you need old behavior change it to `toSDL({ sortTypes: undefined })`.
  • Loading branch information
nodkz committed May 11, 2021
1 parent 8aa88fd commit bb6bb14
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 87 deletions.
7 changes: 6 additions & 1 deletion src/SchemaComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -981,14 +981,19 @@ export class SchemaComposer<TContext = any> extends TypeStorage<any, NamedTypeCo
* @param {Boolean} options.omitDirectiveDefinitions - do not add directives definitions to SDL
* @param {Boolean} options.commentDescriptions - print descriptions like comments, starting with #
* @param {Boolean} options.sortAll - sort fields, args, values, interfaces by its names. Useful for snapshot testing.
* @param {CompareTypeComposersOption} options.sortTypes - sort types. Available options: true, false, 'GROUP_BY_TYPE'
* @param {Boolean} options.sortFields - sort fields by name
* @param {Boolean} options.sortArgs - sort args by name
* @param {Boolean} options.sortInterfaces - sort interfaces by name
* @param {Boolean} options.sortUnions - sort union types by name
* @param {Boolean} options.sortEnums - sort enum values by name
*/
toSDL(options?: SchemaComposerPrinterOptions): string {
return printSchemaComposer(this, options);
const opts = {
sortTypes: 'GROUP_BY_TYPE',
...options,
} as SchemaComposerPrinterOptions;
return printSchemaComposer(this, opts);
}

/**
Expand Down
31 changes: 16 additions & 15 deletions src/__tests__/SchemaComposer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1523,36 +1523,35 @@ describe('SchemaComposer', () => {
sc.createTC(`type User { age: Int }`);

expect(sc.toSDL()).toBe(dedent`
type Query {
existedInQuery: String
}
type Mutation {
existedInMutation: String
}
type Query {
existedInQuery: String
type Subscription {
existedInSubscription: String
}
"""
The \`String\` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
"""
scalar String
type Subscription {
existedInSubscription: String
}
`);
});

describe('toSDL({ sortTypes })', () => {
it('should print types in alphabetic order by default', () => {
it('should print types in alphabetic order', () => {
const sc = new SchemaComposer();
sc.addTypeDefs(toSDL_sortTypeDefs);

const printed = sc.toSDL();
const printedUndefined = sc.toSDL({ sortTypes: undefined });
const printedTrue = sc.toSDL({ sortTypes: true });
const printedAlpha = sc.toSDL({ sortTypes: 'ALPHABETIC' });

expect(printed).toBe(dedent`
expect(printedUndefined).toBe(dedent`
interface Character {
id: ID!
name: String!
Expand Down Expand Up @@ -1640,18 +1639,18 @@ describe('SchemaComposer', () => {
"""
scalar String
`);
expect(printed).toBe(printedUndefined);
expect(printed).toBe(printedTrue);
expect(printed).toBe(printedAlpha);
expect(printedUndefined).toBe(printedTrue);
expect(printedUndefined).toBe(printedAlpha);
});

it('should allow to print types grouped by type', () => {
it('should allow to print types grouped by type by default', () => {
const sc = new SchemaComposer();
sc.addTypeDefs(toSDL_sortTypeDefs);

const printed = sc.toSDL({ sortTypes: 'GROUP_BY_TYPE' });
const printedByDefault = sc.toSDL();
const printedGrouped = sc.toSDL({ sortTypes: 'GROUP_BY_TYPE' });

expect(printed).toBe(dedent`
expect(printedGrouped).toBe(dedent`
type Query {
heroes(episode: Episode): [SearchResult]
droid(id: ID!): Droid
Expand Down Expand Up @@ -1739,6 +1738,8 @@ describe('SchemaComposer', () => {
commentary: String
}
`);

expect(printedGrouped).toBe(printedByDefault);
});

it('should allow to print types with custom sorting function', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/github_issues/238-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ describe('github issue #238: `setTypeName` doesnt update the usage of the type',
scalar String
union T_U = T_A | T_B
type T_A {
a: String
}
type T_B {
b: String
}
union T_U = T_A | T_B
`);
});

Expand Down
56 changes: 28 additions & 28 deletions src/__tests__/github_issues/246-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ describe('github issue #246: Directives are removed from schema in SchemaCompose
.toMatchInlineSnapshot(`
"directive @test(reason: String = \\"No longer supported\\") on FIELD_DEFINITION | ENUM_VALUE | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
type Query {
hello(a: ModifyMeInput): ModifyMe
}
scalar ID
enum Status {
OK @test(reason: \\"enum\\")
}
type ModifyMe {
id: ID! @test(reason: \\"asjdk\\")
field(arg: ID! @test(reason: \\"123\\")): String
Expand All @@ -42,14 +50,6 @@ describe('github issue #246: Directives are removed from schema in SchemaCompose
input ModifyMeInput {
id: ID! @test(reason: \\"input\\")
}
type Query {
hello(a: ModifyMeInput): ModifyMe
}
enum Status {
OK @test(reason: \\"enum\\")
}"
`);
});
Expand All @@ -59,32 +59,32 @@ describe('github issue #246: Directives are removed from schema in SchemaCompose
if (graphqlVersion >= 15.1) {
expect(schemaComposer.toSDL({ omitDescriptions: true, exclude: ['String'] }))
.toMatchInlineSnapshot(`
"directive @test(reason: String = \\"No longer supported\\") on FIELD_DEFINITION | ENUM_VALUE | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
"directive @test(reason: String = \\"No longer supported\\") on FIELD_DEFINITION | ENUM_VALUE | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
directive @specifiedBy(
url: String!
) on SCALAR
directive @specifiedBy(
url: String!
) on SCALAR
scalar ID
type Query {
hello(a: ModifyMeInput): ModifyMe
}
type ModifyMe {
id: ID! @test(reason: \\"asjdk\\")
field(arg: ID! @test(reason: \\"123\\")): String
status: Status
}
scalar ID
input ModifyMeInput {
id: ID! @test(reason: \\"input\\")
}
enum Status {
OK @test(reason: \\"enum\\")
}
type Query {
hello(a: ModifyMeInput): ModifyMe
}
type ModifyMe {
id: ID! @test(reason: \\"asjdk\\")
field(arg: ID! @test(reason: \\"123\\")): String
status: Status
}
enum Status {
OK @test(reason: \\"enum\\")
}"
`);
input ModifyMeInput {
id: ID! @test(reason: \\"input\\")
}"
`);
}
});
});
20 changes: 10 additions & 10 deletions src/__tests__/github_issues/262-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ describe('github issue #262: SchemaComposer fails to map enum values in field di
url: String!
) on SCALAR
type Note {
id: ID!
title: String! @auth(permissions: [CREATE, READ])
}
enum CrudPermissions {
CREATE
READ
UPDATE
DELETE
}
type Note {
id: ID!
title: String! @auth(permissions: [CREATE, READ])
}"
`);
});
Expand Down Expand Up @@ -92,16 +92,16 @@ describe('github issue #262: SchemaComposer fails to map enum values in field di
url: String!
) on SCALAR
type Note {
id: ID!
title: String! @auth(permissions: [CREATE, READ])
}
enum CrudPermissions {
CREATE
READ
UPDATE
DELETE
}
type Note {
id: ID!
title: String! @auth(permissions: [CREATE, READ])
}"
`);

Expand Down
16 changes: 8 additions & 8 deletions src/__tests__/github_issues/273-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ describe('github issue #273: Object directives are removed from schema', () => {
url: String!
) on SCALAR
enum Enum @test
input Input @test {
id: ID!
type Query {
hello(a: Input, s: Scalar, e: Enum): ModifyMe
}
scalar Scalar @test
enum Enum @test
type ModifyMe @test {
id: ID!
}
type Query {
hello(a: Input, s: Scalar, e: Enum): ModifyMe
input Input @test {
id: ID!
}
scalar Scalar @test
`);
});
});
18 changes: 9 additions & 9 deletions src/__tests__/github_issues/287-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ describe('github issue #287: Can we merge schemas with overriding types in field
sc.merge(schemaB);

expect(sc.toSDL({ omitScalars: true, omitDirectiveDefinitions: true })).toEqual(dedent`
type Mutation
type Query {
"""BBB"""
field1: String
Expand All @@ -36,6 +34,8 @@ describe('github issue #287: Can we merge schemas with overriding types in field
field22: Int
}
type Mutation
type Subscription
`);

Expand Down Expand Up @@ -95,8 +95,15 @@ describe('github issue #287: Can we merge schemas with overriding types in field
sc.merge(schemaB);

expect(sc.toSDL({ omitScalars: true, omitDirectiveDefinitions: true })).toEqual(dedent`
type Query {
post: Post
node(id: ID!): Node
}
type Mutation
type Subscription
interface Node {
id: ID!
}
Expand All @@ -107,13 +114,6 @@ describe('github issue #287: Can we merge schemas with overriding types in field
fieldA: Int
fieldB: String
}
type Query {
post: Post
node(id: ID!): Node
}
type Subscription
`);
});

Expand Down

0 comments on commit bb6bb14

Please sign in to comment.