【问题标题】:How to get custom directive arguments in lighthouse graphql laravel如何在 lighthouse graphql laravel 中获取自定义指令参数
【发布时间】:2020-08-02 23:14:15
【问题描述】:

我的问题是关于自定义指令 https://lighthouse-php.com/4.11/the-basics/directives.html#definition

我的架构是:

type Query {
  sayFriendly: String @append(text: ", please.")
}

在 lighthouse.php 的配置中,我已经有了

'namespaces' => [
        'models' => ['App', 'App\\Models'],
        'queries' => 'App\\GraphQL\\Queries',
        'mutations' => 'App\\GraphQL\\Mutations',
        'subscriptions' => 'App\\GraphQL\\Subscriptions',
        'interfaces' => 'App\\GraphQL\\Interfaces',
        'unions' => 'App\\GraphQL\\Unions',
        'scalars' => 'App\\GraphQL\\Scalars',
        'directives' => ['App\\GraphQL\\Directives'],
    ],

指令已启用。

我将 \App\GraphQL\Directives\appendDirective 中的指令定义为

<?php

namespace App\GraphQL\Directives;

use Closure;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\Contracts\Directive;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;

class appendDirective implements Directive, FieldMiddleware
{
    /**
     * Name of the directive as used in the schema.
     *
     * @return string
     */
    public function name(): string
    {
        return 'append';
    }

    /**
     * Wrap around the final field resolver.
     *
     * @param \Nuwave\Lighthouse\Schema\Values\FieldValue $fieldValue
     * @param \Closure $next
     * @return \Nuwave\Lighthouse\Schema\Values\FieldValue
     */
    public function handleField(FieldValue $fieldValue, Closure $next): FieldValue
    {

        // Retrieve the existing resolver function
        /** @var Closure $previousResolver */
        $previousResolver = $fieldValue->getResolver();

        // Wrap around the resolver
        $wrappedResolver = function ($root, array $args, GraphQLContext $context, ResolveInfo $info) use ($previousResolver): string {
            // Call the resolver, passing along the resolver arguments
            /** @var string $result */
            $result = $previousResolver($root, $args, $context, $info);
            return ($result);
        };

        // Place the wrapped resolver back upon the FieldValue
        // It is not resolved right now - we just prepare it
        $fieldValue->setResolver($wrappedResolver);

        // Keep the middleware chain going
        return $next($fieldValue);
    }

}

如何从指令中获取键“text”的值并附加到 $result [ @append(text: ", please.") ]。 $args 是一个空数组(应该是因为我确实做了参数化查询 [sayFriendly] )

【问题讨论】:

    标签: php laravel graphql laravel-lighthouse


    【解决方案1】:

    如果您从 Nuwave\Lighthouse\Schema\Directives\BaseDirective 扩展指令,您可以访问 $this-&gt;directiveArgValue('text'); 以检索自定义指令的文本参数。

    $args 是空的,因为这是客户端在查询中提供的参数,在 sayFriendly 查询示例中没有可能的参数,因此它始终为空。

    作为提示:您可以通过查看 Lighthouse 内部的 already implemented directives 来找到这一点,自定义指令的文档有点薄,但您可以通过查看 Lighthouse 提供的指令学到很多东西。

    【讨论】:

      猜你喜欢
      • 2020-01-18
      • 2021-12-22
      • 2021-07-08
      • 2021-06-18
      • 1970-01-01
      • 2020-11-06
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多