Skip to content

Confusion over prefetching #428

@calbro7

Description

@calbro7

Hi, I'm a bit confused by the sample code in the prefetching section of the docs:

#[Type]
class PostType {
    /**
     * @param Post $post
     * @param mixed $prefetchedUsers
     * @return User
     */
    #[Field(prefetchMethod: "prefetchUsers")]
    public function getUser(Post $post, $prefetchedUsers): User
    {
        // This method will receive the $prefetchedUsers as second argument. This is the return value of the "prefetchUsers" method below.
        // Using this prefetched list, it should be easy to map it to the post
    }

    /**
     * @param Post[] $posts
     * @return mixed
     */
    public function prefetchUsers(iterable $posts)
    {
        // This function is called only once per GraphQL request
        // with the list of posts. You can fetch the list of users
        // associated with this posts in a single request,
        // for instance using a "IN" query in SQL or a multi-fetch
        // in your cache back-end.
    }
}

The class is PostType but all the type annotations are for Post. Is this a mistake and the types are supposed to match up? Or else what is Post?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions