如何在lighthouse graphql laravel中获取自定义指令参数?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在lighthouse graphql laravel中获取自定义指令参数?相关的知识,希望对你有一定的参考价值。
我的问题是关于自定义指令https:/lighthouse-php.com4.11the-basicsdirectives.html#定义。
我的模式是
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 AppGraphQLDirectives;
use Closure;
use GraphQLTypeDefinitionResolveInfo;
use NuwaveLighthouseSchemaValuesFieldValue;
use NuwaveLighthouseSupportContractsDirective;
use NuwaveLighthouseSupportContractsFieldMiddleware;
use NuwaveLighthouseSupportContractsGraphQLContext;
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 NuwaveLighthouseSchemaValuesFieldValue $fieldValue
* @param Closure $next
* @return NuwaveLighthouseSchemaValuesFieldValue
*/
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])。
如果你把你的指令从 NuwaveLighthouseSchemaDirectivesBaseDirective
您可以获得 $this->directiveArgValue('text');
来检索自定义指令的文本参数。
这个 $args
是空的,因为那是客户端在查询中提供的参数,在 sayFriendly
查询的例子中不可能有args,所以总是空的。
小贴士:你可以通过查看一个叫做 已执行的指令 在Lighthouse内部,关于自定义指令的文档有点少,但通过查看Lighthouse提供的指令,你可以学到很多东西。
以上是关于如何在lighthouse graphql laravel中获取自定义指令参数?的主要内容,如果未能解决你的问题,请参考以下文章
Laravel Lighthouse GraphQL - 在服务器端排序
Lighthouse GraphQL 将 ID 转换为字符串而不是整数?
变异中的 Laravel LightHouse GraphQL DateTime 输入始终为空