Skip to content

Commit 92ed2ab

Browse files
authored
Immutability (#584)
* Make parts of graphqlite immutable * Revert previous versions docs * Refactor getters, PHPStan, codestyle * Fix phpcbf's broken Cloneable * Fix return types in docs * Fix "test" class names used for resolvers * Refactor GlobAnnotationsCache to be immutable
1 parent 18c5060 commit 92ed2ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+898
-739
lines changed

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ parameters:
1717
- "#Parameter .* of class ReflectionMethod constructor expects string(\\|null)?, object\\|string given.#"
1818
- "#PHPDoc tag @throws with type Psr\\\\SimpleCache\\\\InvalidArgumentException is not subtype of Throwable#"
1919
- '#Variable \$context might not be defined.#'
20+
# TODO: fix these in the resolver refactor PR that follows; it'll be initialized in the constructor
21+
- '#Class TheCodingMachine\\GraphQLite\\(QueryFieldDescriptor|InputFieldDescriptor) has an uninitialized readonly property \$(originalResolver|resolver)\. Assign it in the constructor.#'
22+
- '#Readonly property TheCodingMachine\\GraphQLite\\(QueryFieldDescriptor|InputFieldDescriptor)::\$(originalResolver|resolver) is assigned outside of the constructor\.#'
23+
- '#Property TheCodingMachine\\GraphQLite\\(QueryFieldDescriptor|InputFieldDescriptor)::\$resolver \(callable\) in isset\(\) is not nullable\.#'
2024
-
2125
message: '#Parameter .* of class GraphQL\\Error\\Error constructor expects#'
2226
path: src/Exceptions/WebonyxErrorHandler.php

src/Context/Context.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace TheCodingMachine\GraphQLite\Context;
66

77
use SplObjectStorage;
8+
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
89
use TheCodingMachine\GraphQLite\PrefetchBuffer;
9-
use TheCodingMachine\GraphQLite\QueryField;
1010

1111
/**
1212
* A context class that should be passed to the Webonyx executor.
@@ -24,7 +24,7 @@ public function __construct()
2424
* Returns the prefetch buffer associated to the field $field.
2525
* (the buffer is created on the fly if it does not exist yet).
2626
*/
27-
public function getPrefetchBuffer(QueryField $field): PrefetchBuffer
27+
public function getPrefetchBuffer(ParameterInterface $field): PrefetchBuffer
2828
{
2929
if ($this->prefetchBuffers->offsetExists($field)) {
3030
$prefetchBuffer = $this->prefetchBuffers->offsetGet($field);

src/Context/ContextInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace TheCodingMachine\GraphQLite\Context;
66

7+
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
78
use TheCodingMachine\GraphQLite\PrefetchBuffer;
8-
use TheCodingMachine\GraphQLite\QueryField;
99

1010
/**
1111
* A context class that should be passed to the Webonyx executor.
@@ -18,5 +18,5 @@ interface ContextInterface
1818
* Returns the prefetch buffer associated to the field $field.
1919
* (the buffer is created on the fly if it does not exist yet).
2020
*/
21-
public function getPrefetchBuffer(QueryField $field): PrefetchBuffer;
21+
public function getPrefetchBuffer(ParameterInterface $field): PrefetchBuffer;
2222
}

src/FieldsBuilder.php

Lines changed: 106 additions & 90 deletions
Large diffs are not rendered by default.

src/InputField.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use GraphQL\Type\Definition\Type;
1515
use TheCodingMachine\GraphQLite\Exceptions\GraphQLAggregateException;
1616
use TheCodingMachine\GraphQLite\Middlewares\ResolverInterface;
17-
use TheCodingMachine\GraphQLite\Middlewares\SourceResolverInterface;
1817
use TheCodingMachine\GraphQLite\Parameters\MissingArgumentException;
1918
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
2019
use TheCodingMachine\GraphQLite\Parameters\SourceParameter;
@@ -54,12 +53,9 @@ public function __construct(string $name, InputType $type, array $arguments, Res
5453
}
5554

5655
if ($originalResolver !== null && $resolver !== null) {
57-
$this->resolve = function ($source, array $args, $context, ResolveInfo $info) use ($arguments, $originalResolver, $resolver) {
58-
if ($originalResolver instanceof SourceResolverInterface) {
59-
$originalResolver->setObject($source);
60-
}
56+
$this->resolve = function (object $source, array $args, $context, ResolveInfo $info) use ($arguments, $originalResolver, $resolver) {
6157
$toPassArgs = $this->paramsToArguments($arguments, $source, $args, $context, $info, $resolver);
62-
$result = $resolver(...$toPassArgs);
58+
$result = $resolver($source, ...$toPassArgs);
6359

6460
try {
6561
$this->assertInputType($result);
@@ -72,7 +68,7 @@ public function __construct(string $name, InputType $type, array $arguments, Res
7268
};
7369
} else {
7470
$this->forConstructorHydration = true;
75-
$this->resolve = function ($source, array $args, $context, ResolveInfo $info) use ($arguments) {
71+
$this->resolve = function (object|null $source, array $args, $context, ResolveInfo $info) use ($arguments) {
7672
$result = $arguments[$this->name]->resolve($source, $args, $context, $info);
7773
$this->assertInputType($result);
7874
return $result;
@@ -143,7 +139,7 @@ public static function fromFieldDescriptor(InputFieldDescriptor $fieldDescriptor
143139
if ($fieldDescriptor->isInjectSource() === true) {
144140
$arguments = ['__graphqlite_source' => new SourceParameter()] + $arguments;
145141
}
146-
$fieldDescriptor->setParameters($arguments);
142+
$fieldDescriptor = $fieldDescriptor->withParameters($arguments);
147143

148144
return self::fromDescriptor($fieldDescriptor);
149145
}

0 commit comments

Comments
 (0)