Skip to content

Cleaner and simpler field deferring #738

Open
@oprypkhantc

Description

@oprypkhantc

Hey

We've found use cases for deferring field resolution directly, without parameters or prefetch, e.g.:

#[Type]
class SomeType {
    #[Field(outputType: 'Int!')]
    public function complexCalculation(
        bool $arg1
    ): Deferred {
        // Do something with $arg1, similar to prefetch

        // Return value from some kind of buffer, again, similar to prefetch
        return new Deferred(fn () => 123);
    }
}

Currently it's possible, but ugly:

  • requires the use of outputType
  • forces the return type to be GraphQL\Deferred
  • forces creation of new GraphQL\Deferred instance everywhere

I propose that such cases are simplified into returning a (typed) callable, which removes the dependency on Deferred from userland code, as well as allows specifying an actual PHPDoc type:

#[Type]
class SomeType {
    /** @return callable(): int */
    #[Field]
    public function complexCalculation(
        bool $arg1
    ): callable {
        // Do something with $arg1, similar to prefetch

        // Return value from some kind of buffer, again, similar to prefetch
        return fn () => 123;
    }
}

The implementation should be somewhat trivial - under the hood the callable value would just get wrapped in Deferred, and additional root type mapper would handle callable() PHPDoc types. Thoughts?

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