Skip to content

Commit 0b7d55c

Browse files
committed
SchemaPrinter: sort fields before printing to get more stable diff
1 parent 9b9a74c commit 0b7d55c

File tree

3 files changed

+50
-44
lines changed

3 files changed

+50
-44
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use GraphQL\Language\Printer;
55
use GraphQL\Schema;
6+
use GraphQL\Type\Definition\CompositeType;
67
use GraphQL\Type\Definition\EnumType;
78
use GraphQL\Type\Definition\InputObjectType;
89
use GraphQL\Type\Definition\InterfaceType;
@@ -203,7 +204,7 @@ private static function printEnumValues($values)
203204

204205
private static function printInputObject(InputObjectType $type)
205206
{
206-
$fields = array_values($type->getFields());
207+
$fields = self::sortFields($type->getFields());
207208
return self::printDescription($type) .
208209
"input {$type->name} {\n" .
209210
implode("\n", array_map(function($f, $i) {
@@ -214,7 +215,7 @@ private static function printInputObject(InputObjectType $type)
214215

215216
private static function printFields($type)
216217
{
217-
$fields = array_values($type->getFields());
218+
$fields = self::sortFields($type->getFields());
218219
return implode("\n", array_map(function($f, $i) {
219220
return self::printDescription($f, ' ', !$i) . ' ' .
220221
$f->name . self::printArgs($f->args, ' ') . ': ' .
@@ -301,4 +302,10 @@ private static function breakLine($line, $len)
301302
return trim($part);
302303
}, $parts);
303304
}
305+
306+
private static function sortFields(array $fields)
307+
{
308+
ksort($fields);
309+
return array_values($fields);
310+
}
304311
}

tests/Utils/BuildSchemaTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public function testSimpleType()
7171
}
7272
7373
type HelloScalars {
74-
str: String
75-
int: Int
74+
bool: Boolean
7675
float: Float
7776
id: ID
78-
bool: Boolean
77+
int: Int
78+
str: String
7979
}
8080
';
8181
$output = $this->cycleOutput($body);
@@ -194,11 +194,11 @@ public function testTypeModifiers()
194194
}
195195
196196
type HelloScalars {
197-
nonNullStr: String!
198-
listOfStrs: [String]
199197
listOfNonNullStrs: [String!]
200-
nonNullListOfStrs: [String]!
198+
listOfStrs: [String]
201199
nonNullListOfNonNullStrs: [String!]!
200+
nonNullListOfStrs: [String]!
201+
nonNullStr: String!
202202
}
203203
';
204204
$output = $this->cycleOutput($body);
@@ -216,8 +216,8 @@ public function testRecursiveType()
216216
}
217217
218218
type Recurse {
219-
str: String
220219
recurse: Recurse
220+
str: String
221221
}
222222
';
223223
$output = $this->cycleOutput($body);
@@ -259,10 +259,10 @@ public function testSingleArgumentField()
259259
}
260260
261261
type Hello {
262-
str(int: Int): String
262+
booleanToStr(bool: Boolean): String
263263
floatToStr(float: Float): String
264264
idToStr(id: ID): String
265-
booleanToStr(bool: Boolean): String
265+
str(int: Int): String
266266
strToStr(bool: String): String
267267
}
268268
';
@@ -479,9 +479,9 @@ public function testSimpleTypeWithMutation()
479479
}
480480
481481
type HelloScalars {
482-
str: String
483-
int: Int
484482
bool: Boolean
483+
int: Int
484+
str: String
485485
}
486486
487487
type Mutation {
@@ -504,9 +504,9 @@ public function testSimpleTypeWithSubscription()
504504
}
505505
506506
type HelloScalars {
507-
str: String
508-
int: Int
509507
bool: Boolean
508+
int: Int
509+
str: String
510510
}
511511
512512
type Subscription {
@@ -572,9 +572,9 @@ enum MyEnum {
572572
}
573573
574574
type Query {
575+
enum: MyEnum
575576
field1: String @deprecated
576577
field2: Int @deprecated(reason: "Because I said so")
577-
enum: MyEnum
578578
}
579579
';
580580
$output = $this->cycleOutput($body);

tests/Utils/SchemaPrinterTest.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ interface Baaz {
445445
}
446446
447447
type Bar implements Foo, Baaz {
448-
str: String
449448
int: Int
449+
str: String
450450
}
451451
452452
interface Foo {
@@ -512,8 +512,8 @@ public function testPrintUnions()
512512
union MultipleUnion = Foo | Bar
513513
514514
type Root {
515-
single: SingleUnion
516515
multiple: MultipleUnion
516+
single: SingleUnion
517517
}
518518
519519
union SingleUnion = Foo
@@ -677,13 +677,13 @@ public function testPrintIntrospectionSchema()
677677
# skipping a field. Directives provide this by describing additional information
678678
# to the executor.
679679
type __Directive {
680-
name: String!
680+
args: [__InputValue!]!
681681
description: String
682682
locations: [__DirectiveLocation!]!
683-
args: [__InputValue!]!
684-
onOperation: Boolean! @deprecated(reason: "Use `locations`.")
685-
onFragment: Boolean! @deprecated(reason: "Use `locations`.")
683+
name: String!
686684
onField: Boolean! @deprecated(reason: "Use `locations`.")
685+
onFragment: Boolean! @deprecated(reason: "Use `locations`.")
686+
onOperation: Boolean! @deprecated(reason: "Use `locations`.")
687687
}
688688
689689
# A Directive can be adjacent to many parts of the GraphQL language, a
@@ -748,53 +748,52 @@ enum __DirectiveLocation {
748748
# placeholder for a string or numeric value. However an Enum value is returned in
749749
# a JSON response as a string.
750750
type __EnumValue {
751-
name: String!
751+
deprecationReason: String
752752
description: String
753753
isDeprecated: Boolean!
754-
deprecationReason: String
754+
name: String!
755755
}
756756
757757
# Object and Interface types are described by a list of Fields, each of which has
758758
# a name, potentially a list of arguments, and a return type.
759759
type __Field {
760-
name: String!
761-
description: String
762760
args: [__InputValue!]!
763-
type: __Type!
764-
isDeprecated: Boolean!
765761
deprecationReason: String
762+
description: String
763+
isDeprecated: Boolean!
764+
name: String!
765+
type: __Type!
766766
}
767767
768768
# Arguments provided to Fields or Directives and the input fields of an
769769
# InputObject are represented as Input Values which describe their type and
770770
# optionally a default value.
771771
type __InputValue {
772-
name: String!
773-
description: String
774-
type: __Type!
775-
776772
# A GraphQL-formatted string representing the default value for this input value.
777773
defaultValue: String
774+
description: String
775+
name: String!
776+
type: __Type!
778777
}
779778
780779
# A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all
781780
# available types and directives on the server, as well as the entry points for
782781
# query, mutation, and subscription operations.
783782
type __Schema {
784-
# A list of all types supported by this server.
785-
types: [__Type!]!
786-
787-
# The type that query operations will be rooted at.
788-
queryType: __Type!
783+
# A list of all directives supported by this server.
784+
directives: [__Directive!]!
789785
790786
# If this server supports mutation, the type that mutation operations will be rooted at.
791787
mutationType: __Type
792788
789+
# The type that query operations will be rooted at.
790+
queryType: __Type!
791+
793792
# If this server support subscription, the type that subscription operations will be rooted at.
794793
subscriptionType: __Type
795794
796-
# A list of all directives supported by this server.
797-
directives: [__Directive!]!
795+
# A list of all types supported by this server.
796+
types: [__Type!]!
798797
}
799798
800799
# The fundamental unit of any GraphQL Schema is the type. There are many kinds of
@@ -806,15 +805,15 @@ enum __DirectiveLocation {
806805
# they describe. Abstract types, Union and Interface, provide the Object types
807806
# possible at runtime. List and NonNull types compose other types.
808807
type __Type {
809-
kind: __TypeKind!
810-
name: String
811808
description: String
812-
fields(includeDeprecated: Boolean = false): [__Field!]
813-
interfaces: [__Type!]
814-
possibleTypes: [__Type!]
815809
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
810+
fields(includeDeprecated: Boolean = false): [__Field!]
816811
inputFields: [__InputValue!]
812+
interfaces: [__Type!]
813+
kind: __TypeKind!
814+
name: String
817815
ofType: __Type
816+
possibleTypes: [__Type!]
818817
}
819818
820819
# An enum describing what kind of type a given `__Type` is.

0 commit comments

Comments
 (0)