SchemaPrinter: reverted sorting of fields in printed version (as it breaks s = parse(print(s)) rule)

This commit is contained in:
Vladimir Razuvaev 2017-07-05 19:45:02 +07:00
parent ea94ee7515
commit 24bcc65314
3 changed files with 46 additions and 51 deletions

View File

@ -204,7 +204,7 @@ class SchemaPrinter
private static function printInputObject(InputObjectType $type) private static function printInputObject(InputObjectType $type)
{ {
$fields = self::sortFields($type->getFields()); $fields = array_values($type->getFields());
return self::printDescription($type) . return self::printDescription($type) .
"input {$type->name} {\n" . "input {$type->name} {\n" .
implode("\n", array_map(function($f, $i) { implode("\n", array_map(function($f, $i) {
@ -215,7 +215,7 @@ class SchemaPrinter
private static function printFields($type) private static function printFields($type)
{ {
$fields = self::sortFields($type->getFields()); $fields = array_values($type->getFields());
return implode("\n", array_map(function($f, $i) { return implode("\n", array_map(function($f, $i) {
return self::printDescription($f, ' ', !$i) . ' ' . return self::printDescription($f, ' ', !$i) . ' ' .
$f->name . self::printArgs($f->args, ' ') . ': ' . $f->name . self::printArgs($f->args, ' ') . ': ' .
@ -302,10 +302,4 @@ class SchemaPrinter
return trim($part); return trim($part);
}, $parts); }, $parts);
} }
private static function sortFields(array $fields)
{
ksort($fields);
return array_values($fields);
}
} }

View File

@ -71,11 +71,11 @@ schema {
} }
type HelloScalars { type HelloScalars {
bool: Boolean str: String
int: Int
float: Float float: Float
id: ID id: ID
int: Int bool: Boolean
str: String
} }
'; ';
$output = $this->cycleOutput($body); $output = $this->cycleOutput($body);
@ -194,11 +194,11 @@ schema {
} }
type HelloScalars { type HelloScalars {
listOfNonNullStrs: [String!]
listOfStrs: [String]
nonNullListOfNonNullStrs: [String!]!
nonNullListOfStrs: [String]!
nonNullStr: String! nonNullStr: String!
listOfStrs: [String]
listOfNonNullStrs: [String!]
nonNullListOfStrs: [String]!
nonNullListOfNonNullStrs: [String!]!
} }
'; ';
$output = $this->cycleOutput($body); $output = $this->cycleOutput($body);
@ -216,8 +216,8 @@ schema {
} }
type Recurse { type Recurse {
recurse: Recurse
str: String str: String
recurse: Recurse
} }
'; ';
$output = $this->cycleOutput($body); $output = $this->cycleOutput($body);
@ -259,10 +259,10 @@ schema {
} }
type Hello { type Hello {
booleanToStr(bool: Boolean): String str(int: Int): String
floatToStr(float: Float): String floatToStr(float: Float): String
idToStr(id: ID): String idToStr(id: ID): String
str(int: Int): String booleanToStr(bool: Boolean): String
strToStr(bool: String): String strToStr(bool: String): String
} }
'; ';
@ -479,9 +479,9 @@ schema {
} }
type HelloScalars { type HelloScalars {
bool: Boolean
int: Int
str: String str: String
int: Int
bool: Boolean
} }
type Mutation { type Mutation {
@ -504,9 +504,9 @@ schema {
} }
type HelloScalars { type HelloScalars {
bool: Boolean
int: Int
str: String str: String
int: Int
bool: Boolean
} }
type Subscription { type Subscription {
@ -572,9 +572,9 @@ enum MyEnum {
} }
type Query { type Query {
enum: MyEnum
field1: String @deprecated field1: String @deprecated
field2: Int @deprecated(reason: "Because I said so") field2: Int @deprecated(reason: "Because I said so")
enum: MyEnum
} }
'; ';
$output = $this->cycleOutput($body); $output = $this->cycleOutput($body);

View File

@ -445,8 +445,8 @@ interface Baaz {
} }
type Bar implements Foo, Baaz { type Bar implements Foo, Baaz {
int: Int
str: String str: String
int: Int
} }
interface Foo { interface Foo {
@ -512,8 +512,8 @@ type Foo {
union MultipleUnion = Foo | Bar union MultipleUnion = Foo | Bar
type Root { type Root {
multiple: MultipleUnion
single: SingleUnion single: SingleUnion
multiple: MultipleUnion
} }
union SingleUnion = Foo union SingleUnion = Foo
@ -677,13 +677,13 @@ directive @deprecated(
# skipping a field. Directives provide this by describing additional information # skipping a field. Directives provide this by describing additional information
# to the executor. # to the executor.
type __Directive { type __Directive {
args: [__InputValue!]! name: String!
description: String description: String
locations: [__DirectiveLocation!]! locations: [__DirectiveLocation!]!
name: String! args: [__InputValue!]!
onField: Boolean! @deprecated(reason: "Use `locations`.")
onFragment: Boolean! @deprecated(reason: "Use `locations`.")
onOperation: Boolean! @deprecated(reason: "Use `locations`.") onOperation: Boolean! @deprecated(reason: "Use `locations`.")
onFragment: Boolean! @deprecated(reason: "Use `locations`.")
onField: Boolean! @deprecated(reason: "Use `locations`.")
} }
# A Directive can be adjacent to many parts of the GraphQL language, a # A Directive can be adjacent to many parts of the GraphQL language, a
@ -748,52 +748,53 @@ enum __DirectiveLocation {
# placeholder for a string or numeric value. However an Enum value is returned in # placeholder for a string or numeric value. However an Enum value is returned in
# a JSON response as a string. # a JSON response as a string.
type __EnumValue { type __EnumValue {
deprecationReason: String name: String!
description: String description: String
isDeprecated: Boolean! isDeprecated: Boolean!
name: String! deprecationReason: String
} }
# Object and Interface types are described by a list of Fields, each of which has # Object and Interface types are described by a list of Fields, each of which has
# a name, potentially a list of arguments, and a return type. # a name, potentially a list of arguments, and a return type.
type __Field { type __Field {
args: [__InputValue!]!
deprecationReason: String
description: String
isDeprecated: Boolean!
name: String! name: String!
description: String
args: [__InputValue!]!
type: __Type! type: __Type!
isDeprecated: Boolean!
deprecationReason: String
} }
# Arguments provided to Fields or Directives and the input fields of an # Arguments provided to Fields or Directives and the input fields of an
# InputObject are represented as Input Values which describe their type and # InputObject are represented as Input Values which describe their type and
# optionally a default value. # optionally a default value.
type __InputValue { type __InputValue {
name: String!
description: String
type: __Type!
# A GraphQL-formatted string representing the default value for this input value. # A GraphQL-formatted string representing the default value for this input value.
defaultValue: String defaultValue: String
description: String
name: String!
type: __Type!
} }
# A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all # A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all
# available types and directives on the server, as well as the entry points for # available types and directives on the server, as well as the entry points for
# query, mutation, and subscription operations. # query, mutation, and subscription operations.
type __Schema { type __Schema {
# A list of all directives supported by this server. # A list of all types supported by this server.
directives: [__Directive!]! types: [__Type!]!
# If this server supports mutation, the type that mutation operations will be rooted at.
mutationType: __Type
# The type that query operations will be rooted at. # The type that query operations will be rooted at.
queryType: __Type! queryType: __Type!
# If this server supports mutation, the type that mutation operations will be rooted at.
mutationType: __Type
# If this server support subscription, the type that subscription operations will be rooted at. # If this server support subscription, the type that subscription operations will be rooted at.
subscriptionType: __Type subscriptionType: __Type
# A list of all types supported by this server. # A list of all directives supported by this server.
types: [__Type!]! directives: [__Directive!]!
} }
# The fundamental unit of any GraphQL Schema is the type. There are many kinds of # The fundamental unit of any GraphQL Schema is the type. There are many kinds of
@ -805,15 +806,15 @@ type __Schema {
# they describe. Abstract types, Union and Interface, provide the Object types # they describe. Abstract types, Union and Interface, provide the Object types
# possible at runtime. List and NonNull types compose other types. # possible at runtime. List and NonNull types compose other types.
type __Type { type __Type {
description: String
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
fields(includeDeprecated: Boolean = false): [__Field!]
inputFields: [__InputValue!]
interfaces: [__Type!]
kind: __TypeKind! kind: __TypeKind!
name: String name: String
ofType: __Type description: String
fields(includeDeprecated: Boolean = false): [__Field!]
interfaces: [__Type!]
possibleTypes: [__Type!] possibleTypes: [__Type!]
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
inputFields: [__InputValue!]
ofType: __Type
} }
# An enum describing what kind of type a given `__Type` is. # An enum describing what kind of type a given `__Type` is.