Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial Error That Doesn't Nullify Data #432

Closed
ievgenklymenko opened this issue Jan 16, 2019 · 3 comments
Closed

Partial Error That Doesn't Nullify Data #432

ievgenklymenko opened this issue Jan 16, 2019 · 3 comments

Comments

@ievgenklymenko
Copy link

Is there a natural solution for partial errors?

Say we have an existing schema, with a scalar non-nullable field, and we need both the field data and error which occurred when resolving that field presented in top-level 'errors' in response; this is legal by GraphQL spec.

#374 issue was closed, but solved only for that, particular, (array data) case. Because there, it is possible to return exception (which turns to null in response) instead of array element. But we can't return exception because that would erase resolved data.

So the only solution is to do manual manipulations with response, is that it? There is no natural way of doing it in webonyx/graphql-php ?

@vladar
Copy link
Member

vladar commented Jan 17, 2019

It should be really straightforward to do in the userland by utilizing context. Something along the lines:

Resolver:

use GraphQL\Error\Error;

'resolve' => function($parentValue, $args, $context, $info) {
    $context->errors[] = Error::createLocatedError('Error message', $info->fieldNodes, $info->path);
    return '';
}

Then when you run the query:

$context = new MyContext();
$result = GraphQL::executeQuery($schema, $source, null, $context);
$result->errors = array_merge($result->errors, $context->errors);

@spawnia
Copy link
Collaborator

spawnia commented Jul 20, 2020

@vladar we recently implemented something quite similar to your suggested approach in Lighthouse. Works good so far.

I wonder if we could ease support by adding a special result class that graphql-php recognizes:

class Result {
    /** @var mixed The value returned from the resolver */
    public $value;

    /** @var array<\Throwable> Non-fatal errors that come up during resolution */
    public $errors = [];
}

We could then check in the executor:

$result = $resolver(...);

if ($result instanceof Result) {
  $this->addErrorsToContext($result->errors);
  $result = $result->value;
}

@vladar
Copy link
Member

vladar commented Jul 20, 2020

Yeah, I had a similar idea in the past with a potentially wider scope: #225 But never saw a real-world demand for it or a benefit that would justify adding this complexity.

And actually I think the better approach would be to suggest this kind of thing in graphql-js first and then port it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants