Skip to content

Commit

Permalink
fix(graphql-printer)!: Exported type will be on the top of the document.
Browse files Browse the repository at this point in the history
Closes: #100
  • Loading branch information
LastDragon-ru committed Oct 18, 2023
1 parent 1f78bbd commit 5b9e5b2
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 190 deletions.
13 changes: 10 additions & 3 deletions packages/graphql-printer/src/Blocks/ListBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ private function getBlocks(): array {
// Sort
if (count($blocks) > 0 && $this->isNormalized()) {
usort($blocks, static function (Block $a, Block $b): int {
$aName = $a instanceof NamedBlock ? $a->getName() : '';
$bName = $b instanceof NamedBlock ? $b->getName() : '';
if ($a instanceof NamedBlock && $b instanceof NamedBlock) {
return strnatcmp($a->getName(), $b->getName());
} elseif ($a instanceof NamedBlock) {
return -1;
} elseif ($b instanceof NamedBlock) {
return 1;
} else {
// empty
}

return strnatcmp($aName, $bName);
return 0;
});
}

Expand Down
6 changes: 4 additions & 2 deletions packages/graphql-printer/src/Contracts/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

interface Printer {
/**
* Print the current type only.
* Print the current type/note/etc only.
*
* Please note:
* - types filtering will work only if the schema is known
Expand All @@ -32,9 +32,11 @@ public function print(
): Result;

/**
* Print current type and all used types/directives.
* Print current type/note/etc and all used types/directives.
*
* Please note:
* - the exported object will be on the top of document
* - the document may be not valid GraphQL document for some objects (fields/arguments/etc)
* - the Schema is required to determine the type (and used types) of argument/variable/etc nodes
* - types filtering will work only if the schema is known
* - for some AST node types, their type may also be required
Expand Down
13 changes: 9 additions & 4 deletions packages/graphql-printer/src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function export(
}
}
} else {
$this->process($collector, $context, $content, $level, $used);
$content[] = $this->process($collector, $context, $content, $level, $used);
}

return new ResultImpl($collector, $content->serialize($collector, $level, $used));
Expand Down Expand Up @@ -172,16 +172,19 @@ protected function getList(Context $context, bool $root = false, bool $eof = tru

/**
* @param Block&ArrayAccess<Block, Block> $root
*
* @return Block&ArrayAccess<Block, Block>
*/
protected function process(
Collector $collector,
Context $context,
Block $root,
int $level,
int $used,
): void {
): Block {
$root = $this->analyze($collector, $root, $level, $used);
$stack = $collector->getUsedDirectives() + $collector->getUsedTypes();
$output = $this->getList($context);
$printed = [];
$directives = $context->getSettings()->isPrintDirectiveDefinitions();

Expand Down Expand Up @@ -217,15 +220,17 @@ protected function process(
}

// Stack
if ($block && !isset($root[$block])) {
if ($block && !isset($output[$block]) && !isset($root[$block])) {
$statistics = new Collector();
$root[] = $this->analyze($statistics, $block, $level, $used);
$output[] = $this->analyze($statistics, $block, $level, $used);
$statistics = $collector->addUsed($statistics);
$stack = $stack
+ $statistics->getUsedDirectives()
+ $statistics->getUsedTypes();
}
}

return $output;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
"""
Description
"""
input InputUnused {
a: InputA
}

"""
Directive description
"""
Expand Down Expand Up @@ -56,10 +63,3 @@ input InputA
input InputHidden {
a: Int
}

"""
Description
"""
input InputUnused {
a: InputA
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
interface InterfaceC
implements
& InterfaceA
& InterfaceB
{
a: Int
b: [String!]
c: [Float!]!
d: TypeHidden!

e(
a: Int
b: InputHidden
): Int
}

"""
Directive description
"""
Expand Down Expand Up @@ -59,22 +75,6 @@
)
}

interface InterfaceC
implements
& InterfaceA
& InterfaceB
{
a: Int
b: [String!]
c: [Float!]!
d: TypeHidden!

e(
a: Int
b: InputHidden
): Int
}

type TypeHidden {
a: Int
}
60 changes: 30 additions & 30 deletions packages/graphql-printer/src/PrinterTest~export-ObjectType.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
"""
Type description.
"""
type TypeA
implements
& InterfaceB
& InterfaceC
@directive(
location: "type"
)
@directive(
location: "extend"
)
{
a: Int
@deprecated

"""
Field description.
"""
b: [String!]

c: [Float!]!

d: [String!]
@directive(
location: "extend"
)
}

"""
Directive description
"""
Expand Down Expand Up @@ -75,36 +105,6 @@ implements
): Int
}

"""
Type description.
"""
type TypeA
implements
& InterfaceB
& InterfaceC
@directive(
location: "type"
)
@directive(
location: "extend"
)
{
a: Int
@deprecated

"""
Field description.
"""
b: [String!]

c: [Float!]!

d: [String!]
@directive(
location: "extend"
)
}

type TypeHidden {
a: Int
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
union Union =
| TypeA

"""
Directive description
"""
Expand Down Expand Up @@ -108,6 +111,3 @@ implements
type TypeHidden {
a: Int
}

union Union =
| TypeA
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
union Union =
| TypeA

"""
Directive description
"""
Expand Down Expand Up @@ -108,6 +111,3 @@
type TypeHidden {
a: Int
}

union Union =
| TypeA
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
"""
Description
"""
input CodeInput
@schemaDirective
{
a: Boolean
}

"""
Directive
"""
Expand All @@ -16,12 +25,3 @@ on
| SCALAR
| SCHEMA
| UNION

"""
Description
"""
input CodeInput
@schemaDirective
{
a: Boolean
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
union CodeUnion =
| CodeType

"""
Directive
"""
Expand Down Expand Up @@ -25,6 +28,3 @@
{
a: Boolean
}

union CodeUnion =
| CodeType
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
"""
Description
"""
input SchemaInput {
a: CodeInput
}

"""
Directive
"""
Expand Down Expand Up @@ -25,10 +32,3 @@ input CodeInput
{
a: Boolean
}

"""
Description
"""
input SchemaInput {
a: CodeInput
}
Loading

0 comments on commit 5b9e5b2

Please sign in to comment.