Skip to content

Commit

Permalink
Merge pull request #12 from vossik/sorting
Browse files Browse the repository at this point in the history
added TypeKindSorter
  • Loading branch information
peldax authored Jun 17, 2020
2 parents ce917c6 + 1f68732 commit 327b3f6
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Utils/Sort/TypeKindSorter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types = 1);

namespace Graphpinator\Utils\Sort;

class TypeKindSorter implements PrintSorter
{
public function sortTypes(array $types) : array
{
$interface = $union = $input = $enum = $scalar = $object = [];

foreach ($types as $name => $type) {
switch ($type->getTypeKind()) {
case \Graphpinator\Type\Introspection\TypeKind::INTERFACE:
$interface[$name] = $type;
break;
case \Graphpinator\Type\Introspection\TypeKind::UNION:
$union[$name] = $type;
break;
case \Graphpinator\Type\Introspection\TypeKind::INPUT_OBJECT:
$input[$name] = $type;
break;
case \Graphpinator\Type\Introspection\TypeKind::ENUM:
$enum[$name] = $type;
break;
case \Graphpinator\Type\Introspection\TypeKind::SCALAR:
$scalar[$name] = $type;
break;
case \Graphpinator\Type\Introspection\TypeKind::OBJECT:
$object[$name] = $type;
break;
}
}

\ksort($interface);
\ksort($union);
\ksort($input);
\ksort($enum);
\ksort($scalar);
\ksort($object);

return \array_merge($interface, $object, $union, $input, $scalar, $enum);
}

public function sortDirectives(array $directives) : array
{
\ksort($directives);

return $directives;
}
}
146 changes: 146 additions & 0 deletions tests/Unit/Utils/Sort/TypeKindSorterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

declare(strict_types=1);

namespace Graphpinator\Tests\Unit\Utils\Sort;

final class TypeKindSorterTest extends \PHPUnit\Framework\TestCase
{
public function testPrintTestSchema() : void
{
$expected = <<<EOL
schema {
query: Query
mutation: null
subscription: null
}
interface TestInterface {
name: String!
}
type Abc {
field1(arg1: Int = 123, arg2: TestInput): TestInterface @deprecated
}
type Query {
field0: TestUnion
fieldInvalidType: TestUnion
fieldAbstract: TestUnion
fieldThrow: TestUnion
}
type Xyz implements TestInterface {
name: String!
}
type Zzz {
enumList: [TestEnum]
}
union TestUnion = Abc | Xyz
input TestInnerInput {
name: String!
number: [Int!]!
bool: Boolean
}
input TestInput {
name: String!
inner: TestInnerInput
innerList: [TestInnerInput!]!
innerNotNull: TestInnerInput!
}
enum TestEnum {
A
B
C
D
}
enum TestExplicitEnum {
A @deprecated
B @deprecated
C @deprecated
D @deprecated
}
directive @invalidDirective repeatable on FIELD
directive @testDirective repeatable on FIELD
EOL;

self::assertSame($expected, \Graphpinator\Tests\Spec\TestSchema::getSchema()->printSchema(new \Graphpinator\Utils\Sort\TypeKindSorter()));
}

public function testPrintFullTestSchema() : void
{
$expected = <<<EOL
schema {
query: Query
mutation: Query
subscription: Query
}
interface TestInterface {
name: String!
}
type Abc {
field1(arg1: Int = 123, arg2: TestInput): TestInterface @deprecated
}
type Query {
field0: TestUnion
fieldInvalidType: TestUnion
fieldAbstract: TestUnion
fieldThrow: TestUnion
}
type Xyz implements TestInterface {
name: String!
}
type Zzz {
enumList: [TestEnum]
}
union TestUnion = Abc | Xyz
input TestInnerInput {
name: String!
number: [Int!]!
bool: Boolean
}
input TestInput {
name: String!
inner: TestInnerInput
innerList: [TestInnerInput!]!
innerNotNull: TestInnerInput!
}
enum TestEnum {
A
B
C
D
}
enum TestExplicitEnum {
A @deprecated
B @deprecated
C @deprecated
D @deprecated
}
directive @invalidDirective repeatable on FIELD
directive @testDirective repeatable on FIELD
EOL;

self::assertSame($expected, \Graphpinator\Tests\Spec\TestSchema::getFullSchema()->printSchema(new \Graphpinator\Utils\Sort\TypeKindSorter()));
}
}

0 comments on commit 327b3f6

Please sign in to comment.