Skip to content

Commit 24bcc65

Browse files
committed
SchemaPrinter: reverted sorting of fields in printed version (as it breaks s = parse(print(s)) rule)
1 parent ea94ee7 commit 24bcc65

File tree

3 files changed

+44
-49
lines changed

3 files changed

+44
-49
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private static function printEnumValues($values)
204204

205205
private static function printInputObject(InputObjectType $type)
206206
{
207-
$fields = self::sortFields($type->getFields());
207+
$fields = array_values($type->getFields());
208208
return self::printDescription($type) .
209209
"input {$type->name} {\n" .
210210
implode("\n", array_map(function($f, $i) {
@@ -215,7 +215,7 @@ private static function printInputObject(InputObjectType $type)
215215

216216
private static function printFields($type)
217217
{
218-
$fields = self::sortFields($type->getFields());
218+
$fields = array_values($type->getFields());
219219
return implode("\n", array_map(function($f, $i) {
220220
return self::printDescription($f, ' ', !$i) . ' ' .
221221
$f->name . self::printArgs($f->args, ' ') . ': ' .
@@ -302,10 +302,4 @@ private static function breakLine($line, $len)
302302
return trim($part);
303303
}, $parts);
304304
}
305-
306-
private static function sortFields(array $fields)
307-
{
308-
ksort($fields);
309-
return array_values($fields);
310-
}
311305
}

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-
bool: Boolean
74+
str: String
75+
int: Int
7576
float: Float
7677
id: ID
77-
int: Int
78-
str: String
78+
bool: Boolean
7979
}
8080
';
8181
$output = $this->cycleOutput($body);
@@ -194,11 +194,11 @@ public function testTypeModifiers()
194194
}
195195
196196
type HelloScalars {
197-
listOfNonNullStrs: [String!]
197+
nonNullStr: String!
198198
listOfStrs: [String]
199-
nonNullListOfNonNullStrs: [String!]!
199+
listOfNonNullStrs: [String!]
200200
nonNullListOfStrs: [String]!
201-
nonNullStr: String!
201+
nonNullListOfNonNullStrs: [String!]!
202202
}
203203
';
204204
$output = $this->cycleOutput($body);
@@ -216,8 +216,8 @@ public function testRecursiveType()
216216
}
217217
218218
type Recurse {
219-
recurse: Recurse
220219
str: String
220+
recurse: Recurse
221221
}
222222
';
223223
$output = $this->cycleOutput($body);
@@ -259,10 +259,10 @@ public function testSingleArgumentField()
259259
}
260260
261261
type Hello {
262-
booleanToStr(bool: Boolean): String
262+
str(int: Int): String
263263
floatToStr(float: Float): String
264264
idToStr(id: ID): String
265-
str(int: Int): String
265+
booleanToStr(bool: Boolean): String
266266
strToStr(bool: String): String
267267
}
268268
';
@@ -479,9 +479,9 @@ public function testSimpleTypeWithMutation()
479479
}
480480
481481
type HelloScalars {
482-
bool: Boolean
483-
int: Int
484482
str: String
483+
int: Int
484+
bool: Boolean
485485
}
486486
487487
type Mutation {
@@ -504,9 +504,9 @@ public function testSimpleTypeWithSubscription()
504504
}
505505
506506
type HelloScalars {
507-
bool: Boolean
508-
int: Int
509507
str: String
508+
int: Int
509+
bool: Boolean
510510
}
511511
512512
type Subscription {
@@ -572,9 +572,9 @@ enum MyEnum {
572572
}
573573
574574
type Query {
575-
enum: MyEnum
576575
field1: String @deprecated
577576
field2: Int @deprecated(reason: "Because I said so")
577+
enum: MyEnum
578578
}
579579
';
580580
$output = $this->cycleOutput($body);

tests/Utils/SchemaPrinterTest.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ interface Baaz {
445445
}
446446
447447
type Bar implements Foo, Baaz {
448-
int: Int
449448
str: String
449+
int: Int
450450
}
451451
452452
interface Foo {
@@ -512,8 +512,8 @@ public function testPrintUnions()
512512
union MultipleUnion = Foo | Bar
513513
514514
type Root {
515-
multiple: MultipleUnion
516515
single: SingleUnion
516+
multiple: MultipleUnion
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-
args: [__InputValue!]!
680+
name: String!
681681
description: String
682682
locations: [__DirectiveLocation!]!
683-
name: String!
684-
onField: Boolean! @deprecated(reason: "Use `locations`.")
685-
onFragment: Boolean! @deprecated(reason: "Use `locations`.")
683+
args: [__InputValue!]!
686684
onOperation: Boolean! @deprecated(reason: "Use `locations`.")
685+
onFragment: Boolean! @deprecated(reason: "Use `locations`.")
686+
onField: Boolean! @deprecated(reason: "Use `locations`.")
687687
}
688688
689689
# A Directive can be adjacent to many parts of the GraphQL language, a
@@ -748,52 +748,53 @@ 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-
deprecationReason: String
751+
name: String!
752752
description: String
753753
isDeprecated: Boolean!
754-
name: String!
754+
deprecationReason: 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-
args: [__InputValue!]!
761-
deprecationReason: String
762-
description: String
763-
isDeprecated: Boolean!
764760
name: String!
761+
description: String
762+
args: [__InputValue!]!
765763
type: __Type!
764+
isDeprecated: Boolean!
765+
deprecationReason: String
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-
# A GraphQL-formatted string representing the default value for this input value.
773-
defaultValue: String
774-
description: String
775772
name: String!
773+
description: String
776774
type: __Type!
775+
776+
# A GraphQL-formatted string representing the default value for this input value.
777+
defaultValue: String
777778
}
778779
779780
# A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all
780781
# available types and directives on the server, as well as the entry points for
781782
# query, mutation, and subscription operations.
782783
type __Schema {
783-
# A list of all directives supported by this server.
784-
directives: [__Directive!]!
785-
786-
# If this server supports mutation, the type that mutation operations will be rooted at.
787-
mutationType: __Type
784+
# A list of all types supported by this server.
785+
types: [__Type!]!
788786
789787
# The type that query operations will be rooted at.
790788
queryType: __Type!
791789
790+
# If this server supports mutation, the type that mutation operations will be rooted at.
791+
mutationType: __Type
792+
792793
# If this server support subscription, the type that subscription operations will be rooted at.
793794
subscriptionType: __Type
794795
795-
# A list of all types supported by this server.
796-
types: [__Type!]!
796+
# A list of all directives supported by this server.
797+
directives: [__Directive!]!
797798
}
798799
799800
# The fundamental unit of any GraphQL Schema is the type. There are many kinds of
@@ -805,15 +806,15 @@ enum __DirectiveLocation {
805806
# they describe. Abstract types, Union and Interface, provide the Object types
806807
# possible at runtime. List and NonNull types compose other types.
807808
type __Type {
809+
kind: __TypeKind!
810+
name: String
808811
description: String
809-
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
810812
fields(includeDeprecated: Boolean = false): [__Field!]
811-
inputFields: [__InputValue!]
812813
interfaces: [__Type!]
813-
kind: __TypeKind!
814-
name: String
815-
ofType: __Type
816814
possibleTypes: [__Type!]
815+
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
816+
inputFields: [__InputValue!]
817+
ofType: __Type
817818
}
818819
819820
# An enum describing what kind of type a given `__Type` is.

0 commit comments

Comments
 (0)