From 926893ea74a4caf79cb73fced5086d334cafc824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E6=96=87=E5=8D=9A?= Date: Tue, 8 Oct 2019 13:47:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=20lighthouse=20~4.0=EF=BC=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4=20group=20=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- .../Directives/GetlistDirective.php | 45 +++++++--- src/Lighthouse/Directives/GroupDirective.php | 88 ------------------- .../Directives/PaginationManipulator.php | 8 +- 4 files changed, 36 insertions(+), 107 deletions(-) delete mode 100644 src/Lighthouse/Directives/GroupDirective.php diff --git a/composer.json b/composer.json index 2bc66f2..56133b6 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require-dev": { "composer/composer": "^1.7", "mockery/mockery": "~1.0", - "nuwave/lighthouse": "~3.0", + "nuwave/lighthouse": "~4.0", "orchestra/database": "3.5.*|3.6.*|3.7.*@dev", "orchestra/testbench": "3.5.*|3.6.*|3.7.*", "phpunit/phpunit": "~7.0" diff --git a/src/Lighthouse/Directives/GetlistDirective.php b/src/Lighthouse/Directives/GetlistDirective.php index 5137428..0f44759 100644 --- a/src/Lighthouse/Directives/GetlistDirective.php +++ b/src/Lighthouse/Directives/GetlistDirective.php @@ -10,12 +10,14 @@ use GraphQL\Language\AST\ObjectTypeDefinitionNode; use Nuwave\Lighthouse\Exceptions\DirectiveException; use Nuwave\Lighthouse\Schema\Directives\BaseDirective; +use Nuwave\Lighthouse\Support\Contracts\DefinedDirective; use Nuwave\Lighthouse\Support\Contracts\FieldResolver; use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; use MatrixLab\LaravelAdvancedSearch\ConditionsGeneratorTrait; -class GetlistDirective extends BaseDirective implements FieldResolver, FieldManipulator +class GetlistDirective extends BaseDirective implements FieldResolver, FieldManipulator, DefinedDirective { + use ConditionsGeneratorTrait; /** @@ -28,30 +30,45 @@ public function name(): string return 'getlist'; } + public static function definition(): string + { + return /* @lang GraphQL */ <<<'SDL' +""" +Query multiple entries as a paginated list. +""" +directive @getlist( + """ + Reference a function to customize the complexity score calculation. + Consists of two parts: a class name and a method name, seperated by an `@` symbol. + If you pass only a class name, the method name defaults to `__invoke`. + """ + resolver: String +) on FIELD_DEFINITION +SDL; + } + /** - * @param FieldDefinitionNode $fieldDefinition - * @param ObjectTypeDefinitionNode $parentType - * @param DocumentAST $current + * @param FieldDefinitionNode $fieldDefinition + * @param ObjectTypeDefinitionNode $parentType + * @param DocumentAST $current * * @return DocumentAST * @throws DirectiveException * @throws \Nuwave\Lighthouse\Exceptions\ParseException */ - public function manipulateSchema(FieldDefinitionNode $fieldDefinition, ObjectTypeDefinitionNode $parentType, DocumentAST $current): DocumentAST - { - return PaginationManipulator::transformToPaginatedField( - $this->getPaginationType(), - $fieldDefinition, - $parentType, - $current, - $this->directiveArgValue('defaultCount') - ); + public function manipulateFieldDefinition( + DocumentAST &$documentAST, + FieldDefinitionNode &$fieldDefinition, + ObjectTypeDefinitionNode &$parentType + ): void { + PaginationManipulator::transformToPaginatedField($this->getPaginationType(), $fieldDefinition, $parentType, + $documentAST, $this->directiveArgValue('defaultCount')); } /** * Resolve the field directive. * - * @param FieldValue $fieldValue + * @param FieldValue $fieldValue * * @return FieldValue * @throws DirectiveException diff --git a/src/Lighthouse/Directives/GroupDirective.php b/src/Lighthouse/Directives/GroupDirective.php deleted file mode 100644 index 83d5ef1..0000000 --- a/src/Lighthouse/Directives/GroupDirective.php +++ /dev/null @@ -1,88 +0,0 @@ -directiveArgValue('namespace'); - - if (! $namespaceValue) { - return $objectType; - } - - if (! is_string($namespaceValue)) { - throw new DirectiveException('The value of the namespace directive on has to be a string'); - } - - $namespaceValue = addslashes($namespaceValue); - - $objectType->fields = new NodeList( - collect($objectType->fields) - ->map(function (FieldDefinitionNode $fieldDefinition) use ($namespaceValue) { - $existingNamespaces = ASTHelper::directiveDefinition( - $fieldDefinition, - NamespaceDirective::NAME - ); - - $newNamespaceDirective = $existingNamespaces - ? $this->mergeNamespaceOnExistingDirective($namespaceValue, $existingNamespaces) - : PartialParser::directive("@namespace(field: \"$namespaceValue\", complexity: \"$namespaceValue\", getlist: \"$namespaceValue\")"); - - $fieldDefinition->directives = $fieldDefinition->directives->merge([$newNamespaceDirective]); - - return $fieldDefinition; - }) - ->toArray() - ); - - return $objectType; - } - - /** - * @param string $namespaceValue - * @param DirectiveNode $directive - * - * @return DirectiveNode - */ - protected function mergeNamespaceOnExistingDirective(string $namespaceValue, DirectiveNode $directive): DirectiveNode - { - $namespaces = PartialParser::arguments([ - "field: \"$namespaceValue\"", - "complexity: \"$namespaceValue\"", - "getlist: \"$namespaceValue\"", - ]); - - $directive->arguments = $directive->arguments->merge($namespaces); - - return $directive; - } -} diff --git a/src/Lighthouse/Directives/PaginationManipulator.php b/src/Lighthouse/Directives/PaginationManipulator.php index e7b65ea..b7e95a6 100755 --- a/src/Lighthouse/Directives/PaginationManipulator.php +++ b/src/Lighthouse/Directives/PaginationManipulator.php @@ -90,10 +90,10 @@ public static function registerPaginator(FieldDefinitionNode $fieldDefinition, O $fieldDefinition->type = PartialParser::namedType($paginatorTypeName); $parentType->fields = ASTHelper::mergeNodeList($parentType->fields, [$fieldDefinition]); - $documentAST->setDefinition($paginatorType); - $documentAST->setDefinition($parentType); - $documentAST->setDefinition($paginationCursor); - $documentAST->setDefinition($paginatorInput); + $documentAST->setTypeDefinition($paginatorType); + $documentAST->setTypeDefinition($parentType); + $documentAST->setTypeDefinition($paginationCursor); + $documentAST->setTypeDefinition($paginatorInput); return $documentAST; }