From 6e91e2181cc1db5b189b5ae48a32d898cb2eff0c Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 18:04:30 +0200 Subject: [PATCH 01/11] Spread ternary expressions across multiple lines --- docs/data-fetching.md | 4 ++- src/Executor/Executor.php | 4 ++- src/Executor/ReferenceExecutor.php | 6 ++-- src/Executor/Values.php | 3 +- src/Language/Parser.php | 35 ++++++++++--------- src/Language/Visitor.php | 14 ++++++-- src/Server/Helper.php | 12 +++++-- src/Type/Definition/InputObjectType.php | 4 ++- src/Type/Definition/ListOfType.php | 4 ++- src/Type/Definition/NonNull.php | 4 ++- src/Type/Definition/ObjectType.php | 18 ++++++---- src/Type/Definition/Type.php | 4 ++- src/Type/Introspection.php | 4 ++- src/Type/SchemaValidationContext.php | 10 +++--- src/Utils/ASTDefinitionBuilder.php | 4 +-- src/Utils/SchemaExtender.php | 4 ++- src/Utils/SchemaPrinter.php | 7 ++-- src/Utils/TypeInfo.php | 10 +++--- src/Validator/Rules/LoneSchemaDefinition.php | 14 ++++---- .../Rules/OverlappingFieldsCanBeMerged.php | 24 ++++++------- .../ProvidedRequiredArgumentsOnDirectives.php | 16 +++++---- tests/Language/VisitorTest.php | 11 ++++-- tests/Utils/SchemaExtenderTest.php | 4 ++- tools/gendocs.php | 4 ++- 24 files changed, 141 insertions(+), 83 deletions(-) diff --git a/docs/data-fetching.md b/docs/data-fetching.md index 4bc0cfb..9b6f8e7 100644 --- a/docs/data-fetching.md +++ b/docs/data-fetching.md @@ -118,7 +118,9 @@ function defaultFieldResolver($source, $args, $context, \GraphQL\Type\Definition } } - return $property instanceof Closure ? $property($source, $args, $context, $info) : $property; + return $property instanceof Closure + ? $property($source, $args, $context, $info) + : $property; } ``` diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 30ccad4..db23e50 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -182,6 +182,8 @@ class Executor } } - return $property instanceof Closure ? $property($source, $args, $context, $info) : $property; + return $property instanceof Closure + ? $property($source, $args, $context, $info) + : $property; } } diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 6e35f7c..08911e4 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -252,9 +252,9 @@ class ReferenceExecutor implements ExecutorImplementation // // Similar to completeValueCatchingError. try { - $result = $operation->operation === 'mutation' ? - $this->executeFieldsSerially($type, $rootValue, $path, $fields) : - $this->executeFields($type, $rootValue, $path, $fields); + $result = $operation->operation === 'mutation' + ? $this->executeFieldsSerially($type, $rootValue, $path, $fields) + : $this->executeFields($type, $rootValue, $path, $fields); if ($this->isPromise($result)) { return $result->then( null, diff --git a/src/Executor/Values.php b/src/Executor/Values.php index 82e26c8..e80f868 100644 --- a/src/Executor/Values.php +++ b/src/Executor/Values.php @@ -273,6 +273,7 @@ class Values return $error->getMessage(); }, $errors - ) : []; + ) + : []; } } diff --git a/src/Language/Parser.php b/src/Language/Parser.php index a0c123f..2a68ffa 100644 --- a/src/Language/Parser.php +++ b/src/Language/Parser.php @@ -512,15 +512,15 @@ class Parser */ private function parseVariableDefinitions() { - return $this->peek(Token::PAREN_L) ? - $this->many( + return $this->peek(Token::PAREN_L) + ? $this->many( Token::PAREN_L, function () { return $this->parseVariableDefinition(); }, Token::PAREN_R - ) : - new NodeList([]); + ) + : new NodeList([]); } /** @@ -592,9 +592,9 @@ class Parser */ private function parseSelection() { - return $this->peek(Token::SPREAD) ? - $this->parseFragment() : - $this->parseField(); + return $this->peek(Token::SPREAD) + ? $this->parseFragment() + : $this->parseField(); } /** @@ -634,17 +634,17 @@ class Parser */ private function parseArguments($isConst) { - $parseFn = $isConst ? - function () { + $parseFn = $isConst + ? function () { return $this->parseConstArgument(); - } : - function () { + } + : function () { return $this->parseArgument(); }; - return $this->peek(Token::PAREN_L) ? - $this->many(Token::PAREN_L, $parseFn, Token::PAREN_R) : - new NodeList([]); + return $this->peek(Token::PAREN_L) + ? $this->many(Token::PAREN_L, $parseFn, Token::PAREN_R) + : new NodeList([]); } /** @@ -1208,8 +1208,8 @@ class Parser do { $types[] = $this->parseNamedType(); } while ($this->skip(Token::AMP) || - // Legacy support for the SDL? - (! empty($this->lexer->options['allowLegacySDLImplementsInterfaces']) && $this->peek(Token::NAME)) + // Legacy support for the SDL? + (! empty($this->lexer->options['allowLegacySDLImplementsInterfaces']) && $this->peek(Token::NAME)) ); } @@ -1545,7 +1545,8 @@ class Parser Token::BRACE_L, [$this, 'parseOperationTypeDefinition'], Token::BRACE_R - ) : []; + ) + : []; if (count($directives) === 0 && count($operationTypes) === 0) { $this->unexpected(); } diff --git a/src/Language/Visitor.php b/src/Language/Visitor.php index 45f8fff..43c59ed 100644 --- a/src/Language/Visitor.php +++ b/src/Language/Visitor.php @@ -251,8 +251,18 @@ class Visitor $inArray = $stack['inArray']; $stack = $stack['prev']; } else { - $key = $parent !== null ? ($inArray ? $index : $keys[$index]) : $UNDEFINED; - $node = $parent !== null ? ($parent instanceof NodeList || is_array($parent) ? $parent[$key] : $parent->{$key}) : $newRoot; + $key = $parent !== null + ? ($inArray + ? $index + : $keys[$index] + ) + : $UNDEFINED; + $node = $parent !== null + ? ($parent instanceof NodeList || is_array($parent) + ? $parent[$key] + : $parent->{$key} + ) + : $newRoot; if ($node === null || $node === $UNDEFINED) { continue; } diff --git a/src/Server/Helper.php b/src/Server/Helper.php index ff62b2b..8816f9f 100644 --- a/src/Server/Helper.php +++ b/src/Server/Helper.php @@ -73,10 +73,14 @@ class Helper } if (stripos($contentType, 'application/graphql') !== false) { - $rawBody = $readRawBodyFn ? $readRawBodyFn() : $this->readRawBody(); + $rawBody = $readRawBodyFn + ? $readRawBodyFn() + : $this->readRawBody(); $bodyParams = ['query' => $rawBody ?: '']; } elseif (stripos($contentType, 'application/json') !== false) { - $rawBody = $readRawBodyFn ? $readRawBodyFn() : $this->readRawBody(); + $rawBody = $readRawBodyFn ? + $readRawBodyFn() + : $this->readRawBody(); $bodyParams = json_decode($rawBody ?: '', true); if (json_last_error()) { @@ -272,7 +276,9 @@ class Helper ); } - $doc = $op->queryId ? $this->loadPersistedQuery($config, $op) : $op->query; + $doc = $op->queryId + ? $this->loadPersistedQuery($config, $op) + : $op->query; if (! $doc instanceof DocumentNode) { $doc = Parser::parse($doc); diff --git a/src/Type/Definition/InputObjectType.php b/src/Type/Definition/InputObjectType.php index c921dda..e31b998 100644 --- a/src/Type/Definition/InputObjectType.php +++ b/src/Type/Definition/InputObjectType.php @@ -69,7 +69,9 @@ class InputObjectType extends Type implements InputType, NullableType, NamedType if ($this->fields === null) { $this->fields = []; $fields = $this->config['fields'] ?? []; - $fields = is_callable($fields) ? call_user_func($fields) : $fields; + $fields = is_callable($fields) + ? call_user_func($fields) + : $fields; if (! is_array($fields)) { throw new InvariantViolation( diff --git a/src/Type/Definition/ListOfType.php b/src/Type/Definition/ListOfType.php index eb8200d..e81db88 100644 --- a/src/Type/Definition/ListOfType.php +++ b/src/Type/Definition/ListOfType.php @@ -31,6 +31,8 @@ class ListOfType extends Type implements WrappingType, OutputType, NullableType, { $type = $this->ofType; - return $recurse && $type instanceof WrappingType ? $type->getWrappedType($recurse) : $type; + return $recurse && $type instanceof WrappingType + ? $type->getWrappedType($recurse) + : $type; } } diff --git a/src/Type/Definition/NonNull.php b/src/Type/Definition/NonNull.php index 6799523..4b6fa48 100644 --- a/src/Type/Definition/NonNull.php +++ b/src/Type/Definition/NonNull.php @@ -66,6 +66,8 @@ class NonNull extends Type implements WrappingType, OutputType, InputType { $type = $this->ofType; - return $recurse && $type instanceof WrappingType ? $type->getWrappedType($recurse) : $type; + return $recurse && $type instanceof WrappingType + ? $type->getWrappedType($recurse) + : $type; } } diff --git a/src/Type/Definition/ObjectType.php b/src/Type/Definition/ObjectType.php index 532a9b0..327699a 100644 --- a/src/Type/Definition/ObjectType.php +++ b/src/Type/Definition/ObjectType.php @@ -185,7 +185,9 @@ class ObjectType extends Type implements OutputType, CompositeType, NullableType { if ($this->interfaces === null) { $interfaces = $this->config['interfaces'] ?? []; - $interfaces = is_callable($interfaces) ? call_user_func($interfaces) : $interfaces; + $interfaces = is_callable($interfaces) + ? call_user_func($interfaces) + : $interfaces; if ($interfaces !== null && ! is_array($interfaces)) { throw new InvariantViolation( @@ -207,12 +209,14 @@ class ObjectType extends Type implements OutputType, CompositeType, NullableType */ public function isTypeOf($value, $context, ResolveInfo $info) { - return isset($this->config['isTypeOf']) ? call_user_func( - $this->config['isTypeOf'], - $value, - $context, - $info - ) : null; + return isset($this->config['isTypeOf']) + ? call_user_func( + $this->config['isTypeOf'], + $value, + $context, + $info + ) + : null; } /** diff --git a/src/Type/Definition/Type.php b/src/Type/Definition/Type.php index 69faa1c..65da9e7 100644 --- a/src/Type/Definition/Type.php +++ b/src/Type/Definition/Type.php @@ -345,7 +345,9 @@ abstract class Type implements JsonSerializable */ public static function getNullableType($type) { - return $type instanceof NonNull ? $type->getWrappedType() : $type; + return $type instanceof NonNull + ? $type->getWrappedType() + : $type; } /** diff --git a/src/Type/Introspection.php b/src/Type/Introspection.php index f0cacee..3943b3e 100644 --- a/src/Type/Introspection.php +++ b/src/Type/Introspection.php @@ -488,7 +488,9 @@ EOD; 'type' => [ 'type' => Type::nonNull(self::_type()), 'resolve' => static function ($value) { - return method_exists($value, 'getType') ? $value->getType() : $value->type; + return method_exists($value, 'getType') + ? $value->getType() + : $value->type; }, ], 'defaultValue' => [ diff --git a/src/Type/SchemaValidationContext.php b/src/Type/SchemaValidationContext.php index 5e52164..a663018 100644 --- a/src/Type/SchemaValidationContext.php +++ b/src/Type/SchemaValidationContext.php @@ -768,8 +768,9 @@ class SchemaValidationContext ); } - return $union->astNode ? - $union->astNode->types : null; + return $union->astNode + ? $union->astNode->types + : null; } private function validateEnumValues(EnumType $enumType) @@ -824,8 +825,9 @@ class SchemaValidationContext ); } - return $enum->astNode ? - $enum->astNode->values : null; + return $enum->astNode + ? $enum->astNode->values + : null; } private function validateInputFields(InputObjectType $inputObj) diff --git a/src/Utils/ASTDefinitionBuilder.php b/src/Utils/ASTDefinitionBuilder.php index b743213..7cd96d0 100644 --- a/src/Utils/ASTDefinitionBuilder.php +++ b/src/Utils/ASTDefinitionBuilder.php @@ -394,8 +394,8 @@ class ASTDefinitionBuilder function ($typeNode) { return $this->buildType($typeNode); } - ) : - [], + ) + : [], 'astNode' => $def, ]); } diff --git a/src/Utils/SchemaExtender.php b/src/Utils/SchemaExtender.php index 4fefc61..6188beb 100644 --- a/src/Utils/SchemaExtender.php +++ b/src/Utils/SchemaExtender.php @@ -609,7 +609,9 @@ class SchemaExtender } $schemaExtensionASTNodes = count($schemaExtensions) > 0 - ? ($schema->extensionASTNodes ? array_merge($schema->extensionASTNodes, $schemaExtensions) : $schemaExtensions) + ? ($schema->extensionASTNodes + ? array_merge($schema->extensionASTNodes, $schemaExtensions) + : $schemaExtensions) : $schema->extensionASTNodes; $types = array_merge( diff --git a/src/Utils/SchemaPrinter.php b/src/Utils/SchemaPrinter.php index 9e9e1ca..a45c99c 100644 --- a/src/Utils/SchemaPrinter.php +++ b/src/Utils/SchemaPrinter.php @@ -353,8 +353,8 @@ class SchemaPrinter private static function printObject(ObjectType $type, array $options) : string { $interfaces = $type->getInterfaces(); - $implementedInterfaces = ! empty($interfaces) ? - ' implements ' . implode( + $implementedInterfaces = ! empty($interfaces) + ? ' implements ' . implode( ' & ', array_map( static function ($i) { @@ -362,7 +362,8 @@ class SchemaPrinter }, $interfaces ) - ) : ''; + ) + : ''; return self::printDescription($options, $type) . sprintf("type %s%s {\n%s\n}", $type->name, $implementedInterfaces, self::printFields($options, $type)); diff --git a/src/Utils/TypeInfo.php b/src/Utils/TypeInfo.php index 13892fc..8468407 100644 --- a/src/Utils/TypeInfo.php +++ b/src/Utils/TypeInfo.php @@ -302,10 +302,12 @@ class TypeInfo case $node instanceof InlineFragmentNode: case $node instanceof FragmentDefinitionNode: $typeConditionNode = $node->typeCondition; - $outputType = $typeConditionNode ? self::typeFromAST( - $schema, - $typeConditionNode - ) : Type::getNamedType($this->getType()); + $outputType = $typeConditionNode + ? self::typeFromAST( + $schema, + $typeConditionNode + ) + : Type::getNamedType($this->getType()); $this->typeStack[] = Type::isOutputType($outputType) ? $outputType : null; break; diff --git a/src/Validator/Rules/LoneSchemaDefinition.php b/src/Validator/Rules/LoneSchemaDefinition.php index 1a8da67..72e5832 100644 --- a/src/Validator/Rules/LoneSchemaDefinition.php +++ b/src/Validator/Rules/LoneSchemaDefinition.php @@ -19,12 +19,14 @@ class LoneSchemaDefinition extends ValidationRule public function getVisitor(ValidationContext $context) { $oldSchema = $context->getSchema(); - $alreadyDefined = $oldSchema !== null ? ( - $oldSchema->getAstNode() || - $oldSchema->getQueryType() || - $oldSchema->getMutationType() || - $oldSchema->getSubscriptionType() - ) : false; + $alreadyDefined = $oldSchema !== null + ? ( + $oldSchema->getAstNode() || + $oldSchema->getQueryType() || + $oldSchema->getMutationType() || + $oldSchema->getSubscriptionType() + ) + : false; $schemaDefinitionsCount = 0; diff --git a/src/Validator/Rules/OverlappingFieldsCanBeMerged.php b/src/Validator/Rules/OverlappingFieldsCanBeMerged.php index 8dfa7f1..efd9038 100644 --- a/src/Validator/Rules/OverlappingFieldsCanBeMerged.php +++ b/src/Validator/Rules/OverlappingFieldsCanBeMerged.php @@ -473,24 +473,24 @@ class OverlappingFieldsCanBeMerged extends ValidationRule private function doTypesConflict(OutputType $type1, OutputType $type2) { if ($type1 instanceof ListOfType) { - return $type2 instanceof ListOfType ? - $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : - true; + return $type2 instanceof ListOfType + ? $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) + : true; } if ($type2 instanceof ListOfType) { - return $type1 instanceof ListOfType ? - $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : - true; + return $type1 instanceof ListOfType + ? $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) + : true; } if ($type1 instanceof NonNull) { - return $type2 instanceof NonNull ? - $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : - true; + return $type2 instanceof NonNull + ? $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) + : true; } if ($type2 instanceof NonNull) { - return $type1 instanceof NonNull ? - $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : - true; + return $type1 instanceof NonNull + ? $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) + : true; } if (Type::isLeafType($type1) || Type::isLeafType($type2)) { return $type1 !== $type2; diff --git a/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php b/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php index 8750711..080d53e 100644 --- a/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php +++ b/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php @@ -66,13 +66,15 @@ class ProvidedRequiredArgumentsOnDirectives extends ValidationRule } $requiredArgsMap[$def->name->value] = Utils::keyMap( - $arguments ? array_filter($arguments, static function (Node $argument) : bool { - return $argument instanceof NonNullTypeNode && - ( - ! isset($argument->defaultValue) || - $argument->defaultValue === null - ); - }) : [], + $arguments + ? array_filter($arguments, static function (Node $argument) : bool { + return $argument instanceof NonNullTypeNode && + ( + ! isset($argument->defaultValue) || + $argument->defaultValue === null + ); + }) + : [], static function (NamedTypeNode $argument) : string { return $argument->name->value; } diff --git a/tests/Language/VisitorTest.php b/tests/Language/VisitorTest.php index b04b4db..0a2c6b3 100644 --- a/tests/Language/VisitorTest.php +++ b/tests/Language/VisitorTest.php @@ -70,7 +70,12 @@ class VisitorTest extends ValidatorTestCase /** @var Node $node */ [$node, $key, $parent, $path, $ancestors] = $args; - $parentArray = $parent && ! is_array($parent) ? ($parent instanceof NodeList ? iterator_to_array($parent) : $parent->toArray()) : $parent; + $parentArray = $parent && ! is_array($parent) + ? ($parent instanceof NodeList + ? iterator_to_array($parent) + : $parent->toArray() + ) + : $parent; self::assertInstanceOf(Node::class, $node); self::assertContains($node->kind, array_keys(NodeKind::$classMap)); @@ -114,7 +119,9 @@ class VisitorTest extends ValidatorTestCase { $result = $ast; foreach ($path as $key) { - $resultArray = $result instanceof NodeList ? iterator_to_array($result) : $result->toArray(); + $resultArray = $result instanceof NodeList + ? iterator_to_array($result) + : $result->toArray(); self::assertArrayHasKey($key, $resultArray); $result = $resultArray[$key]; } diff --git a/tests/Utils/SchemaExtenderTest.php b/tests/Utils/SchemaExtenderTest.php index 341a381..03529a4 100644 --- a/tests/Utils/SchemaExtenderTest.php +++ b/tests/Utils/SchemaExtenderTest.php @@ -219,7 +219,9 @@ class SchemaExtenderTest extends TestCase { $ast = Parser::parse(SchemaPrinter::doPrint($extendedSchema)); $ast->definitions = array_values(array_filter( - $ast->definitions instanceof NodeList ? iterator_to_array($ast->definitions->getIterator()) : $ast->definitions, + $ast->definitions instanceof NodeList + ? iterator_to_array($ast->definitions->getIterator()) + : $ast->definitions, function (Node $node) : bool { return ! in_array(Printer::doPrint($node), $this->testSchemaDefinitions, true); } diff --git a/tools/gendocs.php b/tools/gendocs.php index d28b0c9..7f74842 100644 --- a/tools/gendocs.php +++ b/tools/gendocs.php @@ -40,7 +40,9 @@ function renderClassMethod(ReflectionMethod $method) { $def = $type . '$' . $p->getName(); if ($p->isDefaultValueAvailable()) { - $val = $p->isDefaultValueConstant() ? $p->getDefaultValueConstantName() : $p->getDefaultValue(); + $val = $p->isDefaultValueConstant() + ? $p->getDefaultValueConstantName() + : $p->getDefaultValue(); $def .= " = " . Utils::printSafeJson($val); } From 91b72f145d0e91eaaff4e1d7fd8c264d2eb51cf2 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 18:25:02 +0200 Subject: [PATCH 02/11] Context is the 3rd, ResolveInfo the 4th resolver argument --- docs/reference.md | 2 +- src/Executor/ReferenceExecutor.php | 10 +++++----- src/Type/Definition/ResolveInfo.php | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/reference.md b/docs/reference.md index 7ea7709..f2d828e 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -299,7 +299,7 @@ static function getNullableType($type) ``` # GraphQL\Type\Definition\ResolveInfo Structure containing information useful for field resolution process. -Passed as 3rd argument to every field resolver. See [docs on field resolving (data fetching)](data-fetching.md). +Passed as 4th argument to every field resolver. See [docs on field resolving (data fetching)](data-fetching.md). **Class Props:** ```php diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 6e35f7c..91009cf 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -526,7 +526,11 @@ class ReferenceExecutor implements ExecutorImplementation return self::$UNDEFINED; } $returnType = $fieldDef->getType(); - // The resolve function's optional third argument is a collection of + // The resolve function's optional 3rd argument is a context value that + // is provided to every resolve function within an execution. It is commonly + // used to represent an authenticated user, or request-specific caches. + $context = $exeContext->contextValue; + // The resolve function's optional 4th argument is a collection of // information about the current execution state. $info = new ResolveInfo( $fieldName, @@ -547,10 +551,6 @@ class ReferenceExecutor implements ExecutorImplementation } else { $resolveFn = $this->exeContext->fieldResolver; } - // The resolve function's optional third argument is a context value that - // is provided to every resolve function within an execution. It is commonly - // used to represent an authenticated user, or request-specific caches. - $context = $exeContext->contextValue; // Get the resolve function, regardless of if its result is normal // or abrupt (error). $result = $this->resolveOrError( diff --git a/src/Type/Definition/ResolveInfo.php b/src/Type/Definition/ResolveInfo.php index f2525de..7684e93 100644 --- a/src/Type/Definition/ResolveInfo.php +++ b/src/Type/Definition/ResolveInfo.php @@ -15,7 +15,8 @@ use function array_merge_recursive; /** * Structure containing information useful for field resolution process. - * Passed as 3rd argument to every field resolver. See [docs on field resolving (data fetching)](data-fetching.md). + * + * Passed as 4th argument to every field resolver. See [docs on field resolving (data fetching)](data-fetching.md). */ class ResolveInfo { From 8381f67bd8f2f2d5539435cdcbfc15a0e3409d93 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 18:34:19 +0200 Subject: [PATCH 03/11] Add one more --- src/Executor/ReferenceExecutor.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 08911e4..b7784e1 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -1324,9 +1324,9 @@ class ReferenceExecutor implements ExecutorImplementation ResolveInfo $info, &$result ) { - $runtimeType = is_string($runtimeTypeOrName) ? - $this->exeContext->schema->getType($runtimeTypeOrName) : - $runtimeTypeOrName; + $runtimeType = is_string($runtimeTypeOrName) + ? $this->exeContext->schema->getType($runtimeTypeOrName) + : $runtimeTypeOrName; if (! $runtimeType instanceof ObjectType) { throw new InvariantViolation( sprintf( From bc66034f400d76ac961035555102a93ae579a0f6 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 18:40:56 +0200 Subject: [PATCH 04/11] Rename parameters and private fields to match what they contain --- composer.json | 2 +- src/Executor/ExecutionContext.php | 8 ++--- src/Executor/ReferenceExecutor.php | 48 ++++++++++++++++-------------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/composer.json b/composer.json index b688618..5b63d0b 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "API" ], "require": { - "php": "^7.1||^8.0", + "php": "^7.1", "ext-json": "*", "ext-mbstring": "*" }, diff --git a/src/Executor/ExecutionContext.php b/src/Executor/ExecutionContext.php index 3de1a2e..13e675a 100644 --- a/src/Executor/ExecutionContext.php +++ b/src/Executor/ExecutionContext.php @@ -45,7 +45,7 @@ class ExecutionContext public $errors; /** @var PromiseAdapter */ - public $promises; + public $promiseAdapter; public function __construct( $schema, @@ -53,7 +53,7 @@ class ExecutionContext $root, $contextValue, $operation, - $variables, + $variableValues, $errors, $fieldResolver, $promiseAdapter @@ -63,10 +63,10 @@ class ExecutionContext $this->rootValue = $root; $this->contextValue = $contextValue; $this->operation = $operation; - $this->variableValues = $variables; + $this->variableValues = $variableValues; $this->errors = $errors ?: []; $this->fieldResolver = $fieldResolver; - $this->promises = $promiseAdapter; + $this->promiseAdapter = $promiseAdapter; } public function addError(Error $error) diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 6e35f7c..3f285a9 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -212,7 +212,7 @@ class ReferenceExecutor implements ExecutorImplementation // But for the "sync" case it is always fulfilled return $this->isPromise($result) ? $result - : $this->exeContext->promises->createFulfilled($result); + : $this->exeContext->promiseAdapter->createFulfilled($result); } /** @@ -262,7 +262,7 @@ class ReferenceExecutor implements ExecutorImplementation if ($error instanceof Error) { $this->exeContext->addError($error); - return $this->exeContext->promises->createFulfilled(null); + return $this->exeContext->promiseAdapter->createFulfilled(null); } } ); @@ -574,6 +574,7 @@ class ReferenceExecutor implements ExecutorImplementation /** * This method looks up the field on the given type definition. + * * It has special casing for the two introspection fields, __schema * and __typename. __typename is special because it can always be * queried as a field, even in situations where no other fields @@ -608,8 +609,8 @@ class ReferenceExecutor implements ExecutorImplementation } /** - * Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField` - * function. Returns the result of resolveFn or the abrupt-return Error object. + * Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField` function. + * Returns the result of resolveFn or the abrupt-return Error object. * * @param FieldDefinition $fieldDef * @param FieldNode $fieldNode @@ -623,7 +624,7 @@ class ReferenceExecutor implements ExecutorImplementation private function resolveOrError($fieldDef, $fieldNode, $resolveFn, $source, $context, $info) { try { - // Build hash of arguments from the field.arguments AST, using the + // Build a map of arguments from the field.arguments AST, using the // variables scope to fulfill any variable references. $args = Values::getArgumentValues( $fieldDef, @@ -685,7 +686,7 @@ class ReferenceExecutor implements ExecutorImplementation function ($error) use ($exeContext) { $exeContext->addError($error); - return $this->exeContext->promises->createFulfilled(null); + return $this->exeContext->promiseAdapter->createFulfilled(null); } ); } @@ -732,7 +733,7 @@ class ReferenceExecutor implements ExecutorImplementation return $promise->then( null, function ($error) use ($fieldNodes, $path) { - return $this->exeContext->promises->createRejected(Error::createLocatedError( + return $this->exeContext->promiseAdapter->createRejected(Error::createLocatedError( $error, $fieldNodes, $path @@ -864,7 +865,7 @@ class ReferenceExecutor implements ExecutorImplementation */ private function isPromise($value) { - return $value instanceof Promise || $this->exeContext->promises->isThenable($value); + return $value instanceof Promise || $this->exeContext->promiseAdapter->isThenable($value); } /** @@ -880,12 +881,12 @@ class ReferenceExecutor implements ExecutorImplementation if ($value === null || $value instanceof Promise) { return $value; } - if ($this->exeContext->promises->isThenable($value)) { - $promise = $this->exeContext->promises->convertThenable($value); + if ($this->exeContext->promiseAdapter->isThenable($value)) { + $promise = $this->exeContext->promiseAdapter->convertThenable($value); if (! $promise instanceof Promise) { throw new InvariantViolation(sprintf( '%s::convertThenable is expected to return instance of GraphQL\Executor\Promise\Promise, got: %s', - get_class($this->exeContext->promises), + get_class($this->exeContext->promiseAdapter), Utils::printSafe($promise) )); } @@ -927,28 +928,27 @@ class ReferenceExecutor implements ExecutorImplementation } /** - * Complete a list value by completing each item in the list with the - * inner type + * Complete a list value by completing each item in the list with the inner type. * - * @param FieldNode[] $fieldNodes - * @param mixed[] $path - * @param mixed $result + * @param FieldNode[] $fieldNodes + * @param mixed[] $path + * @param mixed[]|Traversable &$results * * @return mixed[]|Promise * * @throws Exception */ - private function completeListValue(ListOfType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result) + private function completeListValue(ListOfType $returnType, $fieldNodes, ResolveInfo $info, $path, &$results) { $itemType = $returnType->getWrappedType(); Utils::invariant( - is_array($result) || $result instanceof Traversable, + is_array($results) || $results instanceof Traversable, 'User Error: expected iterable, but did not find one for field ' . $info->parentType . '.' . $info->fieldName . '.' ); $containsPromise = false; $i = 0; $completedItems = []; - foreach ($result as $item) { + foreach ($results as $item) { $fieldPath = $path; $fieldPath[] = $i++; $info->path = $fieldPath; @@ -959,7 +959,9 @@ class ReferenceExecutor implements ExecutorImplementation $completedItems[] = $completedItem; } - return $containsPromise ? $this->exeContext->promises->all($completedItems) : $completedItems; + return $containsPromise + ? $this->exeContext->promiseAdapter->all($completedItems) + : $completedItems; } /** @@ -1101,7 +1103,7 @@ class ReferenceExecutor implements ExecutorImplementation } } if (! empty($promisedIsTypeOfResults)) { - return $this->exeContext->promises->all($promisedIsTypeOfResults) + return $this->exeContext->promiseAdapter->all($promisedIsTypeOfResults) ->then(static function ($isTypeOfResults) use ($possibleTypes) { foreach ($isTypeOfResults as $index => $result) { if ($result) { @@ -1187,7 +1189,7 @@ class ReferenceExecutor implements ExecutorImplementation /** * @param FieldNode[] $fieldNodes * @param mixed[] $path - * @param mixed[] $result + * @param mixed $result * * @return mixed[]|Promise|stdClass * @@ -1299,7 +1301,7 @@ class ReferenceExecutor implements ExecutorImplementation { $keys = array_keys($assoc); $valuesAndPromises = array_values($assoc); - $promise = $this->exeContext->promises->all($valuesAndPromises); + $promise = $this->exeContext->promiseAdapter->all($valuesAndPromises); return $promise->then(static function ($values) use ($keys) { $resolvedResults = []; From 65e4488ce80878dee2bc059837196c91bf2451b8 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 18:41:47 +0200 Subject: [PATCH 05/11] Reformat some comments --- src/Executor/ExecutionContext.php | 2 +- src/Executor/ReferenceExecutor.php | 2 +- src/Type/Definition/ResolveInfo.php | 22 +++++++++++----------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Executor/ExecutionContext.php b/src/Executor/ExecutionContext.php index 13e675a..4e541f2 100644 --- a/src/Executor/ExecutionContext.php +++ b/src/Executor/ExecutionContext.php @@ -14,7 +14,7 @@ use GraphQL\Type\Schema; * Data that must be available at all points during query execution. * * Namely, schema of the type system that is currently executing, - * and the fragments defined in the query document + * and the fragments defined in the query document. * * @internal */ diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 3f285a9..e4f83d5 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -199,7 +199,7 @@ class ReferenceExecutor implements ExecutorImplementation public function doExecute() : Promise { // Return a Promise that will eventually resolve to the data described by - // The "Response" section of the GraphQL specification. + // the "Response" section of the GraphQL specification. // // If errors are encountered while executing a GraphQL field, only that // field and its descendants will be omitted, and sibling fields will still diff --git a/src/Type/Definition/ResolveInfo.php b/src/Type/Definition/ResolveInfo.php index f2525de..8ab5486 100644 --- a/src/Type/Definition/ResolveInfo.php +++ b/src/Type/Definition/ResolveInfo.php @@ -20,7 +20,7 @@ use function array_merge_recursive; class ResolveInfo { /** - * The name of the field being resolved + * The name of the field being resolved. * * @api * @var string @@ -36,7 +36,7 @@ class ResolveInfo public $fieldNodes = []; /** - * Expected return type of the field being resolved + * Expected return type of the field being resolved. * * @api * @var ScalarType|ObjectType|InterfaceType|UnionType|EnumType|ListOfType|NonNull @@ -44,7 +44,7 @@ class ResolveInfo public $returnType; /** - * Parent type of the field being resolved + * Parent type of the field being resolved. * * @api * @var ObjectType @@ -52,7 +52,7 @@ class ResolveInfo public $parentType; /** - * Path to this field from the very root value + * Path to this field from the very root value. * * @api * @var string[][] @@ -60,7 +60,7 @@ class ResolveInfo public $path; /** - * Instance of a schema used for execution + * Instance of a schema used for execution. * * @api * @var Schema @@ -68,7 +68,7 @@ class ResolveInfo public $schema; /** - * AST of all fragments defined in query + * AST of all fragments defined in query. * * @api * @var FragmentDefinitionNode[] @@ -76,15 +76,15 @@ class ResolveInfo public $fragments = []; /** - * Root value passed to query execution + * Root value passed to query execution. * * @api - * @var mixed|null + * @var mixed */ public $rootValue; /** - * AST of operation definition node (query, mutation) + * AST of operation definition node (query, mutation). * * @api * @var OperationDefinitionNode|null @@ -92,7 +92,7 @@ class ResolveInfo public $operation; /** - * Array of variables passed to query execution + * Array of variables passed to query execution. * * @api * @var mixed[] @@ -136,7 +136,7 @@ class ResolveInfo /** * Helper method that returns names of all fields selected in query for - * $this->fieldName up to $depth levels + * $this->fieldName up to $depth levels. * * Example: * query MyQuery{ From 9ca7bb6ea1f7dbd5d3f3ad8dc8a0a6474a2b3f70 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 18:42:25 +0200 Subject: [PATCH 06/11] Expand one letter variable names --- tests/Executor/DeferredFieldsTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Executor/DeferredFieldsTest.php b/tests/Executor/DeferredFieldsTest.php index e68388c..439d117 100644 --- a/tests/Executor/DeferredFieldsTest.php +++ b/tests/Executor/DeferredFieldsTest.php @@ -401,7 +401,7 @@ class DeferredFieldsTest extends TestCase return [ 'sync' => [ 'type' => Type::string(), - 'resolve' => function ($v, $a, $c, ResolveInfo $info) { + 'resolve' => function ($val, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return 'sync'; @@ -409,7 +409,7 @@ class DeferredFieldsTest extends TestCase ], 'deferred' => [ 'type' => Type::string(), - 'resolve' => function ($v, $a, $c, ResolveInfo $info) { + 'resolve' => function ($val, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return new Deferred(function () use ($info) { @@ -421,7 +421,7 @@ class DeferredFieldsTest extends TestCase ], 'nest' => [ 'type' => $complexType, - 'resolve' => function ($v, $a, $c, ResolveInfo $info) { + 'resolve' => function ($val, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return []; @@ -429,7 +429,7 @@ class DeferredFieldsTest extends TestCase ], 'deferredNest' => [ 'type' => $complexType, - 'resolve' => function ($v, $a, $c, ResolveInfo $info) { + 'resolve' => function ($val, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return new Deferred(function () use ($info) { From 218e02a88ca0e51932526c2db2be0209918b1a5b Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 18:46:27 +0200 Subject: [PATCH 07/11] Revert composer.json changes --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5b63d0b..b688618 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "API" ], "require": { - "php": "^7.1", + "php": "^7.1||^8.0", "ext-json": "*", "ext-mbstring": "*" }, From a34bb68d65833491b958e3026ac5dd064210bc0a Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 21:03:17 +0200 Subject: [PATCH 08/11] Resolve todo in Boolean coercion, add explanation, update test names to match reference implementation --- src/Type/Definition/BooleanType.php | 9 ++++--- tests/Type/ScalarSerializationTest.php | 34 ++++++++++++++------------ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Type/Definition/BooleanType.php b/src/Type/Definition/BooleanType.php index 56b5f0f..f9c91bb 100644 --- a/src/Type/Definition/BooleanType.php +++ b/src/Type/Definition/BooleanType.php @@ -20,11 +20,14 @@ class BooleanType extends ScalarType public $description = 'The `Boolean` scalar type represents `true` or `false`.'; /** - * @param mixed $value + * Coerce the given value to a boolean. * - * @return bool + * The GraphQL spec leaves this up to the implementations, so we just do what + * PHP does natively to make this intuitive for developers. + * + * @param mixed $value */ - public function serialize($value) + public function serialize($value) : bool { return (bool) $value; } diff --git a/tests/Type/ScalarSerializationTest.php b/tests/Type/ScalarSerializationTest.php index 17f2368..f7b3c6d 100644 --- a/tests/Type/ScalarSerializationTest.php +++ b/tests/Type/ScalarSerializationTest.php @@ -13,9 +13,9 @@ class ScalarSerializationTest extends TestCase { // Type System: Scalar coercion /** - * @see it('serializes output int') + * @see it('serializes output as Int') */ - public function testSerializesOutputInt() : void + public function testSerializesOutputAsInt() : void { $intType = Type::int(); @@ -114,9 +114,9 @@ class ScalarSerializationTest extends TestCase } /** - * @see it('serializes output float') + * @see it('serializes output as Float') */ - public function testSerializesOutputFloat() : void + public function testSerializesOutputAsFloat() : void { $floatType = Type::float(); @@ -149,9 +149,9 @@ class ScalarSerializationTest extends TestCase } /** - * @see it('serializes output strings') + * @see it('serializes output as String') */ - public function testSerializesOutputStrings() : void + public function testSerializesOutputAsString() : void { $stringType = Type::string(); @@ -181,23 +181,27 @@ class ScalarSerializationTest extends TestCase } /** - * @see it('serializes output boolean') + * @see it('serializes output as Boolean') */ - public function testSerializesOutputBoolean() : void + public function testSerializesOutputAsBoolean() : void { $boolType = Type::boolean(); - self::assertTrue($boolType->serialize('string')); - self::assertFalse($boolType->serialize('')); - self::assertTrue($boolType->serialize('1')); - self::assertTrue($boolType->serialize(1)); - self::assertFalse($boolType->serialize(0)); self::assertTrue($boolType->serialize(true)); + self::assertTrue($boolType->serialize(1)); + self::assertTrue($boolType->serialize('1')); + self::assertTrue($boolType->serialize('string')); + self::assertFalse($boolType->serialize(false)); - // TODO: how should it behave on '0'? + self::assertFalse($boolType->serialize(0)); + self::assertFalse($boolType->serialize('0')); + self::assertFalse($boolType->serialize('')); } - public function testSerializesOutputID() : void + /** + * @see it('serializes output as ID') + */ + public function testSerializesOutputAsID() : void { $idType = Type::id(); From e704f8cc5c15c9f87d494efa352d20686cb966c6 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 20:36:30 +0200 Subject: [PATCH 09/11] Split some long lines into multiples --- src/Experimental/Executor/CoroutineExecutor.php | 5 ++++- src/Type/Definition/ResolveInfo.php | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Experimental/Executor/CoroutineExecutor.php b/src/Experimental/Executor/CoroutineExecutor.php index 3aba3e2..f823aea 100644 --- a/src/Experimental/Executor/CoroutineExecutor.php +++ b/src/Experimental/Executor/CoroutineExecutor.php @@ -821,7 +821,10 @@ class CoroutineExecutor implements Runtime, ExecutorImplementation } else { $childContexts = []; - foreach ($this->collector->collectFields($objectType, $ctx->shared->mergedSelectionSet ?? $this->mergeSelectionSets($ctx)) as $childShared) { + foreach ($this->collector->collectFields( + $objectType, + $ctx->shared->mergedSelectionSet ?? $this->mergeSelectionSets($ctx) + ) as $childShared) { /** @var CoroutineContextShared $childShared */ $childPath = $path; diff --git a/src/Type/Definition/ResolveInfo.php b/src/Type/Definition/ResolveInfo.php index f2525de..ee2aa7b 100644 --- a/src/Type/Definition/ResolveInfo.php +++ b/src/Type/Definition/ResolveInfo.php @@ -177,7 +177,10 @@ class ResolveInfo /** @var FieldNode $fieldNode */ foreach ($this->fieldNodes as $fieldNode) { - $fields = array_merge_recursive($fields, $this->foldSelectionSet($fieldNode->selectionSet, $depth)); + $fields = array_merge_recursive( + $fields, + $this->foldSelectionSet($fieldNode->selectionSet, $depth) + ); } return $fields; From a222cc9137580769caaa564f500f59472c518693 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 21:08:49 +0200 Subject: [PATCH 10/11] Reword a comment --- src/Utils/MixedStore.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utils/MixedStore.php b/src/Utils/MixedStore.php index 469abaa..8513fd5 100644 --- a/src/Utils/MixedStore.php +++ b/src/Utils/MixedStore.php @@ -21,7 +21,7 @@ use function is_string; * Similar to PHP array, but allows any type of data to act as key (including arrays, objects, scalars) * * Note: unfortunately when storing array as key - access and modification is O(N) - * (yet this should be really rare case and should be avoided when possible) + * (yet this should rarely be the case and should be avoided when possible) */ class MixedStore implements ArrayAccess { From 99453076b520f0ebdac7d392d2b35b053dd272cf Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 30 Jun 2019 20:57:08 +0200 Subject: [PATCH 11/11] Remove &$ in phpdoc --- src/Executor/ReferenceExecutor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index e4f83d5..f2e85ff 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -932,7 +932,7 @@ class ReferenceExecutor implements ExecutorImplementation * * @param FieldNode[] $fieldNodes * @param mixed[] $path - * @param mixed[]|Traversable &$results + * @param mixed[]|Traversable $results * * @return mixed[]|Promise *