1

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

<?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

4

1 回答 1

1

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

于 2021-01-25T22:41:31.170 回答