【问题标题】:Laravel Lighthouse Where to place non root field resolvers?Laravel Lighthouse 在哪里放置非根字段解析器?
【发布时间】:2021-04-29 06:48:12
【问题描述】:

在灯塔文档的示例中,他们给出了解析非根字段的示例方法。

<?php

use App\Models\User;

function resolveUserId(User $user): string
{
    return $user->name; // test...
}

所以我的猜测是灯塔会搜索一个 resolveUser{Field} 方法,如果它存在就调用它。但是我在哪里放置方法?示例中没有类名或命名空间。

类型查询 @guard(with: ["api"]) { 我:用户! @auth }

type User {
    id: ID!
    name: String!
    email: String!
    posts (content_contains: [String]): [Post!]! @hasMany #Would like to be able to resolve content_contains somehow...
    created_at: DateTime!
    updated_at: DateTime!
}

文档网址:

https://lighthouse-php.com/4.9/the-basics/fields.html#resolving-non-root-fields

【问题讨论】:

    标签: laravel-lighthouse graphql-php


    【解决方案1】:

    例如,您想使用@field 吗?它可以是任何类,具有任何命名空间。请务必明确说明。例如,在app/Graphql/UserPostsResolver.php 中,您可以拥有一个具有handle 函数的类。因此,在您的架构中,您将使用 posts: [Post!] @field(resolver: "App\\Graphql\\UserPostsResolver@handle")

    【讨论】:

    • 哦,我以为它会自动在某处搜索方法,就像通过在 app\GraphQL\Queries\???.php 中具有相同(大写)名称来找到查询一样
    猜你喜欢
    • 1970-01-01
    • 2012-06-09
    • 2018-03-29
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多